VBA查找 - 无法获取WorkSheet函数类的Vlookup属性

时间:2012-12-18 00:05:45

标签: excel-vba vba excel

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

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

我正在尝试构建一张基于

构建发票的发票表
  • 项目编号(本例中为1)
  • 所有项目数据的数据集

每个项目活动都显示为单独的行项目,并由唯一标识符标识,该标识符包含项目编号和行项目编号(因此对于项目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

更新:我现在已将最后一个程序中的代码更改为:

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号:

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

以下新代码:

ServCnt

Option Explicit

Sub Main()

Dim ServCnt As Integer

Call Count_Line_Items Call Count_Total_Rows Call Write_Services(ServCnt)

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 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(ServCnt) '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

更新3:

修正了最后的错误 - 请参阅下面的评论了解详情。下面的工作代码:

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 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

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

1 个答案:

答案 0 :(得分:1)

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

PosIdent = "IdSelect" & "/" & Cnt + 1

应该是

PosIdent = Range("IdSelect").Value & "/" & Cnt + 1

另外我注意到你只定义了一次这就是为什么当你的范围发生变化时它不会变成chnage,我会在这里移动这段代码

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