簡體   English   中英

為什么在遍歷(有時為空)變體時會出現(運行時錯誤“13”:)類型不匹配?

[英]Why am I getting (run-time error '13': ) type mismatch when looping across a (sometimes-empty) variant?

我正在使用Private Sub Worksheet_Change(ByVal Target As Excel.Range)事件(第一次)在單個工作表中構建動態驗證列表。 單元格 A2 和 B2 包含驗證下拉列表; 單元格 B2 取決於單元格 A2; 並且單元格 A2 取決於 A 列中超出 A5 的其余值

[在此列表中,B2 中的最終選擇將產生與其相關的其他值:DB-like ]

這是工作表更改后子程序的工作方式:

VARIABLE DECLARATIONS* variant, range, string

設置三個范圍對象 A、B 和 C,其中 B 和 C 使用 A(范圍中最后使用的單元格)

檢查目標的位置

  1. 如果目標在第一列(A 列)中,

1a) 查看目標是否在第二行 (A2)...如果是,則清除 B 范圍內的內容,調用 function1 返回過濾值數組及其在活動工作表上的單元格位置 >> 分配將數組返回到在后續調用中使用的變體變量 1

  • 調用函數1
  • 變量 1 = 函數 1
  • 調用函數2( Target ,變量1)
  1. 如果目標在第二列 (B) 中,

2a)查看目標是否在第二行(B2)...如果是,則清除C范圍內的內容,調用函數以根據B2中的內容填充C范圍內的單元格; 如果目標不在 B2 中,則不應發生其他任何事情

  1. 如果目標在其他任何地方

...沒發生什么事


問題發生在 call function2; 當我觀察變量/在值更改時添加中斷時,函數 1 和變量 1 似乎正確接收值,但是當調用函數 2 時,它會拋出運行時錯誤並告訴我變量 1 為空...

我不知道在初始執行期間對范圍所做的更改是否算作對工作表的后續更改,或者它們是否都算作相同的更改

我已經成功使 A2 和 B2 中的下拉列表起作用,但是 B2 中的更改並沒有促進最終更改,因此重新配置了代碼......重新配置的代碼產生了奇怪的錯誤追蹤

我確實嘗試停用/激活 enableEvents,(對我來說也是新的)但它沒有改變結果......下面是來自 function2 (arg1, arg2) 的代碼......如果在它之前一切正常,我不明白為什么它返回錯誤

Call StateFinder                                           'calls the function that builds address 
SearchAll50 = StateFinder                                  'sets the new searchable array to it's value
Call ListBuilder(Target, SearchAll50)                      'builds a list of values in the target cell
                                
                                
'a variable set that stores the Target's address
'-----------------------------------------------
StateGPS = GetPlaceGPS(Target.Value, SearchAll50)           'the Target address will be set to variable

GetPlaceGPS 拋出錯誤

Function GetPlaceGPS(ByVal SelectedPlace As String, ByVal MadeList As Variant) As String
'the returned address will then be used to set the range used to search cities

    Dim i As Long     
    For i = LBound(MadeList) To UBound(MadeList)
    
        If MadeList(i)(0) = SelectedPlace Then
            'compares the first value of the ith row to the selected state
            'once the state is found GetStateGPS is set to the sheet address
                
             GetPlaceGPS = MadeList(i)(1)
            
            'exit the function once the value is properly set
             Exit For
        End If        
    Next i
    
End Function

必須使用字符類型參數並且必須聲明為變量。

Dim myString As String
myString = target.Value
StateGPS = GetPlaceGPS(myString, SearchAll50) '<~~ Character type arguments must be used and must be declared as variables.

暫無
暫無

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

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