Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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 VBA查找-无法获取工作表函数类的Vlookup属性_Excel_Vba - Fatal编程技术网

Excel VBA查找-无法获取工作表函数类的Vlookup属性

Excel VBA查找-无法获取工作表函数类的Vlookup属性,excel,vba,Excel,Vba,我知道这里已经讨论过一个类似的问题: 但似乎并不能解决我的问题。这是我在VBA上的第一篇文章,如果有任何问题需要澄清,请告诉我 我正在尝试建立一个发票表,它基于 项目编号(在本例中为1) 所有项目数据的数据集 每个项目活动显示为一个单独的行项目,并由一个唯一标识符标识,该标识符由项目编号和行项目编号组成(因此,对于项目一中的第三个行项目,它将是“1/3”)。标识符的格式为字符串。所有输入数据都在名为“输入”的工作表上 第二张是名为“发票”的实际发票单。其思想是根据每个项目(仍在处理此部分)的

我知道这里已经讨论过一个类似的问题:

但似乎并不能解决我的问题。这是我在VBA上的第一篇文章,如果有任何问题需要澄清,请告诉我

我正在尝试建立一个发票表,它基于

  • 项目编号(在本例中为1)
  • 所有项目数据的数据集
每个项目活动显示为一个单独的行项目,并由一个唯一标识符标识,该标识符由项目编号和行项目编号组成(因此,对于项目一中的第三个行项目,它将是“1/3”)。标识符的格式为字符串。所有输入数据都在名为“输入”的工作表上

第二张是名为“发票”的实际发票单。其思想是根据每个项目(仍在处理此部分)的行项目数自动获得正确的空行数,并自动填写表单。最后一部分是在第80行尝试运行
vlookup
时产生错误的部分:错误消息为

无法获取WorksheetFunction类的Vlookup属性

