Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 2007 VBA VLookup函数_Excel_Vba_Vlookup - Fatal编程技术网

Excel 2007 VBA VLookup函数

Excel 2007 VBA VLookup函数,excel,vba,vlookup,Excel,Vba,Vlookup,我已经编写了在excel中使用vba使用vlookup的代码。但是,我收到一个错误,指示“无法获取工作表函数类的VLOOKUP属性”。“CustomerNumber列表”是一个两列范围。我认为我的问题是使用VLookup函数。在本例中,我使用“偏移量”作为我希望VLookup在工作表中开始查找的位置的指示器。查找“cust_num”(y+1,0)。我如何指出我希望VLookup输入公司名称的列?目前,我的模块有一个Select Case函数,用于获取每个单元格中的公司名称,但我要处理数千行代码,

我已经编写了在excel中使用vba使用vlookup的代码。但是,我收到一个错误,指示“无法获取工作表函数类的VLOOKUP属性”。“CustomerNumber列表”是一个两列范围。我认为我的问题是使用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 rngNums
Dim cmpny As范围
暗v一样长
将ws设置为工作表
作为变量的模糊结果
尺寸c作为范围,纳米作为范围
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
关于错误转到haveError
'查找范围
设置cmpny=Sheets(“CustomerNumber列表”).Range(“A1”).CurrentRegion
对于每个ws-In表单
如果ws.Name像“*Sheet*”那么
v=ws.Range(“A”&Rows.Count).End(xlUp).Row'设置v=行数
设置nm=ws.Rows(1).Find(“cust_num”,LookAt:=xlPart)
如果不是的话,nm什么都不是
设置rngNums=ws.范围(nm.偏移量(1,0),ws.单元格(v,nm.列))
对于rngNums.单元格中的每个c
如果c.值为“”,则
'下一行删除工作表函数以避免引发错误
'如果找不到该值,则测试返回值
结果=Application.VLookup(c.Value,cmpny,2,False)
c、 偏移量(0,1)。值=IIf(IsError(结果),“??”,结果)
如果结束
下一个c
其他的
调试。打印“工作表”第1行中的“无客户数量”_
&ws.Name&“'”
如果结束
如果结束
下一个ws
haveError:
如果错误号为0,则MsgBox错误说明,VBEQUOTE
Application.ScreenUpdating=True
Application.Calculation=xlCalculationAutomatic
端接头
子更新()
作为范围的Dim rngNums
Dim cmpny As范围
暗v一样长
将ws设置为工作表
作为变量的模糊结果
尺寸c作为范围,纳米作为范围
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
关于错误转到haveError
'查找范围
设置cmpny=Sheets(“CustomerNumber列表”).Range(“A1”).CurrentRegion
对于每个ws-In表单
如果ws.Name像“*Sheet*”那么
v=ws.Range(“A”&Rows.Count).End(xlUp).Row'设置v=行数
设置nm=ws.Rows(1).Find(“cust_num”,LookAt:=xlPart)
如果不是的话,nm什么都不是
设置rngNums=ws.范围(nm.偏移量(1,0),ws.单元格(v,nm.列))
对于rngNums.单元格中的每个c
如果c.值为“”,则
'下一行删除工作表函数以避免引发错误
'如果找不到该值,则测试返回值
结果=Application.VLookup(c.Value,cmpny,2,False)
c、 偏移量(0,1)。值=IIf(IsError(结果),“??”,结果)
如果结束
下一个c
其他的
调试。打印“工作表”第1行中的“无客户数量”_
&ws.Name&“'”
如果结束
如果结束
下一个ws
haveError:
如果错误号为0,则MsgBox错误说明,VBEQUOTE
Application.ScreenUpdating=True
Application.Calculation=xlCalculationAutomatic
端接头

调用vlookup时参数太多。可能2是额外的…@TimWilliams,我的初始代码排除了2,我得到了相同的错误。我的想法是“偏移量”没有遵循vlookup函数过程,因为它指的是信息表而不是vlookup范围表。第三个参数需要是一个数字,那么偏移量的值是多少?调用vlookup时参数太多。可能2是额外的…@TimWilliams,我的初始代码排除了2,我得到了相同的错误。我的想法是“偏移量”没有遵循vlookup函数过程,因为它指的是信息表而不是vlookup范围表。第三个参数需要是一个数字,那么偏移量的值是多少?太棒了。我唯一需要做的更改是:Application.VLookup(CLng(c.Value),cmpny,2,False)。结果是获得错误2042,当添加CLng时,它修复了该问题。谢谢明亮的我唯一需要做的更改是:Application.VLookup(CLng(c.Value),cmpny,2,False)。结果是获得错误2042,当添加CLng时,它修复了该问题。谢谢
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