EXCEL VBA-VLOOKUP中未设置对象变量

EXCEL VBA-VLOOKUP中未设置对象变量,vba,Vba,我不熟悉使用VBA for excel。我正在尝试对工作表1中的Col1进行Vlookup,并在工作表2的Col1中使用相应的值来返回工作表2的col2中的值。 我得到一个运行时错误: 未设置对象变量 VBA Excel VLookup 调整常量部分中的值以满足您的需要 代码 请将代码显示为代码,以便于阅读。谢谢您不能将ws2位设置为WS1的两倍WS1您将在Sheet2上的单元格F1中使用的确切Excel VLOOKUP公式是什么?该代码在不清除结果列的情况下工作,并且不会返回第一个value@

我不熟悉使用VBA for excel。我正在尝试对工作表1中的Col1进行Vlookup,并在工作表2的Col1中使用相应的值来返回工作表2的col2中的值。 我得到一个运行时错误:

未设置对象变量

VBA Excel VLookup 调整常量部分中的值以满足您的需要

代码
请将代码显示为代码,以便于阅读。谢谢您不能将ws2位设置为WS1的两倍WS1您将在Sheet2上的单元格F1中使用的确切Excel VLOOKUP公式是什么?该代码在不清除结果列的情况下工作,并且不会返回第一个value@Grettonamn当前位置你的问题很不清楚。你只需要仔细调整常数。使用这些常量,它循环遍历Sh1 ColA上的每个值,在Sh2 ColA中搜索匹配项,并从Sh1 ColF中的Sh2 ColB返回值。Sh1可乐从第1行开始,但Sh2可乐从第2行开始。我已经有10年没有写VB了,至少可以说我很生锈。在你的帮助下,我现在开始工作了。现在我需要做的是从工作表上的另一列中选取一列one@Grettonamn:还是很不清楚。但是我认为只交换常量部分中的工作表名称,并调整列和行。在您的帮助下进行排序
Private Sub CommandButton1_Click()
'
'Dims and sets
Dim intRows As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng As Range
Dim srchres As Variant
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws1 = ThisWorkbook.Sheets("Sheet2")

'
'Determine number of rows in col A containing data
intRows = Application.CountA(Range("B:B"))
MsgBox intRows
For i = 1 To intRows
 srchres = Application.WorksheetFunction.VLookup(ws2.Range("B2"), ws1.Range("A1:1"), 1, False)
 Cells(i, 6).Value = srchres
Next i
End Sub
Option Explicit

Private Sub CommandButton1_Click()

    ' First Sheet
    Const cSheet1 As Variant = "Sheet1"   ' First Sheet Name/Index
    Const cFirstR1 As Integer = 1         ' First Sheet First Row
    Const cRead As Variant = "A"          ' Read Column Letter/Number
    Const cRes As Variant = "F"           ' Result Column Letter/Number

    ' Second Sheet
    Const cSheet2 As Variant = "Sheet2"   ' Second Sheet Name/Index
    Const cFirstR2 As Integer = 2         ' Second Sheet First Row
    Const cFirstC2 As Variant = "A"       ' First Column Letter/Number
    Const cLastC2 As Variant = "B"        ' Last Column Letter/Number
    Const cLookup As Integer = 2          ' Lookup Column

    Dim ws1 As Worksheet  ' First Sheet
    Dim ws2 As Worksheet  ' Second Sheet
    Dim rng As Range      ' Lookup Range
    Dim LastR1 As Long    ' First Sheet Last Row
    Dim LastR2 As Long    ' Second Sheet Last Row
    Dim i As Long         ' First Sheet Row Counter

    With ThisWorkbook
        Set ws1 = .Sheets(cSheet1)
        Set ws2 = .Sheets(cSheet2)
    End With

    With ws1
        ' Calculate last row of First Sheet.
        LastR1 = .Cells(.Rows.Count, cRead).End(xlUp).Row
        ' Clear contents of Result Range (column).
        .Range(.Cells(cFirstR1, cRes), .Cells(LastR1, cRes)).ClearContents
    End With

    With ws2

        ' Calculate last row of Second Sheet.
        LastR2 = .Cells(.Rows.Count, cFirstC2).End(xlUp).Row
        ' Calculate Lookup Range.
        Set rng = .Range(.Cells(cFirstR2, cFirstC2), .Cells(LastR2, cLastC2))

        On Error Resume Next ' Ignore error if match not found.

        ' Loop through rows (cells) of First Sheet.
        For i = cFirstR1 To LastR1
            ' Write result to current cell in Result Column.
            ws1.Cells(i, cRes) = Application.WorksheetFunction.VLookup( _
                    ws1.Cells(i, cRead), rng, cLookup, False)
        Next

        On Error GoTo 0 ' Turn off ignore error.

    End With

    ' Release object variables
    Set rng = Nothing
    Set ws2 = Nothing
    Set ws1 = Nothing

End Sub