greenmonster15
Board Regular
- Joined
- Aug 28, 2012
- Messages
- 70
FYI: Super newbie currently reading VBA for Excel 2007 for Dummies as my starting point. Loving it so far!!!
I'm having trouble understanding the following code. It is presented as a BAD example of looping using a GoTo statement. None the less, I would like to be able to understand logically how this code is executed. Can someone explain to me what the code [ ActiveCell.Offset(CellCount, 0) = StartVal + CellCount] is doing and why?
From the book: " The following code demonstrates a bad loop. The procedure simply entersconsecutive numbers into a range. It starts by prompting the user for two
values: a starting value and the total number of cells to fill. (Because InputBox
returns a string, I convert the strings to integers by using the CInt function.)
This loop uses the GoTo statement to control the flow. The CellCount variable
keeps track of how many cells are filled. If this value is less than the number
requested by the user, program control loops back to DoAnother. "
Thanks to anyone who can help explain this to me.
I'm having trouble understanding the following code. It is presented as a BAD example of looping using a GoTo statement. None the less, I would like to be able to understand logically how this code is executed. Can someone explain to me what the code [ ActiveCell.Offset(CellCount, 0) = StartVal + CellCount] is doing and why?
From the book: " The following code demonstrates a bad loop. The procedure simply entersconsecutive numbers into a range. It starts by prompting the user for two
values: a starting value and the total number of cells to fill. (Because InputBox
returns a string, I convert the strings to integers by using the CInt function.)
This loop uses the GoTo statement to control the flow. The CellCount variable
keeps track of how many cells are filled. If this value is less than the number
requested by the user, program control loops back to DoAnother. "
Code:
Sub BadLoop()[INDENT]Dim StartVal As Long[/INDENT]
[INDENT]Dim NumToFill As Long[/INDENT]
[INDENT]Dim CellCount As Long[/INDENT]
[INDENT]StartVal = InputBox(“Enter the starting value: “)[/INDENT]
[INDENT]NumToFill = InputBox(“How many cells? “)[/INDENT]
[INDENT]ActiveCell = StartVal[/INDENT]
[INDENT]CellCount = 1[/INDENT]
DoAnother:[INDENT][B]ActiveCell.Offset(CellCount, 0) = StartVal + CellCount[/B][/INDENT]
[INDENT]CellCount = CellCount + 1[/INDENT]
[INDENT]If CellCount < NumToFill Then GoTo DoAnother _[/INDENT]
[INDENT]Else Exit Sub[/INDENT]
End Sub
Thanks to anyone who can help explain this to me.
Last edited: