Counting Range Data From Different Sheet

  • Thread starter Thread starter chandran19
  • Start date Start date
C

chandran19

I am using the following code to count the no of entries in colA i
sheet2
I am currently in sheet1

Sub check()
Set myrange = Sheets("sheet2").Range(Cells(1, 1), Cells(4000, 1))
MsgBox Application.CountA(myrange)
End Sub

This gives an error as follows

"run time error 1004

application or object defined error"

but when I am in sheet2 and run this macro there is no error

how to avoid this
 
Hi chandran19,

something like:
Sub check()
Dim myRange As Range
Sheets("sheet2").Activate
Set myRange = Range(Cells(1, 1), Cells(4000, 1))
MsgBox Application.CountA(myRange)
End Sub

Best regards

Wolf
 
Hi

Use it like this

In your example "cells" in the code will use the cells on the activesheet
See the dot before it in my example

Sub check()
Dim myrange As Range
With Sheets("sheet2")
Set myrange = .Range(.Cells(1, 1), .Cells(4000, 1))
End With
MsgBox Application.CountA(myrange)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top