Vba 尝试查找行时出现运行时错误1004

Vba 尝试查找行时出现运行时错误1004,vba,excel,Vba,Excel,我想知道是否有人能帮我解决以下问题。 我收到一个运行时错误1004,在和工作表(“Sheet1”)。单元格(Rowtosave,EGCheck)已经花了相当长的时间尝试,但仍然无法自己定义错误。有人愿意帮我吗?谢谢你的帮助。谢谢 Sub Macro1() Dim LastRow1 As Long, RowCheck As Long, Rowtosave As Long, LastCol1 As Long Dim EGCheck As Long, ColEG As Range, firstEG

我想知道是否有人能帮我解决以下问题。 我收到一个运行时错误1004,在
和工作表(“Sheet1”)。单元格(Rowtosave,EGCheck)
已经花了相当长的时间尝试,但仍然无法自己定义错误。有人愿意帮我吗?谢谢你的帮助。谢谢

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

你的情况有点问题,这里没有显示。当我输入以下内容时:

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
然后将
Name
EG
分别放入单元格
A6
D6
,正如预期的那样,我在单元格
A1
中得到了一个
64
。换句话说,它工作得很好

但是,如果我从单元格
A6
中删除
Name
,我将得到错误1004,与您相同。这是因为,在这种情况下,
Rowtocheck
从未被设置,因此它的默认值为零


而且,当您尝试在
Cells()
调用中使用0作为行时,它会出错,因为该行必须是一个或多个。

您的情况中存在一些错误,此处未显示。当我输入以下内容时:

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
然后将
Name
EG
分别放入单元格
A6
D6
,正如预期的那样,我在单元格
A1
中得到了一个
64
。换句话说,它工作得很好

但是,如果我从单元格
A6
中删除
Name
,我将得到错误1004,与您相同。这是因为,在这种情况下,
Rowtocheck
从未被设置,因此它的默认值为零


而且,当您尝试在
Cells()
调用中使用0作为rown时,它会出错,因为该行必须是一个或多个。

如果看不到示例数据是什么,很难说。但我怀疑设置“Rowtosave”的条件从未满足,因此Rowtosave=0。没有行/列0,因此当代码中的可疑行(此行)运行时:


,它是错误的。

如果看不到样本数据是什么,很难说。但我怀疑设置“Rowtosave”的条件从未满足,因此Rowtosave=0。没有行/列0,因此当代码中的可疑行(此行)运行时:


,它出错了。

嗨,谢谢!我不知道为什么在我的页面上,它不起作用。。无论如何,我已经尝试将EGCheck循环放入我的RowCheck循环中,它也工作得很好。谢谢嗨,谢谢!我不知道为什么在我的页面上,它不起作用。。无论如何,我已经尝试将EGCheck循环放入我的RowCheck循环中,它也工作得很好。谢谢谢谢你的评论!我会再看一次的。。但是当我把它们作为一个循环编译在一起时,没有遇到任何问题谢谢你的评论!我会再看一次的。。但当我将它们作为一个循环编译在一起时,并没有遇到任何问题