2016-07-13 3 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 

End Sub

ответ

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 'строки вместе отображаются txt' Вы можете запустить свой процесс по значению, найденному по ссылке. ActiveCell.Value или .Range. Просто замените stat 'msg = msg'. –

Смежные вопросы