I don't understand this error message!

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have the following code
VBA Code:
Dim lrow, low1, lrow2 As Long

lrow = Sheet6.Cells(Sheet6.Rows.Count, "B").End(xlUp).Row
lrow1 = Sheet8.Cells(Sheet8.Rows.Count, "B").End(xlUp).Row
lrow2 = Sheet13.Cells(Sheet13.Rows.Count, "A").End(xlUp).Row

If lrow2 > 1 Then
Sheet13.Range("A2:A" & lrow2).EntireRow.ClearContents  ' this line is erroring
End If

lrow2 = 571

I am getting Run Time Error 1004: method 'range' of object '_Worksheet' failed?

I'm sure this macro gets run every month without errors. Any thoughts?

Thank you in advance.
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi there

I tested very quickly on my side and it is working. Could be that you do not have a Sheet named Sheet13? Guessing you are working with the codenames of the sheet?

I did pick up from your code above that you need to change: Missing an r at lrow1...

VBA Code:
Dim lrow, low1, lrow2 As Long

to

VBA Code:
Dim lrow, lrow1, lrow2 As Long
 
Upvote 0
I don't see what could be the error. I already tried it and it works correctly.

But, I would like to recommend the following:
1. Use the Option Explicit statement at the beginning of all your code, to check the declaration of all your variables. If you had it, it would be marking an error in the variable lrow1 , since it is not declared, because in your declaration you have: low1

2. You must set the type to each variable, like this:
Dim lrow As Long, lrow1 As Long, lrow2 As Long
Otherwise, the first 2: lrow and lrow1 will be of type variant.

For example:

VBA Code:
Option Explicit

Sub test()
  Dim lrow As Long, lrow1 As Long, lrow2 As Long
  
  lrow = Sheet6.Cells(Sheet6.Rows.Count, "B").End(xlUp).Row
  lrow1 = Sheet8.Cells(Sheet8.Rows.Count, "B").End(xlUp).Row
  lrow2 = Sheet13.Cells(Sheet13.Rows.Count, "A").End(xlUp).Row ' this line is erroring
  
  If lrow2 > 1 Then
    Sheet13.Range("A2:A" & lrow2).EntireRow.ClearContents
  End If
End Sub

You could upload a copy of your file to a free site such www.dropbox.com or google drive to review it.

Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Solution
I don't see what could be the error. I already tried it and it works correctly.

But, I would like to recommend the following:
1. Use the Option Explicit statement at the beginning of all your code, to check the declaration of all your variables. If you had it, it would be marking an error in the variable lrow1 , since it is not declared, because in your declaration you have: low1

2. You must set the type to each variable, like this:
Dim lrow As Long, lrow1 As Long, lrow2 As Long
Otherwise, the first 2: lrow and lrow1 will be of type variant.

For example:

VBA Code:
Option Explicit

Sub test()
  Dim lrow As Long, lrow1 As Long, lrow2 As Long
 
  lrow = Sheet6.Cells(Sheet6.Rows.Count, "B").End(xlUp).Row
  lrow1 = Sheet8.Cells(Sheet8.Rows.Count, "B").End(xlUp).Row
  lrow2 = Sheet13.Cells(Sheet13.Rows.Count, "A").End(xlUp).Row ' this line is erroring
 
  If lrow2 > 1 Then
    Sheet13.Range("A2:A" & lrow2).EntireRow.ClearContents
  End If
End Sub

You could upload a copy of your file to a free site such www.dropbox.com or google drive to review it.

Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.

Thanks all, there is a Sheet13 so what I did was prefixed the above with
Code:
Sheet13.Select

and it worked fine. Bizarre.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top