Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6376361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:44:03+00:00 2026-05-25T01:44:03+00:00

I am trying to set the range in For loop. My code works fine

  • 0

I am trying to set the range in For loop. My code works fine when I do this:

For Each i in Range("A1":"A5")
   'Some process code
Next i

But I do not get the same results when I do this:

For Each i in Range("A1").End(xlDown)
    'Some Process
Next i

Arent the two codes equivalent? What changes should I make to the second one that it perfoms the same way as the first one but doesn’t make me hardcode the Range in the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:44:03+00:00Added an answer on May 25, 2026 at 1:44 am

    The second one you have only gets the last cell in the range, which I believe would me A5 from the first example. Instead, you need to do something like this.

    I structured this like a small test so you can see the first option, the corrected second, and an example of how I would prefer to do this.

    Option Explicit
    
    Sub test()
    
      Dim r As Range
      Dim x As Range
    
      ' Make sure there is stuff in Range("A1:A5")
      Range("A1") = 1
      Range("A2") = 2
      Range("A3") = 3
      Range("A4") = 4
      Range("A5") = 5
    
      ' Your first option
      For Each x In Range("A1:A5")
        Debug.Print x.Address & ", " & x
      Next
    
      ' What you need to do to get the full range
      For Each x In Range("A1", Range("A1").End(xlDown))
        Debug.Print x.Address & ", " & x
      Next
    
      ' My preferred method
      Set r = Range("A1").End(xlDown)
      For Each x In Range("A1", r)
        Debug.Print x.Address & ", " & x
      Next
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to set DoMatchParen I get the following error: E492: Not an editor
I'm new to VBA and I'm trying this for loop out, but I think
I'm trying to group a set of data based on the range of an
Im trying to set the certificate friendly name during the certificate request/acceptance process. I
I am trying to generate a set of points that do not fall within
I've been trying to get a slider to display its current value next to
I am trying to get a date range using Guava's new Range functionality, via
I am trying to set up ehcache replication as documented here: http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s22.2 This is
I'm trying to add a list in the for loop. Here is my code
I am trying to detect whether an integer was set, and if not, skip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.