was wondering if anyone could help me solve the following problem.
I’m given a runtime error 1004, over at With Worksheets("Sheet1").Cells(Rowtosave, EGCheck) Have spent quite some time trying out but still unable to define the error myself. Should anyone nice, care to help me out? Will appreciate your help. Thanks!
Sub Macro1()
Dim LastRow1 As Long, RowCheck As Long, Rowtosave As Long, LastCol1 As Long
Dim EGCheck As Long, ColEG As Range, firstEG As Long
LastRow1 = 50
LastCol1 = 50
For RowCheck = 1 To LastRow1
'Look for "Name"
With Worksheets("Sheet1").Cells(RowCheck, 1)
If .Value = "Name" Then
'Set row to Rowtosave for later use
Rowtosave = RowCheck
End If
End With
Next RowCheck
For EGCheck = 1 To LastCol1
'Look for EG on the name row with varying column
'Since already obtain the row for name as Rowtosave, so set Row to Rowtosave
With Worksheets("Sheet1").Cells(Rowtosave, EGCheck)
If .Value = "EG" Then
firstEG = EGCheck
End If
End With
Next EGCheck
There’s something wrong with your situation that isn’t shown here. When I enter the following:
and put
NameandEGinto cellsA6andD6respectively, I end up with a64in cellA1, as expected. In other words, it’s working fine.However, if I remove
Namefrom cellA6, I get the error 1004, same as you. That’s because, in that case,Rowtocheckis never being set, hence it will have it’s default value of zero.And, when you attempt to use 0 as the rown in the
Cells()call, it errors because the row must be one or more.