Link to home
Start Free TrialLog in
Avatar of BBlu
BBluFlag for United States of America

asked on

VBA For Loop not working

I'm new to VBA and have the attached code.  Can someone tell me what i'm doing wrong?  I'm trying to fill in cells D1-D10 with the numbers 1 through 10.
Sub AddOne()
Dim n As Integer
Dim RanngeToAdd As Range
n = 1
Set RangeToAdd = ActiveSheet.Range("e1:e10")
For Each Cell In RangeToAdd
ActiveCell.Value = n
n = n + 1
Next
End Sub

Open in new window

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello BBlu,

SImple enough when you see it ... cell instead of activecell.  You aren't actually selecting the cell which is good therefore you need to reference 'cell'

Regards,

chris_bottomley
Sub AddOne()
Dim n As Integer
Dim RanngeToAdd As Range
n = 1
Set RangeToAdd = ActiveSheet.Range("e1:e10")
For Each Cell In RangeToAdd
Cell.Value = n
n = n + 1
Next
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BBlu

ASKER

Oh....LOL.
Got it!  
Thanks, Chris.

I'm new to VBA, but having fun learning it!
Avatar of BBlu

ASKER

Oh....LOL.
Got it!  
Thanks, Chris.

I'm new to VBA, but having fun learning it!
We all start sometime so i'm glad to help a little bit

Chris