簡體   English   中英

VBA查找-無法獲取WorkSheet函數類的Vlookup屬性

[英]VBA Lookup - Unable to get the Vlookup property of the WorkSheet function class

我知道這里已經討論了類似的問題: 為什么VBA中的VLookup失敗,並出現運行時錯誤1004?

但似乎並不能解決我的問題。 快速解釋一下我想在這里做些什么-這是我在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

更新:現在,我已將上一個過程中的代碼更改為:

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的新子程序,該子程序調用了所有其他ServCnt ,然后將變量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

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

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

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

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

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

 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 

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

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

 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 

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

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

 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 

End Sub

更新3:

修復了最后的錯誤-有關詳細信息,請參見下面的評論。 下面的工作代碼:

Option Explicit Public ServCnt As Integer

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

 Sub Main() 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 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 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 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 

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

 Sub Main() 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 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 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 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 

Sub Main() 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 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 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 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

Sub Main() 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 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 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 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

 Sub Main() 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 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 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 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 

End Sub

這可能是在黑暗中拍攝的鏡頭,但我相信您的錯誤就在這里

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM