2017-06-26 17 views
0

我使用這個功能,以產生一個字符串數組,並將其正確工作以輸出一個字符串數組: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類型等於字符串數組。有什麼方法將字符串數組轉換爲變體或者爲動態生成的字符串數組創建循環?我試圖解決的問題是我需要動態生成字符串數組,因爲字符串搜索條件可能會逐個文件地改變文件。

+0

再告訴我們爲什麼您需要聲明爲Variant? – MacroMarc

回答

0

這聽起來像你的主要挑戰是如何循環你的字符串數組。以下代碼顯示瞭如何執行此操作:

For i = LBound(test_arr) To UBound(test_arr) 
     'your logic here 
     'you would refer to an element like this test_arr(i) 
    Next 
+0

我意識到我沒有返回數組,因爲沒有將函數var名稱設置爲數組。現在沒有問題,謝謝這個語法,但將來會有幫助! –

相關問題