2016-07-13 75 views
0

用戶通過使用複選框選擇所需的選項。每個複選框的標題值存儲在一個動態數組中,然後顯示在確認選擇的消息框中。搜索動態數組中的值(vba)

我現在需要遍歷一系列單元格,在每一行確定單元格(x,4)是否等於數組中的任何值,但我不知道如何循環。請參閱下面的代碼填充數組的位置。

預先感謝您!

Sub ProcessStrats_Click() 
Dim ctl As Control 
Dim cnt As Long 
Dim msg As String 
Dim i As Long 
Dim cResp As Integer 
Dim stArray() As Variant 

    cnt = 0            'Initialize counter outside of loop 
    For Each ctl In StratFill.Controls     'look at every control in StratForm box 
     If Left(ctl.Name, 8) = "CheckBox" Then   'if the control is named 'checkbox' then 
      If ctl.Value = True Then     'if the control is marked as 'true' i.e. checked, then 
       ReDim Preserve stArray(0 To cnt)  'Reset the array dimension on each iteration of loop 
       stArray(cnt) = ctl.Caption    'Add value in value of checkbox caption to Array 
       cnt = cnt + 1       'Advance the counter to next array item 
      End If 
     End If 
    Next 

    Unload StratFill         'unload and close stratfill form 


    msg = "The following strategies will be priced:" & vbNewLine & vbNewLine 
    For i = LBound(stArray) To UBound(stArray)   'loops through all values of array 
      msg = msg & stArray(i) & vbCR    'strings together displayed txt 
    Next i 

     If MsgBox(msg, vbYesNo, "Confirm Strategies") = vbYes Then 
                    'if yes is clicked 
      Call RunPricing           '"RunPricing" will run 
     Else              'if no is clicked 
      StratFill.Show           'then the strategy selector box will display again 
     End If 

結束子

回答

0

嘗試這種情況:

For i = 1 To UBound(stArray)      'loops through all values of array 

    Range("$D2:" & Range("D" & Rows.Count).End(xlUp).Address).Select 

    Selection.Find(What:=stArray(i), After:=ActiveCell, LookIn:=xlFormulas, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Offset(0, 2).Select 

    msg = msg & stArray(i) & ActiveCell.Value & vbCR    'strings together displayed txt 
Next i 
+0

而不是在消息顯示出來,有一個方法來標記發起如果則過程對於找到以匹配每一個數組中的值? – EmsBish

+0

@emsbish在你原來的問題中有:msg = msg&stArray(i)&vbCR'strings together displayed txt'你可以在ref中找到的值運行你的進程。 ActiveCell.Value或.Range。只需更換'msg = msg'的說明即可。 –