Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
  • Avatar do usuário
#47314
Não consigo rodar a função abaixo com a variável cont1
Obrigado

Private Sub Gerar_Click()
Dim cont As Integer
cont = ContarRegistro(Worksheets("Planilha Ativa").Range(Cells(10, 2), Cells(28, 2)))

Dim cont1 As Integer
cont1 = ContarRegistro(Worksheets("Outra Planilha").Range(Cells(10, 2), Cells(28, 2)))
O primeiro funciona e no segundo ocorre um erro: Erro em tempo de execução 1004
Erro de definição de aplicativo ou de definição de objeto

End Sub
Public Function ContarRegistro(area As Range) As Long
Dim celula As Range, TotalLinhas As Long
TotalLinhas = 0
For Each celula In area
If celula <> "" Then
TotalLinhas = TotalLinhas + 1
End If
Next
ContarRegistro = TotalLinhas
End Function
#47317
Uma possibilidade:
Código: Selecionar todos
Option Explicit

Private Sub Gerar_Click()
Dim cont As Integer
cont = ContarRegistro(Worksheets("Plan1"), Range(Cells(10, 2), Cells(28, 2)))

Dim cont1 As Integer
cont1 = ContarRegistro(Worksheets("Plan2"), Range(Cells(10, 2), Cells(28, 2)))
End Sub

Public Function ContarRegistro(pln As Worksheet, area As Range) As Long
Dim celula As Range, TotalLinhas As Long
TotalLinhas = 0
For Each celula In area
If celula <> "" Then
TotalLinhas = TotalLinhas + 1
End If
Next
ContarRegistro = TotalLinhas
End Function
#47318
Código: Selecionar todos
Sub Gerar_Click()
 Dim cont As Integer, cont1 As Integer
  cont = ContarRegistro(Sheets("Planilha Ativa"), Range(Cells(10, 2), Cells(28, 2)).Address)
  cont1 = ContarRegistro(Sheets("Outra Planilha"), Range(Cells(10, 2), Cells(28, 2)).Address)
End Sub
Public Function ContarRegistro(ws As Worksheet, area As String) As Long
 Dim celula As Range, TotalLinhas As Long
  TotalLinhas = 0
   For Each celula In Sheets(ws.Name).Range(area)
    If celula <> "" Then
    TotalLinhas = TotalLinhas + 1
    End If
   Next
  ContarRegistro = TotalLinhas
  Cells(Rows.Count, 4).End(3)(2).Resize(, 2).Value = Array(ContarRegistro, ws.Name)
End Function
#47324
Luiz,

Copiei seu código, executei e rodou sem problema, e sem nenhuma alteração.

Mande a planilha pra que possamos analisar melhor.

Ou rode passo a passo pra entender em que instrução tá dando erro.

Jimmy San Juan
#47325
Obrigado Jimmy, criei uma planilha qualquer com a função que está dando erro.
Chamando a função na planilha 1 (ativa) funciona normal, já na planilha 2 apresenta erro quando peço para contar os dados da planilha 1 . Como enviar a planilha?
#47326
A sua função da forma como está elaborada só funciona para a planilha ativa. Se você trocar a planilha ativa ao rodar o código então o erro passará para o trecho do código que trata da outra planilha. Veja a função que postei, ela faz a contagem mesmo se a contagem não for na planilha ativa.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord