试图循环另一个工作簿的表格

时间:2015-03-10 14:36:17

标签: excel vba excel-vba

我有两个工作簿,我需要比较我编写脚本的ID和我需要从脚本访问的其他ID。

我遍历表格中的所有数字,并在" tmpFisNo"中记录每个ID。之后,我创建一个循环来迭代其他工作簿的行并进行比较。

然而,它说" Mismatch Error"在For j = ws.Cells(9, 9).Value To ws.Cells(10, 9).Value

Dim i As Integer
Dim j As Integer
Dim tmpFisNo As String
Dim tmpFisNo2 As String

Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim wbPath As String
Dim ws As Worksheet

Sub Dogrula_Click()
    wbPath = Cells(8, 9).Value
    Set wbThis = ActiveWorkbook
    Set wbTarget = Workbooks.Open("2015_OCAK_MUTABAKAT_RAPORU")

    Set ws = wbTarget.Sheets(2)


    For i = Cells(5, 9).Value To Cells(6, 9).Value

        tmpFisNo = Cells(i, 2).Text
        For j = ws.Cells(9, 9).Value To ws.Cells(10, 9).Value
            tmpFisNo2 = ws.Cells(j, 4).Text
            If tmpFisNo = tmpFisNo2 Then
                Cells(i, 7).Value = 1
            End If
        Next j
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

尝试

Dim i As Long
Dim j As Long
Dim tmpFisNo As String
Dim tmpFisNo2 As String

Dim wbThis As Workbook
Dim wbTarget As Workbook
Dim wbPath As String
Dim ws As Worksheet

Sub Dogrula_Click()
    wbPath = Cells(8, 9).Value
    Set wbThis = ActiveWorkbook
    Set wbTarget = Workbooks.Open("2015_OCAK_MUTABAKAT_RAPORU")

    Set ws = wbTarget.Sheets(2)


    For i = Cells(5, 9).Value To Cells(6, 9).Value

        tmpFisNo = Cells(i, 2).Text
        dim a as Long, b as Long
        a = CLng(ws.Cells(9, 9).Value)
        b = CLng(ws.Cells(10, 9).Value)
        For j = a To b
            tmpFisNo2 = ws.Cells(j, 4).Text
            If tmpFisNo = tmpFisNo2 Then
                Cells(i, 7).Value = 1
            End If
        Next j
    Next i
End Sub

如果它在CLngs上给你一个错误,那么因为你在单元格(9,9)或(10,9)的电子表格中有一些不是整数的值

我还建议使用Long而不是Integer - 您无法使用Integer访问工作表中的所有单元格(即使是旧版本的Excel)