vba array autofilter code not working

Dancarro

Board Regular
Joined
Feb 23, 2013
Messages
65
Hi,

I have a vba code which autofilters an array of countries, and this works fine.

The only problems is that when a country is missing from the country column the vba code stops working. I would like some help in the code so that the code can move to the next country of the array filter.
Note: In some occasions there is no data for the country.

The code I have is the following:

Sub FilterCountry()
Dim sh As Worksheet, ws As Worksheet
Dim Wb As Workbook
Dim LstR As Long, i As Long
Dim Rng As Range
Dim Pth As String
Dim Ary As Variant


Pth = "H:\Version 6"
Ary = Array("Austria", "Austria.xlsm", "Country Billing Report", "Belgium", "Belgium.xlsm", "Country Billing Report", "Bulgaria", "Bulgaria.xlsm", "Country Billing Report", "Croatia", "Croatia.xlsm", "Country Billing Report")
Set sh = Sheets("Country report") 'set the sheet to filter
LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row

For i = 0 To UBound(Ary) Step 3
Set Wb = Workbooks.Open(Pth & Ary(i + 1))
Set ws = Wb.Sheets(Ary(i + 2))
Application.ScreenUpdating = False
With sh 'do something with the sheet
.Range("A3:Y3").AutoFilter Field:=5, Criteria1:=Ary(i)
Set Rng = .Range("A4:Y" & LstR).SpecialCells(xlCellTypeVisible) 'Replace Z with correct last column
Rng.Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
.AutoFilterMode = False
End With
Wb.Close True
Next i


End Sub

Many thanks,
Dan
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this


Code:
Sub FilterCountry()
    Dim sh As Worksheet, ws As Worksheet
    Dim Wb As Workbook
    Dim LstR As Long, i As Long
    Dim Rng As Range
    Dim Pth As String
    Dim Ary As Variant
    
    Pth = "H:\Version 6"
    Ary = Array("Austria", "Austria.xlsm", "Country Billing Report", _
                "Belgium", "Belgium.xlsm", "Country Billing Report", _
                "Bulgaria", "Bulgaria.xlsm", "Country Billing Report", _
                "Croatia", "Croatia.xlsm", "Country Billing Report")
    Set sh = Sheets("Country report") 'set the sheet to filter
    LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row
    
    For i = 0 To UBound(Ary) Step 3
        Set Wb = Workbooks.Open(Pth & Ary(i + 1))
        Set ws = Wb.Sheets(Ary(i + 2))
        Application.ScreenUpdating = False
        With sh 'do something with the sheet
            .Range("A3:Y3").AutoFilter Field:=5, Criteria1:=Ary(i)
[COLOR=#ff0000]            LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row[/COLOR]
[COLOR=#ff0000]            If LstR > 3 Then[/COLOR]
                Set Rng = .Range("A4:Y" & LstR).SpecialCells(xlCellTypeVisible) 'Replace Z with correct last column
                Rng.Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
[COLOR=#ff0000]            End If[/COLOR]
            .AutoFilterMode = False
        End With
        Wb.Close True
    Next i
End Sub
 
Upvote 0
Another option
Code:
      With sh 'do something with the sheet
         .Range("A3:Y3").AutoFilter Field:=5, Criteria1:=Ary(i)
         .AutoFilter.Range.Offset(1).Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
         .AutoFilterMode = False
      End With
 
Upvote 0
Hi Dante Amor,

I have a debug on this line:
Set Wb = Workbooks.Open(Pth & Ary(i + 1))

As I don't have data for Belgium, it stops here and it highlights in yellow the above line.

Is there anyway to code that if goes to the next country

Regards,
Dan
 
Upvote 0
Hi Dante Amor,

I have a debug on this line:
Set Wb = Workbooks.Open(Pth & Ary(i + 1))

As I don't have data for Belgium, it stops here and it highlights in yellow the above line.

Is there anyway to code that if goes to the next country

Regards,
Dan

Try this

Code:
Sub FilterCountry()
    Dim sh As Worksheet, ws As Worksheet
    Dim Wb As Workbook
    Dim LstR As Long, i As Long
    Dim Rng As Range, Ary As Variant
    Dim Pth As String, [COLOR=#ff0000]nBook As String[/COLOR]
    
    Pth = "H:\Version 6"
    Ary = Array("Austria", "Austria.xlsm", "Country Billing Report", _
                "Belgium", "Belgium.xlsm", "Country Billing Report", _
                "Bulgaria", "Bulgaria.xlsm", "Country Billing Report", _
                "Croatia", "Croatia.xlsm", "Country Billing Report")
    Set sh = Sheets("Country report") 'set the sheet to filter
    LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row
    
    For i = 0 To UBound(Ary) Step 3
[COLOR=#ff0000]        nBook = Pth & Ary(i + 1)[/COLOR]
[COLOR=#ff0000]        If Dir(nBook) <> "" Then[/COLOR]
            Set Wb = Workbooks.Open(Pth & Ary(i + 1))
            Set ws = Wb.Sheets(Ary(i + 2))
            Application.ScreenUpdating = False
            With sh 'do something with the sheet
                .Range("A3:Y3").AutoFilter Field:=5, Criteria1:=Ary(i)
                LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row
                If LstR > 3 Then
                    Set Rng = .Range("A4:Y" & LstR).SpecialCells(xlCellTypeVisible) 'Replace Z with correct last column
                    Rng.Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
                End If
                .AutoFilterMode = False
            End With
            Wb.Close True
[COLOR=#ff0000]        End If[/COLOR]
    Next i
End Sub
 
Upvote 0
How about
Code:
Sub FilterCountry()
   Dim sh As Worksheet, ws As Worksheet
   Dim Wb As Workbook
   Dim LstR As Long, i As Long
   Dim Rng As Range
   Dim Pth As String
   Dim Ary As Variant
   
   
   Pth = "H:\Version 6"
   Ary = Array("Austria", "Austria.xlsm", "Country Billing Report", "Belgium", "Belgium.xlsm", "Country Billing Report", "Bulgaria", "Bulgaria.xlsm", "Country Billing Report", "Croatia", "Croatia.xlsm", "Country Billing Report")
   Set sh = Sheets("Country report") 'set the sheet to filter
   LstR = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row 'find last row
   
   For i = 0 To UBound(Ary) Step 3
      Set Wb = Nothing
      On Error Resume Next
      Set Wb = Workbooks.Open(Pth & Ary(i + 1))
      On Error GoTo 0
      If Not Wb Is Nothing Then
         Set ws = Wb.Sheets(Ary(i + 2))
         Application.ScreenUpdating = False
         With sh 'do something with the sheet
            .Range("A3:Y3").AutoFilter Field:=5, Criteria1:=Ary(i)
            .AutoFilter.Range.Offset(1).Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
            .AutoFilterMode = False
         End With
         Wb.Close True
      End If
   Next i
End Sub
 
Upvote 0
Hi Fluff and Dante Amor,

Fluff your last code worked perfectly, fantastic.

@ Dante Amor: I tried your code too but nothing really happened. It was looping from nBook to Next i, and so on

I would like to thank you both for your help, much appreciated your assistance.

Kind Regards,
Dan
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
The backslash is missing

Code:
[COLOR=#333333]Pth = "H:\Version 6[/COLOR][B][COLOR=#ff0000]\[/COLOR][/B][COLOR=#333333]"[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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