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
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