2017-07-26 80 views
0

我在解決類型不匹配時遇到問題,當在不同的工作簿中運行相同的代碼時。讓我們說練習冊1是原始練習冊,練習冊2是新練習冊。VBA類型不匹配在不同的工作簿中相同的功能

這兩個工作簿1 & 2具有相同的代碼(下面)Listbox_Refresh子調用GetAccountRef()函數。該代碼在工作簿1中運行良好,但在工作簿2中存在類型不匹配,我無法弄清楚原因。

我檢查了兩個工作簿中的VarTypesGetAccountRef(),它們是不同的。

對於工作簿1

  • 這導致8204(的VBArray +變型)如預期:

    Debug.Print VarType(GetAccountRef()) 
    
  • 這導致8(字符串)如預期:

    Debug.Print VarType(GetAccountRef(0)) 
    

For Workbook 2

  • 這導致0(空):

    Debug.Print VarType(GetAccountRef()) 
    
  • 這導致錯誤類型不匹配:

    Debug.Print VarType(GetAccountRef(0)) 
    

我試圖運行的函數是:

Function GetAccountRef() As Variant 
On Error Resume Next 

Dim Cell As Range 
Dim Row_I As Range 
Set Row_I = Sheet5.Range("9:9") '<- ERROR: This range does not contain "Date" 

Dim Counter As Integer 
Counter = 0 
Dim Date_Ref() As Variant 


For Each Cell In Row_I 
    If Cell = "Date" Then 

     ReDim Preserve Date_Ref(Counter) 
     Date_Ref(Counter) = Cell.Address 
     GetAccountRef = Date_Ref 

     Counter = Counter + 1 
    End If 
Next Cell 


On Error GoTo 0 
End Function 

和我想要使用此功能在For循環,像這樣:

Dim ListedBnk As Variant 
    For Each ListedBnk In GetAccountRef() 
     ListedBnk = Replace(ListedBnk, "9", "7") 
     .ComboBox1.AddItem Range(ListedBnk) 
     .ComboBox2.AddItem Range(ListedBnk) 

    Next ListedBnk 

謝謝!

+0

取下上的錯誤繼續下一步 - 在那裏,它是如何失敗? –

+0

由於GetAccountRef的值最終從Row_I範圍繪製,我懷疑該範圍在Workbook2中爲空。無論如何,我同意上面的評論,應該刪除錯誤陷印,並且應該逐步瀏覽代碼,以確切地查看它失敗的位置。 –

+0

@TonyM是的,這的確是這樣,希望我早點看到您的評論! – AnthonyT

回答

0

您的函數有一些錯誤。

Function GetAccountRef() As Variant 
On Error Resume Next 

Dim Cell As Range 
Dim Row_I As Range 
Set Row_I = Sheet5.Range("9:9") 'TFSA Tracker ONLY 

Dim Counter As Integer 
Counter = 0 
Dim Date_Ref() As Variant 


For Each Cell In Row_I 
    If Cell = "Date" Then 

     ReDim Preserve Date_Ref(Counter) 
     Date_Ref(Counter) = Cell.Address 
     Counter = Counter + 1 
    End If 
Next Cell 

     GetAccountRef = Date_Ref '<~~ At this moved. 
On Error GoTo 0 
End Function 

和你的表模塊

Sub test() 
    Dim ListedBnk As Variant 
    Dim myArray As Variant 

    myArray = GetAccountRef 

    With ActiveSheet 
     .ComboBox1.Clear 
     .ComboBox2.Clear 
    'For Each ListedBnk In GetAccountRef() 
    For Each ListedBnk In myArray 
     ListedBnk = Replace(ListedBnk, "9", "7") 
     .ComboBox1.AddItem Sheet5.Range(ListedBnk) 
     .ComboBox2.AddItem Sheet5.Range(ListedBnk) 

    Next ListedBnk 
    End With 
End Sub 
+0

將GetAccountRef = Date_Ref移至該函數的末尾,修復了VarType問題並將VarType(GetAccountRef())識別爲8204,但無法將VarType(GetAccountRef(0))識別爲數組 - 它顯示爲0. 表單模塊由於GetAccountRef()爲空,所以更改給出了循環錯誤。 感謝您的評論,我發現我的錯誤!將它張貼在下面..這很愚蠢.. – AnthonyT

0

發現我的錯誤,功能會在不包含它的範圍內識別。感謝所有發佈評論/解決方案的人!

改進的方法是編寫動態標識符,以便在添加行/列時調整範圍。

功能:

Function GetAccountRef() As Variant 
On Error Resume Next 

Dim Cell As Range 
Dim Row_I As Range 
Set Row_I = Sheet5.Range(**"10:10"**) '<- previous range("9:9") did not contain the *identifier "Date"*, it was in row 10. 

Dim Counter As Integer 
Counter = 0 
Dim Date_Ref() As Variant 


For Each Cell In Row_I 
    If Cell = "Date" Then 

     ReDim Preserve Date_Ref(Counter) 
     Date_Ref(Counter) = Cell.Address 
     GetAccountRef = Date_Ref 

     Counter = Counter + 1 

    End If 
Next Cell 



On Error GoTo 0 
End Function 

表子:

With Activesheet 
     Dim ListedBnk As Variant 
     For Each ListedBnk In GetAccountRef() 
      ListedBnk = Range(Replace(ListedBnk, "10", "8")) '<- Also needs to refer to **Row 10** 
      .ComboBox1.AddItem ListedBnk 
      .ComboBox2.AddItem ListedBnk 

     Next ListedBnk 
    End With