How to use a cell value as a condition in a While Do Loop

BogginsDad

New Member
Joined
Apr 25, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Have spent an hour looking for an answer on this and have tried various options... My loop works perfectly when I use number 4 but doesn't when I reference cell H2 (on the only sheet in my workbook and from where I trigger the Macro) I want the user to be able to define via H2 how many times they'd like the loop to run. I'd also like to write back to cell H3 each time the loop has run. Effectively H2 has the target number of loops and H3 will have the actual number of loops run. I'd also like to add a button to Sheet 1 to break this loop if required.

Dim i As Integer
i = 1
Do While i < 4 'H2
Application.Wait (Now + TimeValue(Range("L2").Text))
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
i = i + 1
Loop


Thanks in advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
declare something as long and do something like this

VBA Code:
Sub hi()
Dim sheetCell As Long
With Sheets("Sheet1")
    sheetCell = .Range("H2")
    While i < sheetCell
    
    Wend
End With
End Sub
 
Upvote 0
And how would I write the current value of i back to H3 after the execution of each loop?
 
Upvote 0
See if this works
VBA Code:
    Dim i As Integer
    i = 1

    If IsNumeric(Range("H2").Value) Then
        Do While i < Range("H2").Value                'H2
            Application.Wait (Now + TimeValue(Range("L2").Text))
            ActiveSheet.Paste
            ActiveCell.Offset(1, 0).Range("A1").Select
            Range("H3").Value = i
            i = i + 1
        Loop
    Else
        MsgBox "Error - The contents of cell H2 is not a number"
    End If
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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