【问题标题】:VBA Lookup - Unable to get the Vlookup property of the WorkSheet function classVBA 查找 - 无法获取 WorkSheet 函数类的 Vlookup 属性
【发布时间】:2023-03-19 08:55:01
【问题描述】:

我知道这里已经讨论过类似的问题:Why is VLookup in VBA failing with runtime error 1004?

但似乎没有解决我的问题。快速解释我想在这里做什么 - 这是我的第一篇 VBA 帖子,所以如果有任何问题清晰等问题,请告诉我。

我正在尝试构建一个发票表,该发票表基于

  • 项目编号(在本例中为 1)
  • 所有项目数据的数据集

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

第二张表是名为“发票”的实际发票表。这个想法是根据每个项目的行项目数(仍在处理这部分)自动获得正确数量的空白行,并自动填写表格。最后一部分是当我尝试在第 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

更新:我现在已将上一个过程中的代码更改为:

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

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
    PosIdent = Range("IdSelect").Value & "/" & Cnt + 1
    ActiveCell.Value = Application.WorksheetFunction.VLookup(PosIdent, Data, 15, False)
    ActiveCell.Offset(1, 0).Activate
    Cnt = Cnt + 1
Next Cnt
End Sub

但是错误消息仍然相同。感谢您对代码的改进(它确实解决了 PosIdent 没有被循环更新的问题) - 还有其他想法吗?

第 2 次更新:

我现在已经根据我收到的有用答案/cmets 更新了我的代码(非常感谢!),现在它创建了一个新的错误消息(不确定旧的现在是否已解决,因为新的出现在较早的第 59 行的代码)。新错误是“1004:对象'_GLobal'的方法'范围'失败。我真的不知道是什么触发了它,因为我刚刚创建了一个名为Main的新子,它调用了所有其他子,然后传递了变量ServCnt作为最后一个子的参数。有人可以帮忙吗?

下面的新代码:

Option Explicit

子主()

将 ServCnt 调暗为整数

调用 Count_Line_Items 调用 Count_Total_Rows 调用 Write_Services(ServCnt)

结束子

子 Count_Line_Items()

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

 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

结束子

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

结束子

Sub Write_Services(ServCnt) '在数据表上查找服务并将它们写入发票表 将 Cnt 调暗为整数 将 PosIdent 暗淡为字符串 将数据暗淡为范围

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

结束子

更新 3:

修复了最后的错误 - 有关详细信息,请参阅下面的 cmets。工作代码如下:

Option Explicit Public ServCnt As Integer

子主()

调用 Count_Line_Items 调用 Count_Total_Rows 调用 Write_Services(ServCnt)

结束子

子 Count_Line_Items()

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

 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

结束子

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
Sheets("Invoice").Activate
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

结束子

Sub Write_Services(ServCnt) '在数据表上查找服务并将它们写入发票表 将 Cnt 调暗为整数 将 PosIdent 暗淡为字符串 将数据暗淡为范围

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

结束子

【问题讨论】:

标签: excel vba


【解决方案1】:

这可能是在黑暗中拍摄,但我相信你的错误就在这里

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

【讨论】:

  • 感谢您的回答;它没有解决主要问题,但它确实让我将 PosIdent 放入循环中,这是一个不同的问题现在已修复!
  • 好吧,我注意到的另一件事是你没有定义变量 ServCnt 这意味着当你运行你的循环时它只会运行一次。这并不能解决错误消息的问题,但仍需要修复。
  • 我之前应该知道的另一件事是您不需要增加 Cnt,循环会为您完成。
  • 感谢您的 cmets - 我在第一个过程中定义了 ServCnt,并认为我可以在此过程中使用它,因为它默认是公共的?我还尝试将它作为参数传递给当前过程,但该过程将不再运行:当我单击运行时,它要求我从其他两个过程中选择一个,所以我的结论是这不是正确的方法走?谢谢。
  • 如果您的 sub 需要一个参数,您将无法再调试它,因为它没有所需的参数,您必须从另一个过程调用它并给它参数,或者您可以存储它作为一个公共变量 - 在您的选项下显式添加“Public ServCnt as Integer”这将使您的编写服务例程能够引用该变量,但是不能保证该值已由第一个过程设置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2013-10-17
  • 2016-10-13
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多