Run a loop not a set # of times, but based on value of a cell?

d0rian

Active Member
Joined
May 30, 2015
Messages
313
Office Version
  1. 365
My code is below. It currently runs a loop 10 times, but I want 2 variations of it.

1) How can I edit it so that it runs n number of times, where n is the value of cell A1 (which I hardcode / is static prior to the code being run)

2) How do I edit it so that it runs as long as a condition is true? (Say...while the value of cell B1 is less than 5, when B1 changes value as the Loops are running...

VBA Code:
Sub Loop_Clumps()
    Dim i As Long
    For i = 1 To 10
        Range(Range("next_clump").Value).Replace What:="||", Replacement:="=", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Application.Wait (Now + TimeValue("0:00:01"))
    Next i
    Application.CutCopyMode = False
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
Sub Loop_Clumps_1()
    Dim i As Long
    For i = 1 To Range("A1").Value
        Range(Range("next_clump").Value).Replace What:="||", Replacement:="=", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Application.Wait (Now + TimeValue("0:00:01"))
    Next i
    Application.CutCopyMode = False
End Sub

Sub Loop_Clumps_2()
    Dim i As Long
    While Range("B1").Value < 5
        Range(Range("next_clump").Value).Replace What:="||", Replacement:="=", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Application.Wait (Now + TimeValue("0:00:01"))
    Wend
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

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