[英]VBA on excel - using the result of a vlookup as criteria for a match
[英]Excel-VBA to return using LEFT of vlookup result
我有一個基於簡單查找的程序,它工作並返回正確的值。 我想知道是否有一種方法可以合並= LEFT函數,使其返回vlookup結果的前兩個字符。
ub Store_Lookup_left_test()
Dim rw As Long, x As Range
Dim extwbk As Workbook, twb As Workbook
Set twb = ThisWorkbook
Set extwbk = Workbooks.Open("H:\****\****\Master Store Info.xlsm")
Set x = extwbk.Worksheets("Info Sheet").Range("A1:C999999")
With twb.Sheets("Sheet1")
For rw = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
.Cells(rw, 10) = Application.VLookup(.Cells(rw, 1).Value2, x, 3, False)
Next rw
End With
所以通過類比=Left(vlookup(a1,A1:C1,3,0),2)
,我可以做類似.cells(rw, 10) = application.left(application.vlookup(.cells(rw, 1).Value2, x, 3, False),2)
?
目前vlookup結果是
英國光學或愛爾蘭光學
我想要它
英國,Ir
您可以直接在VBA中使用Left
函數,但是您需要檢查vlookup
返回的內容,因為如果沒有匹配則會返回Left
無法使用的錯誤。
' Get vlookup result as variant so no error occurs if not string
Dim lookupresult As Variant
lookupresult = Application.VLookup(.Cells(rw, 1).Value2, x, 3, False)
' Check if vlookup returned an error (no match found)
If IsError(lookupresult) Then
MsgBox "Error with vlookup, no match found"
Else
' Get Left of string, up to 2 characters.
.Cells(rw, 10) = Left(lookupresult, 2)
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.