'How to loop through the same cell
Private Sub CommandButton1_Click()
Range("C10").Select
For Range("C10").Value = 0 To 35
Range("C10").Value = Range("C10").Value + 1
Next
End Sub
I have written the above code for a cell to loop through the values = 0 to 35 . I am doing this because there are other formulas whose values change according to the value of Range("C10").
The code does not work
Solution 1:[1]
Add an increment variable to your For ... Next loop.
Option Explicit
Private Sub CommandButton1_Click()
Dim i As Long
For i = 0 To 35
Range("C10").Value = i
'do other stuff here
Next
End Sub
Solution 2:[2]
That's not a good way to do what you want:
You'd better use a counter variable, and do something like this:
for counter = 0 to 35:
Range("C10").Value = counter
// check the values of the things you want to verify,
// e.g. by putting them in some cells for verification afterwards,
// like "Range("D10").Offset(0,counter) = ...
Next
(This is just pseudo-code, there might be some syntax errors)
Solution 3:[3]
This one would loop from 0 to 35, changing the value of Range("A1")
with i
on every second. Looks animated:
Public Sub LoopMe()
Dim i As Long
For i = 0 To 35
Worksheets(1).Range("A1") = i
Application.Wait(Now + TimeValue("00:00:01"))
Next
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dominique |
Solution 2 | |
Solution 3 | Vityata |