Range of object _Worksheet failed

  • Thread starter Thread starter cparsons
  • Start date Start date
C

cparsons

I am getting an error and not really sure why.

I have a button on worksheet "Test1". When it is pressed it tries to
count the number of columns that have data on worksheet "Test1" and
"Test2". The code that counts the columns on "Test1" works fine but
the same exact code which checks "Test2" is erring with "Method 'Range'
of object '_worksheet' failed".

I am only showing the code for Test2 here but Test1 is an exact
duplicate with the worksheet name being changed.



Code:
--------------------

Dim Cnt as Integer

With Worksheets("Test2").Range("A3")
Cnt = Range(.Offset(0, 0), .End(xlToRight)).Columns.Count
End With
 
See if this gives you the ans you want

Sub countcols()
cnt = Sheets("test2").Range("A3").End(xlToRight).Column
MsgBox cnt
End Sub
 
Back
Top