Conell8383
Board Regular
- Joined
- Jul 26, 2016
- Messages
- 66
Hi All I hope you can help.
What I am trying to achieve is for my Macro or code to look through column F. When it finds a country then create a new sheet name it after that country then copy all the information for that country from the Original sheet in this case 'TOV Full Extract with correct D' then look at column F again find the next country create a new sheet name if after the country copy in all the data and so on until the end of column F.
the code i have so far is below
I am getting a run time error 9 on line
I have also attached a picture of my Excel sheet any help would be greatly appreciated.
What I am trying to achieve is for my Macro or code to look through column F. When it finds a country then create a new sheet name it after that country then copy all the information for that country from the Original sheet in this case 'TOV Full Extract with correct D' then look at column F again find the next country create a new sheet name if after the country copy in all the data and so on until the end of column F.
the code i have so far is below
I am getting a run time error 9 on line
Code:
Set ws1 = Worksheets("Sheet1")

I have also attached a picture of my Excel sheet any help would be greatly appreciated.
Code:
Option Explicit
Sub Filter()
Dim wsCL As Worksheet
Set wsCL = Worksheets("TOV Full Extract with correct D")
Dim rCL As Range, rCountry As Range
Set rCL = wsCL.Range("A1:A201")
Dim ws1 As Worksheet
Set ws1 = Worksheets("Sheet1")
Dim lRow As Long
lRow = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
For Each rCountry In rCL
'check if country exists
Dim rTest As Range
Set rTest = ws1.Range("F1:F" & lRow).Find(rCountry.Value2, lookat:=xlWhole)
If Not rTest Is Nothing Then 'if country is found create sheet and copy data
Dim wsNew As Worksheet
Worksheets.Add (ThisWorkbook.Worksheets.Count)
Set wsNew = ActiveSheet
wsNew.Name = rCountry.Value2
ws1.Range("A1:Q1").Copy wsNew.Range("A1") 'place header row
With ws1.Range("A1:Q" & lRow)
.AutoFilter 10, rCountry.Value2
.Offset(1).SpecialCells(xlCellTypeVisible).Copy wsNew.Range("B1") 'copy data for country under header
.AutoFilter
End With
End If
Next
End Sub