我使用這個功能,以產生一個字符串數組,並將其正確工作以輸出一個字符串數組:VBA環接通過動態生成的字符串數組(類型不匹配)
Dim tmp As String
Dim arr() As String
If Not Selection Is Nothing Then
For Each cell In Selection
If (cell <> "") And (InStr(tmp, cell) = 0) Then
tmp = tmp & cell & "|"
End If
Next cell
End If
If Len(tmp) > 0 Then tmp = Left(tmp, Len(tmp) - 1)
Debug.Print tmp
arr = Split(tmp, "|")
我遇到的問題是我然後嘗試使用生成的數組遍歷我在電子表格中搜索的一組字符串。我需要動態生成這個數組。
'Create IRAF array
Dim test_col As Range
Set test_col = Range(CStr(testSearch.Offset(1, 0).Address), Range(CStr(testSearch.Address)).End(xlDown))
test_col.Select
test_arr = create_array(test_col)
Debug.Print test_arr
'Loop for the Test Cycles
Dim iGen As Variant
Dim iCurrent As Variant
Dim i As Integer
iGen = test_arr
For Each iCurrent In iGen
Set Search = Cells.Find(What:=iCurrent, _
After:=Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Search Is Nothing Then...
我知道我遇到的問題是,陣列正在從函數返回一個數組:我知道如何遍歷數組是用一個變體,並這樣寫代碼的唯一方法的字符串,然後我試圖設置一個Variant類型等於字符串數組。有什麼方法將字符串數組轉換爲變體或者爲動態生成的字符串數組創建循環?我試圖解決的問題是我需要動態生成字符串數組,因爲字符串搜索條件可能會逐個文件地改變文件。
再告訴我們爲什麼您需要聲明爲Variant? – MacroMarc