Excel 如何使用VBA在vlookup中获取多个列值?
我有一个excel文件,如下所示: 现在,我使用name列中的值在单元格“L2”中创建了一个列表,并添加了一个按钮“Find” 我试图做的是,当我在“L2”中选择一个值后单击按钮时,它将从单元格“I6:L6”中的表“A4:E12”中给我相应的名称、ID、DOB、电子邮件的值,如果“I6:L6”有值,那么每次我按下按钮时它都会移动到“I7:L7”等等 以下是我完成的代码:Excel 如何使用VBA在vlookup中获取多个列值?,excel,vba,Excel,Vba,我有一个excel文件,如下所示: 现在,我使用name列中的值在单元格“L2”中创建了一个列表,并添加了一个按钮“Find” 我试图做的是,当我在“L2”中选择一个值后单击按钮时,它将从单元格“I6:L6”中的表“A4:E12”中给我相应的名称、ID、DOB、电子邮件的值,如果“I6:L6”有值,那么每次我按下按钮时它都会移动到“I7:L7”等等 以下是我完成的代码: Sub getValues() Range("I6").Select ActiveCell.Formula = "=VLOO
Sub getValues()
Range("I6").Select
ActiveCell.Formula = "=VLOOKUP($L$2,$A$5:$E$12,{1,2,4,5},0)"
Range("I6").AutoFill Destination:=Range("I6:L6"), Type:=xlFillDefault
Range("I6:L6").Select
Range("A3").Select
End Sub
此代码的问题在于它给出的值如下所示:
预期结果为:
此外,如果“I6:L6”被占用,它应该自动将值粘贴到“I7:L7”,以此类推
有人能告诉我怎么做吗?这是一个很长的答案(不是最有效的),但我这样写是为了一步一步地说明它
查看代码中的注释,并根据需要进行调整
Public Sub GetDataByPersonName()
Dim sourceSheet As Worksheet
Dim sourceDataRange As Range
Dim rowOffset As Long
Dim sourceSheetName As String
Dim sourcePersonName As String
Dim sourceRangeAddress As String
Dim sourceLookupTableRange As String
Dim sourceMatchedRow As Variant
' Results
Dim resultsInitialCellAddress As String
Dim resultName As Variant
Dim resultID As Variant
Dim resultCountry As Variant
Dim resultDOB As Variant
Dim resultEmail As Variant
' Adjust to your needs
sourceSheetName = "Sheet1" ' Sheet name where data is located
sourceRangeAddress = "L2" ' Cell address of enter name
sourceLookupTableRange = "A4:E12" ' Range address where lookup data is located
resultsInitialCellAddress = "I4" ' Cell address of headers destination where results are going to be added
' Set references to sheet and lookup range
Set sourceSheet = ThisWorkbook.Worksheets(sourceSheetName)
Set sourceDataRange = sourceSheet.Range(sourceLookupTableRange)
' Get the current name to be looked up
sourcePersonName = sourceSheet.Range(sourceRangeAddress).Value
' Lookup the results (see columns(1) to check the column where name is located
sourceMatchedRow = Application.Match(sourcePersonName, sourceDataRange.Columns(1), 0)
If Not IsError(sourceMatchedRow) Then
' Find the last empty row below results header
If sourceSheet.Range(resultsInitialCellAddress).Offset(1, 0).Value = vbNullString Then
rowOffset = 1
Else
rowOffset = sourceSheet.Range(resultsInitialCellAddress).End(xlDown).Row - sourceSheet.Range(resultsInitialCellAddress).Row + 1
End If
' Name (see name column is 1 at the end for line)
resultName = Application.WorksheetFunction.Index(sourceDataRange, sourceMatchedRow, 1)
resultID = Application.WorksheetFunction.Index(sourceDataRange, sourceMatchedRow, 2)
resultCountry = Application.WorksheetFunction.Index(sourceDataRange, sourceMatchedRow, 3)
resultDOB = Application.WorksheetFunction.Index(sourceDataRange, sourceMatchedRow, 4)
resultEmail = Application.WorksheetFunction.Index(sourceDataRange, sourceMatchedRow, 5)
' Dump results into worksheet
sourceSheet.Range(resultsInitialCellAddress).Offset(rowOffset, 0).Value = resultName
sourceSheet.Range(resultsInitialCellAddress).Offset(rowOffset, 1).Value = resultID
sourceSheet.Range(resultsInitialCellAddress).Offset(rowOffset, 2).Value = resultCountry
sourceSheet.Range(resultsInitialCellAddress).Offset(rowOffset, 3).Value = resultDOB
sourceSheet.Range(resultsInitialCellAddress).Offset(rowOffset, 4).Value = resultEmail
End If
End Sub
记住,如果答案对你有帮助,就要标记它,以帮助他人。好的,我认为你应该自己尝试这样做,并在宏中记录你的步骤。这样你会学到很多东西。请参阅下面的链接 在单击步骤之前打开宏录制器。然后,您将拥有所需的所有代码