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
 
J-
This line is missing something.... not sure
ws.Columns("n").Replace(myList(i,1), myList(i,2), xlPart)

There are only two worksheets we need to evaluate not each,
PCCombined_FF and PCCombined_VB
not every worksheet in the workbook
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about?
Code:
Sub ColorsColorFF()
     Dim Ws As Worksheet, myList, i As Long, c As Range, e
     Application.ScreenUpdating = False
    With Sheets("Color Colors")
         myList = .Range("d150",.Range("d" & Rows.Count).End(xlUp)).Resize(,2).Value
    End With
    For Each e In Array("PCCombined_FF", "PCCombined_VB")
         With Sheets(e)
               With .Range("n4", .Range("n" & Rows.Count).End(xlUp))
                   .Value = Evaluate(""" "" & "&" & .Address & "" """)
               End With
               For i = LBound(myList,1) To UBound(myList,1)
                    .Columns("n").Replace What:=myList(i,1), _
                              Replacement:=myList(i,2), LookAt:=xlPart
               Next
               For Each c In .Range("N4",.Range("N" & Rows.Count).End(xlUp))
                    c.Value = Trim(c.Value)
               Next c
         End With
    Next
Application.ActiveSheet.Calculate
Application.ScreenUpdating = True
                  
End Sub
 
Upvote 0
J-
The trim feature failed, Type mismatch error.

All the colors in col N on both sheets returned a #Value error.
 
Upvote 0
It performed the operation but generated this error:
MasterImportSheetWebStorecolors.xls
DEFGHIJKLMN
432305BootsBlack/White999SNOWBOOT274.99   #VALUE!
532305BootsBlack/White100SNOWBOOT274.993256305BW  10#VALUE!
632305BootsBlack/White110SNOWBOOT274.993256305BW  11#VALUE!
732LashedBoots06Black/Red/Grey999SNOWBOOT199.99   #VALUE!
832LashedBoots06Black/Red/Grey102SNOWBOOT199.993256LASBGR  10#VALUE!
932LashedBoots06Black/Red/Grey110SNOWBOOT199.993256LASBGR  11#VALUE!
1032LashedBoots06Black/Red/Grey71SNOWBOOT199.993256LASBGR  7#VALUE!
1132LashedBoots06Black/Red/Grey82SNOWBOOT199.993256LASBGR  8#VALUE!
1232LashedBoots06Black/Red/Grey92SNOWBOOT199.993256LASBGR  9#VALUE!
1332LashedBoots06Black/Red/Grey9.52SNOWBOOT199.993256LASBGR  91/2#VALUE!
1432LashedBoots06Black999SNOWBOOT199.99   #VALUE!
PCCombined_FF
 
Upvote 0
How about?
Code:
Sub ColorsColorFF()
     Dim Ws As Worksheet, myList, i As Long, c As Range, e
     Application.ScreenUpdating = False
    With Sheets("Color Colors")
         myList = .Range("d150",.Range("d" & Rows.Count).End(xlUp)).Resize(,2).Value
    End With
    For Each e In Array("PCCombined_FF", "PCCombined_VB")
         With Sheets(e)
               For Each c In .Range("n4", .Range("n" & Rows.Count).End(xlUp))
                   c.Value = Chr(32) & c.Value & Chr(32)
               End With
               For i = LBound(myList,1) To UBound(myList,1)
                    .Columns("n").Replace What:=myList(i,1), _
                              Replacement:=myList(i,2), LookAt:=xlPart
               Next
               For Each c In .Range("N4",.Range("N" & Rows.Count).End(xlUp))
                    c.Value = Trim(c.Value)
               Next c
         End With
    Next
Application.ActiveSheet.Calculate
Application.ScreenUpdating = True
                  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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