【发布时间】: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
结束子
【问题讨论】:
-
瑞克给了你一个解决方案。此外,您使用了太多
.Selects和.Activates建议您也查看此链接stackoverflow.com/questions/10714251/…