Loop column values and run procedures for each value

GDunn

Board Regular
Joined
Mar 24, 2009
Messages
51
Hi,


I am trying to:
  • Loop through the list of names in 'Set RmRng = Worksheets("Data").Range("B9:B25")'
  • Copy and Paste each value into 'Set TgRng = Worksheets("Data").Range("C1")'
  • Run procedures
  • Repeat for next value in range 'Set RmRng = Worksheets("Data").Range("B9:B25") until last value in range
But am unable to loop once I leave the range. Does anyone know if this can be done?

Sub process_test()
Dim RmRng As Range
Dim TgRng As Range
Set RmRng = Worksheets("Data").Range("B9:B25")
Set TgRng = Worksheets("Data").Range("C1")

'unhide sheets
UnhideAllSheets

'select CAM/BDM name
Worksheets("Data").Select
RmRng.Select
ActiveCell.Offset(0, 0).Copy
TgRng.PasteSpecial (xlPasteAllUsingSourceTheme)

'refresh pivot tables name range
Refresh_Pivots

'hide Team sheets
Hide_Sheet

'hide Data sheet
Hide_Data

'print PDF sheets to file
PrintPDF
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Maybe you could adapt this to suit your needs
Code:
Sub cyclethroughlist()
Dim LR As Long, i As Long
With Sheets("Sheet2")
    LR = .Range("A" & Rows.count).End(xlUp).Row
    For i = 1 To LR
        .Range("A" & i).Copy Destination:=Sheets("Sheet1").Range("A3")
        Call Macro2
    Next i
End With
End Sub
 
Upvote 0
Hi,
take a look at this code
Code:
Sub process_test()
    Dim RmRng                      As Range
    Dim TgRng                      As Range
    Dim i                          As Long

    Set RmRng = Worksheets("Data").Range("B9:B25")
    Set TgRng = Worksheets("Data").Range("C1")

    'unhide sheets
    UnhideAllSheets


    'NewComment.  There is rarely a need to select something you're working with.
    'select CAM/BDM name
    '    Worksheets("Data").Select
    '    RmRng.Select
    'NewComment.  activecell.Offset(0,0) is the same thing as activecell,
    'NewComment.  which is the same thing as RmRng (which I'm assuming is the
    'newcomment.  range you want to loop through)
    '    ActiveCell.Offset(0, 0).Copy

    'NewComment. using a long counter variable to keep track of what you're looping through
    For i = 9 To 25
        'NewComment. I'm assuming that cell C1 doesn't need all the formatting and whatnot
        'NewComment. to be brought over, just the value.
        
        TgRng.Value = Worksheets("Data").Range("B" & i).Value

        'NewComment. If you do need all the formatting and whatnot, then use these lines
        'Worksheets("Data").Range("B" & i).copy
        'tgrng.PasteSpecial(xlPasteAllUsingSourceTheme)

        'NewComment.  Assuming that each of these subs need to be ran for each cell in the
        'NewComment. range b9:b25. If not, then place the subs that don't need to be
        'NewComment. ran on every iteration AFTER the next i line.
        
        'refresh pivot tables name range
        Refresh_Pivots

        'hide Team sheets
        Hide_Sheet

        'hide Data sheet
        Hide_Data

        'print PDF sheets to file
        PrintPDF

    Next i
End Sub
...and for possibly more info than you looking for, you can loop through a range more directly by looping through it's collection of range. For example,
instead of this
Code:
Sub LoopStyle1()
    Dim i As Long
    For i = 1 To 5
        MsgBox Range("A" & i).Value
    Next i
End Sub
you could use this
Code:
Sub LoopStyle2()
    Dim cl As Range
    For Each cl In Range("A1:A5")
        MsgBox cl.Value
    Next cl
End Sub
 
Upvote 0
Thanks pboltonchina and cjo

Both codes are very helpful.

cjo thanks for the additional loop examples.
 
Upvote 0

Forum statistics

Threads
1,216,366
Messages
6,130,198
Members
449,566
Latest member
MikeWnbExclWiz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top