Public Sub Split_Sheet1_By_Country2()
Dim destFolder As String
Dim CountriesDict As Object 'Scripting.Dictionary
Dim CountryCell As Range, CountryKey As Variant
Dim filteredCells As Range
Dim CountryWorkbook As Workbook
Dim AutoFilterWasOn As Boolean
Dim DVcell As Range, DVlist As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the destination folder"
.InitialFileName = ActiveWorkbook.Path
.AllowMultiSelect = False
If Not .Show Then Exit Sub
destFolder = .SelectedItems(1) & "\"
End With
Application.ScreenUpdating = False
Set CountriesDict = CreateObject("Scripting.Dictionary") 'New Scripting.Dictionary
'Save current data validation list
Set DVcell = ActiveWorkbook.Worksheets("Sheet2").Range("B2")
DVlist = DVcell.Validation.Formula1
Application.ScreenUpdating = False
With ActiveWorkbook.Worksheets("Sheet1")
AutoFilterWasOn = .AutoFilterMode
'Create dictionary of unique Country values from column C
For Each CountryCell In .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
If Not CountriesDict.Exists(CountryCell.Value) Then CountriesDict.Add CountryCell.Value, 1
Next
'Autofilter column C by each Country and copy results to new workbooks
For Each CountryKey In CountriesDict.Keys
'Filter on column C to show only rows for this Country
.UsedRange.AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:="=" & CountryKey
Set filteredCells = .UsedRange.SpecialCells(xlCellTypeVisible)
'Copy filtered cells to new workbook
Set CountryWorkbook = Workbooks.Add(xlWBATWorksheet)
filteredCells.Copy CountryWorkbook.Worksheets(1).Range("A1")
'Add this Country to data validation list
DVcell.Validation.Modify Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=DVlist & "," & CountryKey
'Copy sheet containing data validation cell to the new workbook
DVcell.Parent.Copy After:=CountryWorkbook.Worksheets(1)
Application.DisplayAlerts = False 'suppress warning if file already exists
CountryWorkbook.SaveAs destFolder & "Data " & CountryKey & " 2023.xlsx", xlOpenXMLWorkbook
Application.DisplayAlerts = True
CountryWorkbook.Close False
Next
'Restore autofilter if it was on
.AutoFilter.ShowAllData
If Not AutoFilterWasOn Then .AutoFilterMode = False
'Restore data validation list
DVcell.Validation.Modify Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=DVlist
End With
Application.ScreenUpdating = True
MsgBox "Done"
End Sub