[英]Excel 2007 VBA VLookup function
我已經整理了代碼,以在excel中使用vba使用vlookup。 但是,我收到一條錯誤消息,指出“無法獲取WorkSheet Function類的VLOOKUP屬性”。 “ CustomerNumberList”是兩列范圍。 我相信我的問題是使用VLookup函數。 我正在使用“偏移”作為我希望VLookup在表中開始查找的位置的指示器,在這種情況下,請查找“ cust_num”(y + 1,0)。 如何指示我希望VLookup在哪一列輸入公司名稱? 目前,我的模塊具有Select Case函數,該函數可用於獲取每個單元格中的公司名稱,但是我要處理數千行,因此可能需要20分鍾才能運行。 我希望這種方式更快。 有人可以協助嗎?
Excel工作表樣本:VLookup范圍工作表
"CustomerNumberList"
Cust_Num Company Name
10001 CompanyX
10002 CompanyX
10003 CompanyX
10004 CompanyX
10005 CompanyX
10006 CompanyX
10007 CompanyX
10008 CompanyX
10009 CompanyX
10010 CompanyY
10011 CompanyY
10012 CompanyY
10013 CompanyY
10014 CompanyY
10015 CompanyY
10016 CompanyY
10017 CompanyY
資料表
Sheet1
OrderID Cust_Num Company Name Customer Name ShipLocation Cost Price
1 10001 VLookupHere Rand Dallas $1.00 $2.00
2 10002 " Rand Chicago $2.00 $3.00
3 10003 " Rand Florida $1.00 $2.00
4 10004 " Wel California $1.33 $2.33
5 10005 Wel Dallas $1.33 $2.33
6 10006 Wel Chicago $1.33 $2.33
7 10007 Sead Florida $1.33 $2.33
8 10008 Sead California $1.33 $2.33
9 10009 Sead Dallas $1.33 $2.33
10 10010 Sead Chicago $1.33 $2.33
11 10011 Sead Florida $1.33 $2.33
12 10012 Sead California $1.33 $2.33
13 10013 Campe Dallas $1.33 $2.33
14 10014 Campe Chicago $1.33 $2.33
15 10015 Campe Florida $1.33 $2.33
16 10016 Campe California $1.33 $2.33
17 10017 Campe Dallas $1.33 $2.33
碼:
Dim Nu As Range
Dim cmpny As Range
Dim v As Integer
Dim y As Integer
Dim ws As Worksheet
Dim Offset As Range
Dim result As String
'
'
'
'Insert company name into each row based on cust_num
Set Nu = Sheets("CustomerNumberList").Range("A1") 'set Nu = cust_name region
Set cmpny = Sheets("CustomerNumberList").Range("A1").CurrentRegion 'set cmpny = cell range
For Each ws In Sheets
If ws.Name Like "*Sheet*" Then
v = ws.Range("A" & Rows.Count).End(xlUp).Row 'set v = number of rows
Set Nm = ws.Rows(1).Find("cust_num", LookAt:=xlPart)
For y = 0 To v
Set Offset = Nm.Offset(1 + y, 0)
result = Application.WorksheetFunction.VLookup(Nu.Value, cmpny, 2, Offset, False) **'ERROR HERE**
Next
End If
Next
End Sub
Sub Update()
Dim rngNums As Range
Dim cmpny As Range
Dim v As Long
Dim ws As Worksheet
Dim result As Variant
Dim c As Range, nm As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error GoTo haveError
'lookup range
Set cmpny = Sheets("CustomerNumberList").Range("A1").CurrentRegion
For Each ws In Sheets
If ws.Name Like "*Sheet*" Then
v = ws.Range("A" & Rows.Count).End(xlUp).Row 'set v = number of rows
Set nm = ws.Rows(1).Find("cust_num", LookAt:=xlPart)
If Not nm Is Nothing Then
Set rngNums = ws.Range(nm.Offset(1, 0), ws.Cells(v, nm.Column))
For Each c In rngNums.Cells
If c.Value <> "" Then
'Next line drops the WorksheetFunction to avoid raising error
' if the value is not found - instead, test the return value
result = Application.VLookup(c.Value, cmpny, 2, False)
c.Offset(0, 1).Value = IIf(IsError(result), "???", result)
End If
Next c
Else
Debug.Print "No 'cust_num' in row 1 on sheet '" _
& ws.Name & "'"
End If
End If
Next ws
haveError:
If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.