Need to add a loop into current macro

imeade

New Member
Joined
Jun 28, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I have the following code that works well for the static "LenderCode" reference; however, I need the macro to loop through the "LenderCode" range of "B4:B28" and run the same code for each value in the range

Sub Filter()

Dim i As Integer
Dim rngData As Range
Dim LenderCode As Range


Sheets("Data").Select
ActiveSheet.Range("a1").Select

Set rngData = Range("A1").CurrentRegion
i = Application.WorksheetFunction.Match("Reference-2", Range("A1:BZ1"), 0)

Set LenderCode = Sheets("Reference").Range("B4")

rngData.AutoFilter Field:=i, Criteria1:=LenderCode

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste

End Sub



Thanks!
Ian
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Ian,

what about

VBA Code:
Sub Filter()

Dim lngMatch As Long
Dim rngData As Range
Dim rngWork As Range
Dim rngCell As Range
Dim wsData As Worksheet

Set wsData = Sheets("Data")
Set rngData = wsData.Range("A1").CurrentRegion
Set rngWork = Sheets("Reference").Range("B4:B28")

lngMatch = Application.WorksheetFunction.Match("Reference-2", wsData.Range("A1:BZ1"), 0)
For Each rngCell In rngWork
  With wsData
    rngData.AutoFilter Field:=lngMatch, Criteria1:=rngCell.Value
    rngData.SpecialCells(xlCellTypeVisible).Copy
    Sheets.Add
    ActiveSheet.Paste
  End With
Next rngCell

Application.CutCopyMode = False
Set rngWork = Nothing
Set rngData = Nothing
wsData.AutoFilterMode = False
Set wsData = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Hi Ian,

what about

VBA Code:
Sub Filter()

Dim lngMatch As Long
Dim rngData As Range
Dim rngWork As Range
Dim rngCell As Range
Dim wsData As Worksheet

Set wsData = Sheets("Data")
Set rngData = wsData.Range("A1").CurrentRegion
Set rngWork = Sheets("Reference").Range("B4:B28")

lngMatch = Application.WorksheetFunction.Match("Reference-2", wsData.Range("A1:BZ1"), 0)
For Each rngCell In rngWork
  With wsData
    rngData.AutoFilter Field:=lngMatch, Criteria1:=rngCell.Value
    rngData.SpecialCells(xlCellTypeVisible).Copy
    Sheets.Add
    ActiveSheet.Paste
  End With
Next rngCell

Application.CutCopyMode = False
Set rngWork = Nothing
Set rngData = Nothing
wsData.AutoFilterMode = False
Set wsData = Nothing
End Sub

Ciao,
Holger
Thanks! However, there are blank cells in the range so I would need the loop to not do anything if the reference cell is blank
 
Upvote 0
Hi Ian,

add a check like

VBA Code:
For Each rngCell In rngWork
  If rngCell.Value <> "" Then
    With wsData
      rngData.AutoFilter Field:=lngMatch, Criteria1:=rngCell.Value
      rngData.SpecialCells(xlCellTypeVisible).Copy
      Sheets.Add
      ActiveSheet.Paste
    End With
  End If
Next rngCell

Holger
 
Upvote 0
Hi Ian,

add a check like

VBA Code:
For Each rngCell In rngWork
  If rngCell.Value <> "" Then
    With wsData
      rngData.AutoFilter Field:=lngMatch, Criteria1:=rngCell.Value
      rngData.SpecialCells(xlCellTypeVisible).Copy
      Sheets.Add
      ActiveSheet.Paste
    End With
  End If
Next rngCell

Holger
Fantastic, thank you. Can I please ask for one more addition. I would like the newly created sheet to be renamed to the respective reference that is in "Reference C4:C28".

Thanks again
 
Upvote 0
Hi Ian,

code takes care if a sheet with the name of the cell exists. So any exisitng sheet will be cleared before copying over, new sheets will be added if needed:

VBA Code:
Sub Filter_mod3()
' https://www.mrexcel.com/board/threads/need-to-add-a-loop-into-current-macro.1227580/
'add sheets if not already in workbook
Dim lngMatch As Long
Dim rngData As Range
Dim rngWork As Range
Dim rngCell As Range
Dim wsData As Worksheet
Dim wsTarg As Worksheet

Set wsData = Sheets("Data")
Set rngData = wsData.Range("A1").CurrentRegion
Set rngWork = Sheets("Reference").Range("B4:B28")

lngMatch = Application.WorksheetFunction.Match("Reference-2", wsData.Range("A1:BZ1"), 0)
For Each rngCell In rngWork
  If rngCell.Value <> "" Then
    With wsData
      If Not Evaluate("ISREF('" & rngCell.Value & "'!A1)") Then
        Set wsTarg = Sheets.Add
        wsTarg.Name = rngCell.Value
      Else
        Set wsTarg = Sheets(rngCell.Value)
        wsTarg.Cells.Clear
        Application.Goto wsTarg.Cells(1, 1)
      End If
      rngData.AutoFilter Field:=lngMatch, Criteria1:=rngCell.Value
      rngData.SpecialCells(xlCellTypeVisible).Copy
      wsTarg.Paste
    End With
  End If
Next rngCell

Application.CutCopyMode = False
Set rngWork = Nothing
Set rngData = Nothing
wsData.AutoFilterMode = False
Set wsData = Nothing
End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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