+ Reply to Thread
Results 1 to 4 of 4

Double 'For Each Cell' loop range problem

Hybrid View

gombi Double 'For Each Cell' loop... 06-20-2014, 12:28 PM
Mad-Mizer Re: Double 'For Each Cell'... 06-20-2014, 12:42 PM
Norie Re: Double 'For Each Cell'... 06-20-2014, 12:48 PM
gombi Re: Double 'For Each Cell'... 06-20-2014, 02:25 PM
  1. #1
    Registered User
    Join Date
    06-26-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    69

    Double 'For Each Cell' loop range problem

    Hi all,

    I’m doing a ‘for loop’ within a ‘for loop’ and the range does not change. I can’t figure out why.

    I need the variable ColumnB to give me the addresses of column B, but it returns $A$1 and so on.

    Thanks for an advice.

    Sub Rectangle1_Click()
    
       For Each Cell In Range("A1:A4")
         
        ColumnA = Cell.Address
        MsgBox ColumnA
                
                    For Each Cell1 In Range("B1:B4")
                        ColumnB = Cell.Address
                        MsgBox ColumnB
                    Next Cell1
        Next Cell
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Double 'For Each Cell' loop range problem

    Hi gombi,

    Declaring variables will be very helpful as you learn how to code VBA. If you need to know about variable types, I suggest you Google VBA Variable types. As for the code itself, let me know if mine covers what you want. If yes, compare it to yours and think about the differences.
    Sub RClick()
        'Declare all Variables here'
        Dim oC As Range, cA As String, cB As String
        Dim wS As Worksheet
        
        'Set the variable wS to be the active sheet
        Set wS = ActiveSheet
        'Loop through the A range and set the addresses to _
        'the String type variables.
        For Each oC In wS.Range("A1:A4")
            cA = oC.Address
            cB = oC.Offset(0, 1).Address
            MsgBox "cA = " & cA & " | cB = " & cB
            'the Immediate window shows displays better when programming, click _
            'view and click Immediate window for debugging.
            Debug.Print "cA = " & cA & " | cB = " & cB
        Next oC
        
    End Sub
    Click the * to give Rep to a post you like.

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

    Re: Double 'For Each Cell' loop range problem

    Try changinging Cell to Cell1 in the inner loop.

    Mind you I'm not sure you need 2 loops.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    06-26-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Double 'For Each Cell' loop range problem

    Try changinging Cell to Cell1 in the inner loop.
    Thanks a lot! That fixed my issue! :-)

+ 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. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  2. Problem using loop for sending excel range in email body using Range to HTML code
    By drajanm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2012, 03:17 AM
  3. Loop and range problem
    By jalea148 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2012, 03:24 PM
  4. .Find problem when the found range's rows go from single to double digits
    By alsega in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-13-2012, 11:21 AM
  5. problem with Range statement in With-End With loop
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2010, 04:19 AM

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