+ Reply to Thread
Results 1 to 6 of 6

Loop or For Each within a For Each

Hybrid View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Loop or For Each within a For Each

    I am having some technical difficulties trying to place data onto sheet2. Sheet2 starts out blank, as sheet1 is proccessed it pastes data to sheet2. I have tried a "For Each" that failed only pasting data into Range "A1" for every found instance.

    In the code below, the area colored "Magenta", I need a Loop of some type that as data is pasted into column A on sheet2, it indexes to the next available cell and continues.
    How do I construct such a Loop or For Each with in the existing For Each?
    Sub aaa()
    If Left(ActiveWorkbook.Name, 3) = "MOT" Then
    
        'iTimeStart = Now()    'Time counter start to time macro
        With Sheets("Sheet1")    'Loop thru Column A
            Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set Sh1Range = .Range("A10:A" & Sh1LastRow)
        End With
    
        With Sheets("Sheet2")    'Loop thru Column A
            Sh2LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set Sh2Range = .Range("A1:A" & Sh2LastRow)
        End With
    
        For Each Sh1Cell In Sh1Range
            If IsNumeric(Sh1Cell.Offset(0, 3).Value) = True Then
                If Sh1Cell.Offset(0, 5).Comment Is Nothing Then
                    If Sh1Cell.Offset(0, 5).Value <> "" Then
                        sBubbleNumber = Sh1Cell.Offset(0, 3).Value
                        sDimValue = Sh1Cell.Offset(0, 5).Value
                        Sh1Cell.Activate
                        'add "For Each" or "Loop" for sheet2?
    
                                'Sh2Cell.Value = sBubbleNumber
                                'Sh2Cell.Offset(0, 1).Value = sDimValue
    
                        'MsgBox sBubbleNumber & " " & sDimValue    'for testing
                    End If
                End If
            End If
        Next Sh1Cell
    
    End If
    End Sub
    Any hints, tips or examples are welcome.
    Last edited by Rick_Stanich; 01-22-2010 at 12:56 PM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Loop or For Each within a For Each

    It's not clear from code alone what is suppose to happen.
    Can you create small test workbook with before and after records.

    guessing....
    Should Sh2Range be a range of cells? Perhaps it should be just the next single cell at the end of any existing data. Then you would simply set the reference to the next cell below by using the OFFSET method.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Re: Loop or For Each within a For Each

    No animals have been harmed in the making of this macro!

    In the macro, enable this line to see what data is found on sheet 1.
    'MsgBox sBubbleNumber & " " & sDimValue    'for testing
    Attached Files Attached Files
    Last edited by Rick_Stanich; 01-22-2010 at 12:36 PM.

  4. #4
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Re: Loop or For Each within a For Each

    Edit:
    I forgot the macro is workbook name sensitive.
    in the macro change:
    If Left(ActiveWorkbook.Name, 3) = "MOT" Then
    to
    If Left(ActiveWorkbook.Name, 4) = "Copy" Then

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Loop or For Each within a For Each

    I guessed right,

    Sub aaa()
        Dim rngSht2Output As Range
        
    If Left(ActiveWorkbook.Name, 3) = "Cop" Then
    
        'iTimeStart = Now()    'Time counter start to time macro
        With Sheets("Sheet1")    'Loop thru Column A
            Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set Sh1Range = .Range("A10:A" & Sh1LastRow)
        End With
    
        With Sheets("Sheet2")    'Loop thru Column A
            If Len(.Range("A1").Value) > 0 Then
                Set rngSht2Output = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            Else
                Set rngSht2Output = .Range("A1")
            End If
        End With
    
        For Each Sh1Cell In Sh1Range
            If IsNumeric(Sh1Cell.Offset(0, 3).Value) = True Then
                If Sh1Cell.Offset(0, 5).Comment Is Nothing Then
                    If Sh1Cell.Offset(0, 5).Value <> "" Then
                        sBubbleNumber = Sh1Cell.Offset(0, 3).Value
                        sDimValue = Sh1Cell.Offset(0, 5).Value
                        rngSht2Output.Value = sBubbleNumber
                        rngSht2Output.Offset(, 1).Value = sDimValue
                        Set rngSht2Output = rngSht2Output.Offset(1)
                    End If
                End If
            End If
        Next Sh1Cell
    
    End If
    End Sub

  6. #6
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Re: Loop or For Each within a For Each

    How the heck do you guys (gals) do this? LOL
    You make it seem simple with the methods you use.

    I see the loop need not be within the"For Each" as I thought.

    Thank you!

    My last humbling attempt. Which only resulted in the last entry (49) being pasted to sheet2 for all 9 total values found. (Just so you know I was working on it.)
    Sub aaa()
    If Left(ActiveWorkbook.Name, 4) = "Copy" Then
    
        'iTimeStart = Now()    'Time counter start to time macro
        With Sheets("Sheet1")    'Loop thru Column A
            Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
            Set Sh1Range = .Range("A10:A" & Sh1LastRow)
        End With
    
        'With Sheets("Sheet2")    'Loop thru Column A
        'Sh2LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        'Set Sh2Range = .Range("A1:A" & Sh2LastRow)
        'End With
    
        For Each Sh1Cell In Sh1Range
            If IsNumeric(Sh1Cell.Offset(0, 3).Value) = True Then
                If Sh1Cell.Offset(0, 5).Comment Is Nothing Then
                    If Sh1Cell.Offset(0, 5).Value <> "" Then
                        sBubbleNumber = Sh1Cell.Offset(0, 3).Value
                        sDimValue = Sh1Cell.Offset(0, 5).Value
                        Sh1Cell.Activate
                        'add "For Each" or "Loop" for sheet2?
                        With Sh1Cell
                            'MsgBox sBubbleNumber & " " & sDimValue    'for testing
                            With Sheets("Sheet2")    'Loop thru Column A
                                Sh2LastRow = .Cells(Rows.Count, "A").Top(xlDown).Row
                                Set Sh2Range = .Range("A1:A" & Sh2LastRow)
                            End With
                            For Each Sh2Cell In Sh2Range
                                Sh2Cell.Value = sBubbleNumber
                                Sh2Cell.Offset(0, 1).Value = sDimValue
                            Next Sh2Cell
                        End With
                    End If
                End If
            End If
        Next Sh1Cell
    
    End If
    End Sub
    Just ignor my code, it may cause brain damage.
    Last edited by Rick_Stanich; 01-22-2010 at 12:56 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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