• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

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

0
BBlu
Asked:
BBlu
  • 3
  • 2
1 Solution
 
Chris BottomleySoftware Quality Lead EngineerCommented:
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

0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
BBlu,

In fact you had a typo as well so the following tidies it up somewhat.

chris_bottomley
Sub AddOne()
Dim n As Integer
Dim RangeToAdd As Range
Dim cel As Range

    n = 1
        Set RangeToAdd = ActiveSheet.Range("e1:e10")
        For Each cel In RangeToAdd
        cel.Value = n
        n = n + 1
    Next
    
End Sub

Open in new window

0
 
BBluAuthor Commented:
Oh....LOL.
Got it!  
Thanks, Chris.

I'm new to VBA, but having fun learning it!
0
 
BBluAuthor Commented:
Oh....LOL.
Got it!  
Thanks, Chris.

I'm new to VBA, but having fun learning it!
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
We all start sometime so i'm glad to help a little bit

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now