+ Reply to Thread
Results 1 to 6 of 6

Problems with a For Each loop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Problems with a For Each loop

    I have the following code:

    
    Sub test()
    
    For Each Cell In Range("A3:A40")
    
        If Selection.Interior.Colorindex = 34 Then
            Rows(ActiveCell.Row).Select
            Selection.Copy
                Worksheets(1).Select
                Range("A1").Select
                Selection.End(xlDown).Select
                ActiveCell.Offset(1, 0).Select
                Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
                , SkipBlanks:=False, Transpose:=False
        End If
        
    Next Cell
            
    End Sub
    This makes sense in my head, but it does not do anything. It runs without error, but it doesn't perform any actions.

    There are cell in the range of A3:A40 that contain the interior colorindex 34, but the macro does not copy them, and it doesn't seem to be looping through anything.

  2. #2
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Problems with a For Each loop

    For loop is fine, Your if statement is looking at selection. What did you select?

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: Problems with a For Each loop

    Hmmm - I was thinking that as the loop went through A3:A40, it would go like this:

    1.) Select A3
    2.) As A3 is selected, look at the selection(a3).Interior.Colorindex
    3.) If it's 34, do stuff, if it's not, continue onto A4.
    4.) Select A4 & repeat steps 1 through 3.

    Should I not be using Selection.Interior.Colorindex? I've also tried with Activecell.Interior.Colorindex and this also does not work.

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

    Re: Problems with a For Each loop

    Use the loop variable Cell.

    Sub test()
    
        For Each Cell In Range("A3:A40")
    
            If Cell.Interior.Colorindex = 34 Then
                Cell.EntireRow.Copy
                Worksheets(1).Range("A" & Rows.Count).End(xlUpDown).Offset(1, 0).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
            End If
        
        Next Cell
            
    End Sub
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: Problems with a For Each loop

    Quote Originally Posted by Norie View Post
    Use the loop variable Cell.

    Sub test()
    
        For Each Cell In Range("A3:A40")
    
            If Cell.Interior.Colorindex = 34 Then
                Cell.EntireRow.Copy
                Worksheets(1).Range("A" & Rows.Count).End(xlUpDown).Offset(1, 0).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
            End If
        
        Next Cell
            
    End Sub

    When VBA gets to this line:

    Worksheets(1).Range("A" & Rows.Count).End(xlUpDown).Offset(1, 0).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    I get the following error:

    Run-time error '1004':

    Application-defined or object-defined error.

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

    Re: Problems with a For Each loop

    It should be xlUp not xlUpDown.
    Sub test()
    
        For Each Cell In Range("A3:A40")
    
            If Cell.Interior.ColorIndex = 34 Then
                Cell.EntireRow.Copy
                Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
            End If
        
        Next Cell
            
    End Sub

+ 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. Loop Copying Problems
    By wesbuckley in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-07-2009, 07:17 PM
  2. Loop Problems
    By wesbuckley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2009, 06:38 AM
  3. Macro Help - Loop Problems
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2006, 10:02 PM
  4. problems with loop
    By Arjan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-08-2005, 05:20 AM
  5. So close! Problems with Loop
    By Linking to specific cells in p in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2005, 02:06 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