vba vlookup另一张表

时间:2017-06-20 18:34:11

标签: excel vba excel-vba

任何人都知道如何修复这些代码?运行时错误'1004':应用程序定义的错误或对象定义的错误

我改变了我的代码如下..仍然无法正常工作..

 Sub Testing()

    Dim DRACCT As String
    Dim DR24 As Variant
    Dim sh1, sh2 As Workbook
    Dim Lastrow As Long
    Dim i As Long

    Lastrow = Range("A10000000").End(xlUp).Row

    Set sh1 = ThisWorkbook.Worksheets("Sheet1")
    Set sh2 = ThisWorkbook.Worksheets("Sheet2")

    'On Error Resume Next

    For i = 2 To Lastrow

        DRACCT = sh1.Cells(i, 66).Value
        DR24 = Application.VLookup(DRACCT, sh2.Range("A:B"), 2, False)

        If Not IsError(DR24) Then
        sh1.Cells(i, 68).Value = DR24

        Else ' do nothing
        End If

    Next i


    End Sub

1 个答案:

答案 0 :(得分:0)

两个问题

  1. 只有1045876行不是10000000这是您当前的错误。

  2. 您将获得的下一个错误是因为您尝试将工作表设置为工作簿变量

  3. 守则

    Sub Testing()
    
    Dim DRACCT As String
    Dim DR24 As Variant
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim Lastrow As Long
    Dim i As Long
    
    Lastrow = Range("A1046876").End(xlUp).Row
    
    Set sh1 = ThisWorkbook.Worksheets("Sheet1")
    Set sh2 = ThisWorkbook.Worksheets("Sheet2")
    
    'On Error Resume Next
    
    For i = 2 To Lastrow
    
        DRACCT = sh1.Cells(i, 66).Value
        DR24 = Application.VLookup(DRACCT, sh2.Range("A:B"), 2, False)
    
        If Not IsError(DR24) Then
        sh1.Cells(i, 68).Value = DR24
    
        Else ' do nothing
        End If
    
    Next i
    
    
    End Sub