Shorten this copy with a loop?

paldob

New Member
Joined
Apr 23, 2018
Messages
28
I have written the below code in a length way. I'm not too sure how to write loops at the moment, I'm still learning this and it's taking me longer than I expected to understand how.

Is it possible to shorten my below code with a loop?

Code:
Sub NameFilter()

Dim Name1 As String
Dim Name2 As String
Dim Name3 As String
Dim Name4 As String
Dim Name5 As String
Dim Name6 As String


Name1 = ThisWorkbook.Sheets("Home").Range("A1").Value
Name2 = ThisWorkbook.Sheets("Home").Range("A2").Value
Name3 = ThisWorkbook.Sheets("Home").Range("A3").Value
Name4 = ThisWorkbook.Sheets("Home").Range("A4").Value
Name5 = ThisWorkbook.Sheets("Home").Range("A5").Value
Name6 = ThisWorkbook.Sheets("Home").Range("A6").Value


    With Sheets("Home")
    
        If Name1.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name1
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name1
        
        If Name2.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name2
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name2
        
        If Name3.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name3
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name3
        
        If Name4.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name4
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name4
        
        If Name5.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name5
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name5
        
        If Name6.Value = "" Then Exit Sub
        .Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=Name6
        
        ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & Name6
        
    End With
    
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Here's one way (untested) :
Code:
Sub NameFilter()
Dim rng As Range, cel As Range
Set rng = ThisWorkbook.Sheets("Home").Range("A1:A6")
For i = 1 To rng.Cells.Count
    If rng(i) = "" Then Exit Sub
    Sheets("Home").Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=rng(i)
    ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & rng(i)
Next
End Sub
 
Upvote 0
Amended :
Code:
Sub NameFilter()
Dim rng As Range,[COLOR=#ff0000] i%[/COLOR]
Set rng = ThisWorkbook.Sheets("Home").Range("A1:A6")
For i = 1 To rng.Cells.Count
    If rng(i) = "" Then Exit Sub
    Sheets("Home").Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=rng(i)
    ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & rng(i)
Next
End Sub

Another way :
Code:
Sub NameFilter()
Dim rng As Range, cel As Range
Set rng = ThisWorkbook.Sheets("Home").Range("A1:A6")
For Each cel In rng
    If cel = "" Then Exit Sub
    Sheets("Home").Range("$B$10:$O$39").AutoFilter Field:=10, Criteria1:=cel
    ThisWorkbook.SaveCopyAs Filename:=ThisWorkook.Name & cel
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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