Looping through Block of Code

DougStroud

Well-known Member
Joined
Aug 16, 2005
Messages
2,968
I have a large number of lines of code that I need to repeat on another sheet. The criteria for the code is the same, but the parameters are different, i.e. the last row, the name of the sheet, etc...
Rather than creating another procedure I was curious if I can loop through the following code and created new variable, sheet objects to carry out the instructions on the other sheet?
The following code is significantly reduced- I have about 500 total lines of replace, so don't gauge it soley by what you see here-- I did this to save everyone the pain...

Code:
Sub ColorsColorFF()
     Dim Ws As Worksheet, c As Range, rng As Range
     Dim LRow As Long, LRow1 As Long, LRow2 As Long
     Dim i As Long

     Set Ws = ActiveSheet
     LRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

     Application.ScreenUpdating = False

For Each c In Range("N4:N" & LRow)
        c.Formula = " " & c & " "
    Next c

    With Ws
          For Each c In Intersect(Ws.Columns(14), Ws.UsedRange)
'Workbook("Color Subs.xls").Worksheets(
'Single Color Term Swap- #3
If InStr(c.Value, " 60S ") > 0 Then c.Formula = Replace(c.Value, " 60S ", "Denim-Washes")
If InStr(c.Value, " Amber ") > 0 Then c.Formula = Replace(c.Value, " Amber ", "Yellow")
If InStr(c.Value, " Amethyst ") > 0 Then c.Formula = Replace(c.Value, " Amethyst ", "Purple")
If InStr(c.Value, " Aqua ") > 0 Then c.Formula = Replace(c.Value, " Aqua ", "Turquoise")
If InStr(c.Value, " Argyle ") > 0 Then c.Formula = Replace(c.Value, " Argyle ", "Print")
     End With

For Each c In Range("N4:N" & LRow)
  c.Value = Application.WorksheetFunction.Trim(c.Value)
Next c


'     For Each c In rng
'            c.Value = Application.WorksheetFunction.Trim(c.Value)
'            Next c

Application.ActiveSheet.Calculate
Application.ScreenUpdating = True
                  
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
in the sub definition add the variables you want i.e.


Code:
Sub ColorsColorFF(Ws As Worksheet, LRow as Long )

then when you call it you pass the same ones in

Code:
call ColorsColorFF(activesheet, 100)

then when it runs through it will create ws = activesheet and LRow = 100

i hope you get the general idea..
 
Upvote 0
Doug,

Your code uses "Set Ws = ActiveSheet". I can not see anything specific to a sheetname.

You should be able to run this macro on the other sheets. You may have to put the code in a 'Module'.

Have a great day,
Stan
 
Upvote 0
I assumed there was more code that needed it, obviously you wouldn't need to pass activesheet to a function.

If it isn't in a module go to the VBA window (Alt-F11) then right click on microsoft excel objects on the left of the screen and go Insert > Module
 
Upvote 0
Hi
How about Find & replace?

Code:
Sub ColorsColorFF()
     Dim Ws As Worksheet, myList, i As Long, c As Range
     Application.ScreenUpdating = False
    myList = [{" 60S ", "Denim-Washes";" Amber ","Yellow"; " Amethyst ", "Purple"; " Aqua ", "Turauoise"; " Argyle ", "Print"}]
    For Each ws In Worksheets
         With ws.Range("n4", ws.Range("n" & Rows.Count).End(xlUp))
              .Value = Evaluate(""" "" & "&" & .Address & "" """)
         End With
         For i = LBound(myList,1) To UBound(myList,1)
              ws.Columns("n").Replace(myList(i,1), myList(i,2), xlPart)
         Next
        For Each c In ws.Range("N4",.Range("N" & Rows.Count).End(xlUp))
             c.Value = Trim(c.Value)
        Next c
    Next
Application.ActiveSheet.Calculate
Application.ScreenUpdating = True
                  
End Sub
 
Upvote 0
Hello Jason and Stan and Jindon,
Jason and Stan I was reading through your post's when Jindon dropped in. So bare w/ me while I run some things by him.

Jindon-
My list is big- will I need to transform it int the array?

Code:
Here it is... I will delete it after we use it for reference...
[code]If InStr(c.Value, " 60S ") > 0 Then c.Formula = Replace(c.Value, " 60S ", "Denim-Washes")
If InStr(c.Value, " Amber ") > 0 Then c.Formula = Replace(c.Value, " Amber ", "Yellow")
 
Upvote 0
I have a list in another worksheet that I update to creat any new entries or edits, if that is what you mean.

I have cobbled together a formula that I just paste into my code window.

Is that what you are referring to?
 
Upvote 0
J-
Here is a sample of my code:
Color Subs.xls
CDEF
1503StoneLt.Tan
1513StpStripe
1523StripedStripe
1533TaupeLt.Grey
1543TbgDenim-Washes
1553TeaLt.Green
1563TobaccoBrown
1573TreePrint
1583TreezPrint
1593VinDenim-Washes
1603VintageDenim-Washes
1613WalnutBrown
1623WheatLt.Yellow
1633WineDk.Red
1643WinterLt.Blue
1653WoodPrint
1662Amber/ClearYellow+
1672Army/GumOlive+
1682Ash/WhiteLt.Grey+
1692Beige/BlackLt.Tan+
1702Black/BlackBlack+
1712Black/Black/BlackBlack+
1722Black/Black/Red/SilverBlack+
Color Colors
 
Upvote 0
Then try this
Code:
Sub ColorsColorFF()
     Dim Ws As Worksheet, myList, i As Long, c As Range
     Application.ScreenUpdating = False
    With Sheets("Color Colors")
         myList = .Range("d150",.Range("d" & Rows.Count).End(xlUp)).Resize(,2).Value
    End With
    For Each ws In Worksheets
         With ws.Range("n4", ws.Range("n" & Rows.Count).End(xlUp))
              .Value = Evaluate(""" "" & "&" & .Address & "" """)
         End With
         For i = LBound(myList,1) To UBound(myList,1)
              ws.Columns("n").Replace(myList(i,1), myList(i,2), xlPart)
         Next
        For Each c In ws.Range("N4",.Range("N" & Rows.Count).End(xlUp))
             c.Value = Trim(c.Value)
        Next c
    Next
Application.ActiveSheet.Calculate
Application.ScreenUpdating = True
                  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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