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
哇,非常感謝。它完美地工作。 –
@PeteUlrich請在答案處點擊複選標記並標記爲正確。只有提出問題的人才能做到這一點。 –
已更新。還有一個問題,我注意到,如果工作表2上的列A沒有大小來顯示它不能匹配工作表1上的數字的整個數字,任何想法爲什麼?我應該在開始之前添加一行來調整該列的大小? –