我想知道这是否是由查找值(标识符)引起的,因为我没有正确创建它?我已经研究了这里讨论的解决方案,但找不到答案:(

提前感谢您的帮助!代码如下:

Option Explicit

Sub Count_Line_Items()

'Counts the number of line items of a consulting project to determine the space needed on the invoice form

     Dim Cell As Range
     Dim PosCnt As Integer
     Dim ServCnt As Integer
     Dim ExpCnt As Integer

     PosCnt = 0
     ServCnt = 0
     ExpCnt = 0

    'Counting all project positions for the chosen project number
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect") Then
           PosCnt = PosCnt + 1
        End If
    Next Cell

    MsgBox "Total number of line items: " & PosCnt

    'Counting all positions of that project that are consulting services
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
        ServCnt = ServCnt + 1
       End If
    Next Cell

    MsgBox "Total number of consulting services: " & ServCnt

    'Calculating number of expense items
    ExpCnt = PosCnt - ServCnt

    MsgBox "Total number of expenses: " & ExpCnt

End Sub

Sub Count_Total_Rows()

    Dim Current_RowCnt As Integer
    Dim Target_RowCnt As Integer
    Dim Diff_Rows As Integer

    Target_RowCnt = 62

    'Counting the rows in the print area and calculating difference to target
    Range("Print_Area").Select
    Current_RowCnt = Selection.Rows.Count
    Diff_Rows = Target_RowCnt - Current_RowCnt
        If Diff_Rows > 0 Then
            MsgBox "We need to add " & Diff_Rows & " rows!"
        ElseIf Diff_Rows < 0 Then
            MsgBox "We need to delete " & -Diff_Rows & " rows!"
        Else
            MsgBox "Nothing needs to be done; all good!"
        End If
End Sub

Sub Write_Services()
'Looks up services on data sheet and writes them to invoice sheet
    Dim Cnt As Integer
    Dim ServCnt As Integer
    Dim PosIdent As String
    Dim Data As Range

    Cnt = 0
    'Building position identifier
    PosIdent = "IdSelect" & "/" & Cnt + 1
    Sheets("Input").Select
    ActiveSheet.Range("D26:AD151").Select
    Set Data = Selection

    Sheets("Invoice").Select
    ActiveSheet.Range("Service_Title").Offset(1, 0).Activate
    'There is still an issue with the counter (line number won't increment by 1 if cnt range is incremented by 1
    For Cnt = 0 To ServCnt + 1
        ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
        ActiveCell.Offset(1, 0).Activate
        Cnt = Cnt + 1
    Next Cnt
End Sub
但是错误消息仍然是一样的。感谢代码的改进(它确实解决了PosIdent没有被循环更新的问题)-还有其他想法吗

第2次更新:

我现在已经根据我收到的对far有用的答案/评论更新了代码(非常感谢!),现在它创建了一条新的错误消息(不确定是否现在解决了旧的问题,因为新的问题出现在第59行代码的前面)。新的错误是“1004:对象'\u GLobal'的方法'Range'失败。我真的不知道是什么触发了它,因为我刚刚创建了一个名为
Main
的新子对象,它调用所有其他子对象,然后将变量
ServCnt
作为参数传递给最后一个子对象。有人能帮忙吗?”

新代码如下:

Option Explicit

Sub Count_Line_Items()

'Counts the number of line items of a consulting project to determine the space needed on the invoice form

     Dim Cell As Range
     Dim PosCnt As Integer
     Dim ServCnt As Integer
     Dim ExpCnt As Integer

     PosCnt = 0
     ServCnt = 0
     ExpCnt = 0

    'Counting all project positions for the chosen project number
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect") Then
           PosCnt = PosCnt + 1
        End If
    Next Cell

    MsgBox "Total number of line items: " & PosCnt

    'Counting all positions of that project that are consulting services
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
        ServCnt = ServCnt + 1
       End If
    Next Cell

    MsgBox "Total number of consulting services: " & ServCnt

    'Calculating number of expense items
    ExpCnt = PosCnt - ServCnt

    MsgBox "Total number of expenses: " & ExpCnt

End Sub

Sub Count_Total_Rows()

    Dim Current_RowCnt As Integer
    Dim Target_RowCnt As Integer
    Dim Diff_Rows As Integer

    Target_RowCnt = 62

    'Counting the rows in the print area and calculating difference to target
    Range("Print_Area").Select
    Current_RowCnt = Selection.Rows.Count
    Diff_Rows = Target_RowCnt - Current_RowCnt
        If Diff_Rows > 0 Then
            MsgBox "We need to add " & Diff_Rows & " rows!"
        ElseIf Diff_Rows < 0 Then
            MsgBox "We need to delete " & -Diff_Rows & " rows!"
        Else
            MsgBox "Nothing needs to be done; all good!"
        End If
End Sub

Sub Write_Services()
'Looks up services on data sheet and writes them to invoice sheet
    Dim Cnt As Integer
    Dim ServCnt As Integer
    Dim PosIdent As String
    Dim Data As Range

    Cnt = 0
    'Building position identifier
    PosIdent = "IdSelect" & "/" & Cnt + 1
    Sheets("Input").Select
    ActiveSheet.Range("D26:AD151").Select
    Set Data = Selection

    Sheets("Invoice").Select
    ActiveSheet.Range("Service_Title").Offset(1, 0).Activate
    'There is still an issue with the counter (line number won't increment by 1 if cnt range is incremented by 1
    For Cnt = 0 To ServCnt + 1
        ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
        ActiveCell.Offset(1, 0).Activate
        Cnt = Cnt + 1
    Next Cnt
End Sub

选项显式

副标题()

作为整数的Dim ServCnt

呼叫计数\u行\u项目 呼叫计数\u总计\u行 呼叫写入服务(ServCnt)

端接头

子计数\行\项()

'统计咨询项目的行项目数,以确定发票表单上所需的空间

 Dim Cell As Range
 Dim PosCnt As Integer
 Dim ServCnt As Integer
 Dim ExpCnt As Integer

 PosCnt = 0
 ServCnt = 0
 ExpCnt = 0

'Counting all project positions for the chosen project number
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect") Then
       PosCnt = PosCnt + 1
    End If
Next Cell

MsgBox "Total number of line items: " & PosCnt

'Counting all positions of that project that are consulting services
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
    ServCnt = ServCnt + 1
   End If
Next Cell

MsgBox "Total number of consulting services: " & ServCnt

'Calculating number of expense items
ExpCnt = PosCnt - ServCnt

MsgBox "Total number of expenses: " & ExpCnt
 Dim Cell As Range
 Dim PosCnt As Integer
 Dim ExpCnt As Integer

 PosCnt = 0
 ServCnt = 0
 ExpCnt = 0

'Counting all project positions for the chosen project number
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect") Then
       PosCnt = PosCnt + 1
    End If
Next Cell

MsgBox "Total number of line items: " & PosCnt

'Counting all positions of that project that are consulting services
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
    ServCnt = ServCnt + 1
   End If
Next Cell

MsgBox "Total number of consulting services: " & ServCnt

'Calculating number of expense items
ExpCnt = PosCnt - ServCnt

MsgBox "Total number of expenses: " & ExpCnt
端接头

子计数\u总计\u行()

端接头

更新3:

修复了上一个bug-请参阅下面的注释了解详细信息。工作代码如下:

Option Explicit

Sub Count_Line_Items()

'Counts the number of line items of a consulting project to determine the space needed on the invoice form

     Dim Cell As Range
     Dim PosCnt As Integer
     Dim ServCnt As Integer
     Dim ExpCnt As Integer

     PosCnt = 0
     ServCnt = 0
     ExpCnt = 0

    'Counting all project positions for the chosen project number
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect") Then
           PosCnt = PosCnt + 1
        End If
    Next Cell

    MsgBox "Total number of line items: " & PosCnt

    'Counting all positions of that project that are consulting services
    For Each Cell In Range("ProjectList")
       If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
        ServCnt = ServCnt + 1
       End If
    Next Cell

    MsgBox "Total number of consulting services: " & ServCnt

    'Calculating number of expense items
    ExpCnt = PosCnt - ServCnt

    MsgBox "Total number of expenses: " & ExpCnt

End Sub

Sub Count_Total_Rows()

    Dim Current_RowCnt As Integer
    Dim Target_RowCnt As Integer
    Dim Diff_Rows As Integer

    Target_RowCnt = 62

    'Counting the rows in the print area and calculating difference to target
    Range("Print_Area").Select
    Current_RowCnt = Selection.Rows.Count
    Diff_Rows = Target_RowCnt - Current_RowCnt
        If Diff_Rows > 0 Then
            MsgBox "We need to add " & Diff_Rows & " rows!"
        ElseIf Diff_Rows < 0 Then
            MsgBox "We need to delete " & -Diff_Rows & " rows!"
        Else
            MsgBox "Nothing needs to be done; all good!"
        End If
End Sub

Sub Write_Services()
'Looks up services on data sheet and writes them to invoice sheet
    Dim Cnt As Integer
    Dim ServCnt As Integer
    Dim PosIdent As String
    Dim Data As Range

    Cnt = 0
    'Building position identifier
    PosIdent = "IdSelect" & "/" & Cnt + 1
    Sheets("Input").Select
    ActiveSheet.Range("D26:AD151").Select
    Set Data = Selection

    Sheets("Invoice").Select
    ActiveSheet.Range("Service_Title").Offset(1, 0).Activate
    'There is still an issue with the counter (line number won't increment by 1 if cnt range is incremented by 1
    For Cnt = 0 To ServCnt + 1
        ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
        ActiveCell.Offset(1, 0).Activate
        Cnt = Cnt + 1
    Next Cnt
End Sub

选项显式
作为整数的公共服务

副标题()

呼叫计数\u行\u项目 呼叫计数\u总计\u行 呼叫写入服务(ServCnt)

端接头

子计数\行\项()

'统计咨询项目的行项目数,以确定发票表单上所需的空间

 Dim Cell As Range
 Dim PosCnt As Integer
 Dim ServCnt As Integer
 Dim ExpCnt As Integer

 PosCnt = 0
 ServCnt = 0
 ExpCnt = 0

'Counting all project positions for the chosen project number
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect") Then
       PosCnt = PosCnt + 1
    End If
Next Cell

MsgBox "Total number of line items: " & PosCnt

'Counting all positions of that project that are consulting services
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
    ServCnt = ServCnt + 1
   End If
Next Cell

MsgBox "Total number of consulting services: " & ServCnt

'Calculating number of expense items
ExpCnt = PosCnt - ServCnt

MsgBox "Total number of expenses: " & ExpCnt
 Dim Cell As Range
 Dim PosCnt As Integer
 Dim ExpCnt As Integer

 PosCnt = 0
 ServCnt = 0
 ExpCnt = 0

'Counting all project positions for the chosen project number
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect") Then
       PosCnt = PosCnt + 1
    End If
Next Cell

MsgBox "Total number of line items: " & PosCnt

'Counting all positions of that project that are consulting services
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
    ServCnt = ServCnt + 1
   End If
Next Cell

MsgBox "Total number of consulting services: " & ServCnt

'Calculating number of expense items
ExpCnt = PosCnt - ServCnt

MsgBox "Total number of expenses: " & ExpCnt
端接头

子计数\u总计\u行()

端接头

这可能是瞎猜,但我相信你的错误就在这里

PosIdent = "IdSelect" & "/" & Cnt + 1
那应该是

PosIdent = Range("IdSelect").Value & "/" & Cnt + 1
我还注意到,您只定义了一次,这就是为什么当您的范围发生变化时,它不会改变,我将把这段代码移到这里

For Cnt = 0 To ServCnt + 1
    PosIdent = Range("IdSelect").Value & "/" & Cnt + 1
    ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
    ActiveCell.Offset(1, 0).Activate
Next Cnt
希望有帮助

更新

试试这个:

Option Explicit
Public ServCnt As Integer
Sub Main()

Call Count_Line_Items
Call Count_Total_Rows
Call Write_Services

End Sub
Sub Count_Line_Items()

'Counts the number of line items of a consulting project to determine the space needed on the invoice form

 Dim Cell As Range
 Dim PosCnt As Integer
 Dim ExpCnt As Integer

 PosCnt = 0
 ServCnt = 0
 ExpCnt = 0

'Counting all project positions for the chosen project number
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect") Then
       PosCnt = PosCnt + 1
    End If
Next Cell

MsgBox "Total number of line items: " & PosCnt

'Counting all positions of that project that are consulting services
For Each Cell In Range("ProjectList")
   If Cell.Value = Range("IdSelect").Value And Cell.Offset(0, 3).Value = "Service" Then
    ServCnt = ServCnt + 1
   End If
Next Cell

MsgBox "Total number of consulting services: " & ServCnt

'Calculating number of expense items
ExpCnt = PosCnt - ServCnt

MsgBox "Total number of expenses: " & ExpCnt
End Sub

Sub Count_Total_Rows()

Dim Current_RowCnt As Integer
Dim Target_RowCnt As Integer
Dim Diff_Rows As Integer

Target_RowCnt = 62

'Counting the rows in the print area and calculating difference to target
Range("Print_Area").Select
Current_RowCnt = Selection.Rows.Count
Diff_Rows = Target_RowCnt - Current_RowCnt
    If Diff_Rows > 0 Then
        MsgBox "We need to add " & Diff_Rows & " rows!"
    ElseIf Diff_Rows < 0 Then
        MsgBox "We need to delete " & -Diff_Rows & " rows!"
    Else
        MsgBox "Nothing needs to be done; all good!"
    End If
End Sub

Sub Write_Services() 'Looks up services on data sheet and writes them to invoice sheet Dim Cnt As Integer Dim PosIdent As String Dim Data As Range

Cnt = 0
'Building position identifier

Sheets("Input").Select
ActiveSheet.Range("D26:AD151").Select
Set Data = Selection
PosIdent = Range("IdSelect").Value & "/" & Cnt + 1

Sheets("Invoice").Select
ActiveSheet.Range("Service_Title").Offset(1, 0).Activate
'There is still an issue with the counter (line number won't increment by 1 if cnt range is incremented by 1
For Cnt = 0 To ServCnt + 1
    ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
    ActiveCell.Offset(1, 0).Activate
    Cnt = Cnt + 1
Next Cnt
End Sub
选项显式
作为整数的公共服务
副标题()
呼叫计数\u行\u项目
呼叫计数\u总计\u行
呼叫写入服务
端接头
子计数\行\项()
'统计咨询项目的行项目数,以确定发票表单上所需的空间
暗淡单元格作为范围
作为整数的Dim PosCnt
Dim ExpCnt作为整数
PosCnt=0
ServCnt=0
ExpCnt=0
'计算所选项目编号的所有项目位置
对于范围内的每个单元格(“项目列表”)
如果Cell.Value=Range(“IdSelect”),则
PosCnt=PosCnt+1
如果结束
下一个细胞
MsgBox“行项目总数:”&PosCnt
“计算该项目中咨询服务的所有职位
对于范围内的每个单元格(“项目列表”)
如果Cell.Value=Range(“IdSelect”).Value和Cell.Offset(0,3).Value=“Service”,则
ServCnt=ServCnt+1
如果结束
下一个细胞
MsgBox“咨询服务总数:&ServCnt”
'计算费用项目的数量
ExpCnt=PosCnt-ServCnt
MsgBox“费用总数:&ExpCnt”
端接头
子计数\u总计\u行()
将当前行变暗为整数
Dim Target_RowCnt为整数
Dim Diff_行为整数
目标_RowCnt=62
'计算打印区域中的行数并计算与目标的差异
范围(“打印区域”)。选择
当前_RowCnt=Selection.Rows.Count
差异行=目标行-当前行
如果差异行>0,则
MsgBox“我们需要添加”&Diff_行&“行!”
如果差异行数小于0,则
MsgBox“我们需要删除”&-Diff_行&“行!”
其他的
MsgBox“无需做任何事情,一切都很好!”
如果结束
端接头
Sub Write_Services()'查找数据表上的服务,并将其作为整数Dim PosIdent作为字符串Dim data作为范围写入发票表Dim Cnt
Cnt=0
'建筑物位置标识符
工作表(“输入”)。选择
ActiveSheet.Range(“D26:AD151”)。选择
设置数据=选择
PosIdent=Range(“IdSelect”).Value&“/”&Cnt+1
工作表(“发票”)。选择
ActiveSheet.范围(“服务标题”).偏移量(1,0).激活
'计数器仍然存在问题(如果cnt范围增加1,行号不会增加1
对于Cnt=0到ServCnt+1
ActiveCell.Value=Application.WorksheetFunction.VLookup(PosIdent,Data,15,False)
ActiveCell.Offset(1,0).激活
Cnt=Cnt+1
下一个碳纳米管
端接头

这可能是瞎猜,但我相信你的错误就在这里

PosIdent = "IdSelect" & "/" & Cnt + 1
那应该是

PosIdent = Range("IdSelect").Value & "/" & Cnt + 1
我注意到你只定义了一次,这就是为什么当你的范围改变时它不会改变,我会