+ Reply to Thread
Results 1 to 10 of 10

For Each "cell" in Range - question

  1. #1
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    For Each "cell" in Range - question

    I currently have a macro which looks like this:

    Please Login or Register  to view this content.
    Which works all well and good. However, this code also gives the same result:


    Please Login or Register  to view this content.
    So my question is why does the second sub give the same result??? I thought you had to dimension the range then refer to each cell as "cell" but it works with any word by the looks of it (cabbage???). Can someone explain what's going on in the sub, why does the "For Each cabbage" actually work?

    Cheers
    Rob

    Ps - currently using Excel 2010

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For Each "cell" in Range - question

    Hi, Rob,

    as soon as you place Option Explicit at the top of the module the second code and cabbage would be marked by the debugger.

    Anyhow, why not use
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For Each "cell" in Range - question

    Rob

    Both sets of code do exactly the same thing.

    You can call the loop variable anything you want, within reason, and it will work, unless you have Option Explicit at the top of the module.

    Without Option Explicit you don't need to declare your variables.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Re: For Each "cell" in Range - question

    Ah okay I get it now. So would you guys write the code more like this:

    Please Login or Register  to view this content.
    So you dimension two variables? One as the range and one as the cell? Or would you do something else?

    I'd rather dimension everything correctly (use Option Explicit) as it's good practice and it means I have to understand the code properly.

    Cheers

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For Each "cell" in Range - question

    Hi, TheRobsterUK,

    for me IŽd only use one variable like
    Please Login or Register  to view this content.
    and only work with a second one for the original range if it would not be static like here. Still my favourite would be the one-liner.

    I'd rather dimension everything correctly (use Option Explicit) as it's good practice and it means I have to understand the code properly.
    Please have a look at the Reddick-convention or Hungarian notation for more info on that topic.

    Please Login or Register  to view this content.
    Here the variable "tells" a story of what kind of variable it is and what could be expected from it.

    Ciao,
    Holger

  6. #6
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Re: For Each "cell" in Range - question

    Thanks for the replies HaHoBe, most helpful. For my spreadsheet the range is likely to be dynamic so I will have to use a variable to define it rather than Range("A1:A10") which was just an example, but yes I understand that using your method would be simpler if the range was static.

    One final thing - this also works:

    Please Login or Register  to view this content.
    This also works! However the MyCell range is actually larger than the MyRange range so why does the syntax still work? I am confused as to how the computer reads this...why I need a variable to iterate through the cells in a defined range, yet that variable itself can be a single cell or a range bigger than the one I'm iterating through?

    Maybe I'm overthinknig this...

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For Each "cell" in Range - question

    Hi, TheRobsterUK,

    with
    Please Login or Register  to view this content.
    youŽre assigning a range to the variable but when using it as the variable in the For...Next loop it will get assigned to the cells within the range and loose the information you originally assigned to it. Test it by yourself with
    Please Login or Register  to view this content.
    Ciao,
    Holger

  8. #8
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 365 v2402
    Posts
    263

    Re: For Each "cell" in Range - question

    Ah okay so the important thing is that (with Option Explicit on) that I created the MyCell variable, but I take it Excel is clever enough to know that it needs to change the range assigned to the variable so that it can use it in the For...Next loop. I.e. it just needs it to be a suitable type of variable, the range information associated with it originally does not matter as this is lost?

    Cheers
    -Rob

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: For Each "cell" in Range - question

    Hi, Rob,

    maybe just decide to assign ranges to variables for action which might be monitored as a total while you use different ones for the loops which will get assigned there and sort of overwite the contents of the variable each time.

    Ciao,
    Holger

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For Each "cell" in Range - question

    Rob

    VBA isn't really doing anything clever, its just reusing the variable.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 01-23-2014, 11:02 AM
  2. if Range("A1") = "Mike" Then Range("A1") = "michael" Then Exit Sub
    By a8015945 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-30-2013, 10:08 AM
  3. reference date above last cell in range displaying "1" or "2"
    By leeroyrooney in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-20-2013, 05:41 PM
  4. Problem using "Cells" in "Range" "400" error
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2009, 05:46 PM
  5. [SOLVED] use variable in Workbooks("book1").Worksheets("sheet1").Range("a1"
    By Luc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1