2016-07-28 79 views
0

好吧,所以我試圖創建一個基本上像vlookup一樣的宏,並在A列中搜索工作表2中列A中的相同值的單元格,然後複製所有信息在那一行到工作表1的第一個打開的列。VBA。找到對象變量沒有設置錯誤

基本上我不知道我在做什麼,但它有95%的功能。我唯一的問題是它遇到工作表1列A中的值,它無法在工作表2列A中找到。我能做些什麼來跳到下一個值?

我如果...那麼...其他人絕望地嘗試跳過那個值,顯然這是行不通的。

Sub ProLookUp() 

Dim ColALastRow As Long 
Dim ColALastRow2 As Long 


ColALastRow = Worksheets(1).Columns("A:A").End(xlDown).Row 
MsgBox ColALastRow 

ColALastRow2 = Worksheets(2).Columns("A:A").End(xlDown).Row 
MsgBox ColALastRow2 

Dim i As Long 
Dim Pro As String 
Dim Pro2 As Long 

For i = 1 To ColALastRow 
Pro = Worksheets(1).Cells(i, 1).Value 

'With Worksheets(2).Range("A1:A" & ColALastRow2)' 'ignore this part' 

With Worksheets(2).Range("A1:A10000") 

'the below is where my issue is, once it finds a value in column A that it 
'cannot match in sheet 2 it returns the error 
'Object variable or With block variable not set 

If Pro = .Find(Pro, LookIn:=xlValues).Value Then 
    Pro2 = .Find(Pro, LookIn:=xlValues).Row 
Else 
    i = i + 1 
End If 


    Dim LastColA As Integer 
    Dim CopyRange As Range 
    Dim a As Range 
    Dim b As Range 

     With Worksheets(2) 
      LastColA = .Cells(Pro2, .Columns.Count).End(xlToLeft).Column 
      Set a = .Cells(Pro2, 2) 
      Set b = .Cells(Pro2, LastColA) 
      Set CopyRange = Range(a, b) 
     End With 

    Dim PasteRange As Range 
    Dim LastColumnB As Integer 
     With Worksheets(1) 
      LastColumnB = .Cells(i, .Columns.Count).End(xlToLeft).Column 
      LastColumnB = LastColumnB + 1 
      Set PasteRange = .Cells(i, LastColumnB) 
      MsgBox PasteRange.Address 

     End With 

Worksheets(2).Select 
    CopyRange.Select 
    Selection.Copy 
Worksheets(1).Select 
    PasteRange.Activate 
    ActiveCell.PasteSpecial 

End With  
Next i 
End Sub 

回答

1

我重寫了一些其他的代碼。

你的if語句與i = i + 1不符合你的想法。

我將查找的結果加載到範圍變量中。如果找不到任何結果範圍變量將是Nothing。由於您無法調用Nothing上的任何方法,因此會引發錯誤91。爲了解決它,請在If塊中測試Nothing,並避免該錯誤。

我們測試以確保範圍變量Is Not Nothing,然後做的東西。如果找到Nothing,則跳過該代碼並直接進入Next i

通過嘗試使用if來添加1,我不會觸發For循環的下一次迭代。代碼仍然會嘗試運行,然後迭代,從而實際跳過行。

沒有必要激活工作表和範圍只是爲了複製和粘貼。

Sub ProLookUp() 

Dim ColALastRow As Long 
Dim ColALastRow2 As Long 


ColALastRow = Worksheets(1).Columns("A:A").End(xlDown).Row 
MsgBox ColALastRow 

ColALastRow2 = Worksheets(2).Columns("A:A").End(xlDown).Row 
MsgBox ColALastRow2 

Dim i As Long 
Dim Pro As String 
Dim fnd As Range 
Dim Pro2 As Long 

For i = 1 To ColALastRow 
    Pro = Worksheets(1).Cells(i, 1).Value 

    'With Worksheets(2).Range("A1:A" & ColALastRow2)' 'ignore this part' 

    With Worksheets(2).Range("A1:A10000") 

     'the below is where my issue is, once it finds a value in column A that it 
     'cannot match in sheet 2 it returns the error 
     'Object variable or With block variable not set 
     Set fnd = .Find(Pro, LookIn:=xlValues) 
    End With 
    If Not fnd Is Nothing Then 
     Pro2 = fnd.Row 
     Dim LastColA As Integer 
     Dim CopyRange As Range 
     Dim a As Range 
     Dim b As Range 

     With Worksheets(2) 
      LastColA = .Cells(Pro2, .Columns.Count).End(xlToLeft).Column 
      Set a = .Cells(Pro2, 2) 
      Set b = .Cells(Pro2, LastColA) 
      Set CopyRange = Range(a, b) 
     End With 

     Dim PasteRange As Range 
     Dim LastColumnB As Integer 

     With Worksheets(1) 
      LastColumnB = .Cells(i, .Columns.Count).End(xlToLeft).Column 
      LastColumnB = LastColumnB + 1 
      Set PasteRange = .Cells(i, LastColumnB) 
      MsgBox PasteRange.Address 
     End With 


     CopyRange.Copy PasteRange 

    End If 


Next i 
End Sub 
+0

哇,非常感謝。它完美地工作。 –

+0

@PeteUlrich請在答案處點擊複選標記並標記爲正確。只有提出問題的人才能做到這一點。 –

+0

已更新。還有一個問題,我注意到,如果工作表2上的列A沒有大小來顯示它不能匹配工作表1上的數字的整個數字,任何想法爲什麼?我應該在開始之前添加一行來調整該列的大小? –

相關問題