2012-12-18 59 views
1

的VLOOKUP屬性我知道,類似的問題已經在這裏討論:Why is VLookup in VBA failing with runtime error 1004?VBA查詢 - 無法獲取工作表函數類

但似乎並沒有解決我的問題。快速解釋我想在這裏做什麼 - 這是我第一個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行的代碼中)。新錯誤是對象'_GLobal'的'1004:範圍'失敗。我真的不知道是什麼觸發了它,因爲我剛剛創建了一個名爲Main的新子,它調用所有其他子元素,然後將變量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 

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 

End Sub

+1

裏克已經給了你一個解決方案。你也使用太多'.Select'和'.Activates'建議看看這個鏈接以及http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select –

回答

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

感謝您的回答;它沒有解決主要問題,但它確實讓我把PosIdent放入循環中,這個問題現在已經解決了! – Matthias

+0

好吧,我注意到的另一件事是你沒有定義變量ServCnt,這意味着當你運行你的循環時它只會運行一次。這並不能解決錯誤信息的問題,但仍需要修復。 – Rick

+0

我應該在之前選擇的另一件事是你不需要增加Cnt,循環會爲你做。 – Rick