Loop Structure Recommendations

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

I would like to write a conditional loop that upon meeting the condition(s) will produce a set of variable values to be placed in another sheet.

I would like to be able to produce variables for all instances when a cell value associated with a row (and the counter variable in the loop) is below a threshold, BUT then also produce variable values for the FIRST INSTANCE when the threshold is overcome.

How can I structure a loop so that variable values will be assigned up to the point the threshold is overcome AND at the first point the threshold is overcome?

I am looking for a binary value to be assigned to one of the variables at the moment the threshold is overcome.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
try something along these lines..
- for illustration each value in an array of values checked for values below 6
- value written to string up to and including first time condition is berached
- you can apply the same method to loop and check values in a worksheet range

Code:
Sub Test()
    Dim arr As Variant, a As Long, myStr As String
    arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    For a = 0 To UBound(arr)
        If arr(a) < 6 Then      [COLOR=#006400][I]'write to sheet[/I][/COLOR]
            myStr = myStr & vbCr & "below" & vbTab & arr(a)
        Else [I][COLOR=#006400] 'threshold overcome - write first value to sheet and exit the loop[/COLOR][/I]
            myStr = myStr & vbCr & "breach" & vbTab & arr(a)
            Exit For
        End If
    Next a
    MsgBox myStr
End Sub
 
Last edited:
Upvote 0
Thanks!...I still need to fill in bits and pieces but this is the structure I'm looking for



Code:
If C = "P_HORIZONTAL" Then

        Arr() = PBK.Range(Cells(F, "E"), Cells(2, "E"))
        
        For H = 1 To UBound(Arr)
            
            If E - Arr(H) > 0 Then  'if the resistence trend is below trend write X's and Y to sheet
            
            'calculate X's and Y and write
            
            ElseIf E - Arr(H) <= 0 Then
            
            'calculate X's and Y and write to sheet then exit loop, also redim Arr
            
            End If
        
        
    ElseIf C = "T_HORIZONTAL" Then
    
        Arr() = PBK.Range(Cells(F, "E"), Cells(2, "E"))
    
    End If
 
Upvote 0
Related Question:

I'd like to be able to incorporate an array with additional columns:

Code:
Arr() = PBK.Range(Cells(F, "A"), Cells(2, "E"))        
        For H = 1 To UBound(Arr)

How can I write a counter variable into the loop that uses Arr() as the basis for the counter? Now with two potential subscript values which does UBound use?
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top