Simple question regarding Range.Offset

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. "

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:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
This is the statement:

Code:
ActiveCell.Offset(CellCount, 0) = StartVal + CellCount
Code:

The varialbe CellCount would have to have been declared as some type of number data type. Since StartVal was declared as Long, I would assume that CellCount is also. StartVal is also a variable, which should be of the same data type as CellCount, since they are added to each other in the statement. The offset will count down from the active cell for as many rows as the value of CellCount is equal to and then put the sum value of CellCount and StartVal into that cell.
 
Upvote 0
Amazing. Thank you.

I got confused when the CellCount was being used on both sides of the statement. I see now that it was using the value of CellCount to relocate the ActiveCell and then again on the other side to place a value into that cell. Beginner confusion....the first of many to come! Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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