Run-Time Error 1004:

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am a bit confused to why this error is occurring as it was working a few minutes ago.

VBA Code:
Sub EditTempTble()

Dim LastCell As String
Dim LastRow As Double
Dim iCntr As Long

LastCell = wsTmpData.Range(FindLast(3)).Address
LastRow = Range(LastCell).Row

For iCntr = LastRow To 1 Step -1
    If Trim(Cells(iCntr, 1)) = "" Then
        Rows(iCntr).Delete
    End If
Next

End Sub

The error is happening on the below line:

VBA Code:
Rows(iCntr).Delete

The last row is correct:

1648561330962.png


Please let me know if you need the rest of the code? to assist? but the other subs in this module are running like I wanted.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Is the sheet protected? That will cause a 1004 error. I see nothing else in the code that would cause an error.

By the way, this is not causing an error but you should declare LastRow as Long, not Double.
 
Upvote 0
What is the actual error message? 1004 can be a lot of things.
 
Upvote 0
Sorry, the full error message is:

1648578281164.png


And the issue is the code didn't switch to the tempData sheet I copied the table too from Data that is protected.;)
 
Upvote 0
I have added wsTmpData.activate to the code and that has solved the issue. I am not sure if that is the best way, but it is working. I didn't think about the protected pages:) thank you.
 
Upvote 0
Rather than activating the sheet you can use
VBA Code:
Sub EditTempTble()

Dim LastCell As String
Dim LastRow As Double
Dim iCntr As Long
With wsTmpData
   LastCell = .Range(FindLast(3)).Address
   LastRow = .Range(LastCell).Row
   
   For iCntr = LastRow To 1 Step -1
       If Trim(.Cells(iCntr, 1)) = "" Then
           .Rows(iCntr).Delete
       End If
   Next
End With
End Sub
 
Upvote 0
Rather than activating the sheet you can use
VBA Code:
Sub EditTempTble()

Dim LastCell As String
Dim LastRow As Double
Dim iCntr As Long
With wsTmpData
   LastCell = .Range(FindLast(3)).Address
   LastRow = .Range(LastCell).Row
 
   For iCntr = LastRow To 1 Step -1
       If Trim(.Cells(iCntr, 1)) = "" Then
           .Rows(iCntr).Delete
       End If
   Next
End With
End Sub
This still won't work because the custom function FindLast will operate on ActiveSheet unless an optional argument is provided giving the sheet name. This is discussed in another thread where the code for FindLast was posted.
 
Upvote 0

Forum statistics

Threads
1,215,746
Messages
6,126,639
Members
449,325
Latest member
Hardey6ix

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