VBA 在 2 个循环中访问单元格值时出现错误“1004”

VBA error '1004' when accessing cell values within 2 loops

我最近开始使用 VBA...我正在开发 Excel 用户窗体。当用户在组合框中进行更改时,我希望自动选择列表框中的某些字段。虽然我没有做任何不寻常的事情,但它仍然 returns "Application-defined or object-defined error"。

我搜索了有关此错误的其他问题,其中 none 似乎适用于同一类型的问题。这是代码:

Private Sub ComboBox1_Change()

'first get the current row rule id
lastRow = FindLastRow("Rows Rules")

Dim id, i, k As Integer

For i = 2 To lastRow
    If Sheets("Rows Rules").Cells(i, 2) = ComboBox1.Text Then
        id = Sheets("Rows Rules").Cells(i, 1)
    End If
Next


'get ISINs for id
With Sheets("Row ISINs")

    'loop  for every isin
    For i = 0 To ListBox1.ListCount
        isin = ListBox1.List(i, 0)
        'check if exists
        lastRow = FindLastRow("Row ISINs")
        'loop to compare  with every record
        For k = 1 To lastRow
            If .Cells(k, 0) = id Then 'error happens here--------------
                If .Cells(k, 1) = isin Then
                    'mark those as selected
                    ListBox1.Selected(i) = True
                End If
            End If
        Next

    Next

End With
End Sub

第二次循环后出现:

For k = 1 To lastRow

我每次访问单元格都会returns出错

我也试过使用 MsgBox 检查:

MsgBox .Cells(0, 1)

它仍然returns同样的错误。

我不知道它是否与循环有关,或者我遗漏了什么。任何帮助将不胜感激,谢谢

excel 中没有 行 0 列 0。如果你想要第一行或第一列,应该从 1 开始,如下所示:

.Cells(1,1)

原因是 cells(k, j) 使用 k = 行和 j = 列指定单元格

你不能像第一个值为 0 的数组那样处理它。

k 和 j 必须 >= 1。