user1204868
user1204868

Reputation: 606

Run time error 1004 when trying to find row

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

Upvotes: 1

Views: 1376

Answers (2)

paxdiablo
paxdiablo

Reputation: 882686

There's something wrong with your situation that isn't shown here. When I enter the following:

Option Explicit

Sub Macro1()
    Dim LastRow1 As Long, RowCheck As Long, Rowtosave As Long
    Dim LastCol1 As Long, EGCheck As Long

    Range("a1:a1").Value = -1

    LastRow1 = 50
    LastCol1 = 50

    For RowCheck = 1 To LastRow1
        With Worksheets("Sheet1").Cells(RowCheck, 1)
            If .Value = "Name" Then
                Rowtosave = RowCheck
            End If
        End With
    Next RowCheck

    For EGCheck = 1 To LastCol1
        With Worksheets("Sheet1").Cells(Rowtosave, EGCheck)
            If .Value = "EG" Then
                Range("a1:a1").Value = Rowtosave * 10 + EGCheck
            End If
        End With
    Next EGCheck
End Sub

and put Name and EG into cells A6 and D6 respectively, I end up with a 64 in cell A1, as expected. In other words, it's working fine.

However, if I remove Name from cell A6, I get the error 1004, same as you. That's because, in that case, Rowtocheck is 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.

Upvotes: 1

Marc
Marc

Reputation: 11633

Without seeing what your sample data is, it's hard to say. But i suspect that the condition that sets "Rowtosave" is never being met, so Rowtosave = 0. There is no row/column 0, so when the suspect line in your code runs (this line):

With Worksheets("Sheet1").Cells(Rowtosave, EGCheck)

, it errors.

Upvotes: 0

Related Questions