Hello,
I've been using the below code to separate items in a column into different worksheets, and it's been working really well. The only issue that I have now is that I'd like to make this macro universal so that it is available in different worksheets. When I add it to my personal workbook (which I've found makes the macro universal) it doesn't run correctly. It's my assumption that something in the coding is referencing the personal workbook instead of the workbook that I'm trying to run the macro in, but I'm not yet familiar enough with the coding to recognize where this is or know what to change it to. Can anyone point me in the right direction?
I've been using the below code to separate items in a column into different worksheets, and it's been working really well. The only issue that I have now is that I'd like to make this macro universal so that it is available in different worksheets. When I add it to my personal workbook (which I've found makes the macro universal) it doesn't run correctly. It's my assumption that something in the coding is referencing the personal workbook instead of the workbook that I'm trying to run the macro in, but I'm not yet familiar enough with the coding to recognize where this is or know what to change it to. Can anyone point me in the right direction?
Code:
Sub GenerateCSV()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
iCol = 20 '### Define your criteria column
strOutputFolder = "Leads 360" '### Define your path of output folder
Set ws = ThisWorkbook.ActiveSheet '### Don't edit below this line
Set rngLast = Columns(iCol).Find("*", Cells(1, iCol), , , xlByColumns, xlPrevious)
ws.Columns(iCol).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUnique = Range(Cells(2, iCol), rngLast).SpecialCells(xlCellTypeVisible)
If Dir(strOutputFolder, vbDirectory) = vbNullString Then MkDir strOutputFolder
For Each strItem In rngUnique
If strItem <> "" Then
ws.UsedRange.AutoFilter Field:=iCol, Criteria1:=strItem.Value
Workbooks.Add
ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy Destination:=[A1]
strFilename = strOutputFolder & "\" & strItem
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
End If
Next
ws.ShowAllData
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub