Hi all,
I am new to macro but just want to see if anyone has any expertise on below. I ran this code on another set of data and it worked. just wondering if there is anything wrong with the codes, as i suspect there are some blank cells in my data set causing my run-time error 1004. appreciate all your effort as I am not really familiar with this excel environment, and have a nice day.
I am new to macro but just want to see if anyone has any expertise on below. I ran this code on another set of data and it worked. just wondering if there is anything wrong with the codes, as i suspect there are some blank cells in my data set causing my run-time error 1004. appreciate all your effort as I am not really familiar with this excel environment, and have a nice day.
VBA Code:
Option Explicit
Sub separate_by_cru()
Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet, wsData As Worksheet
Dim rng As Range, arCRU As Variant
Dim n As Long, iLastRow As Long
Dim foldername As String, sName As String
Set wb = ThisWorkbook
Set ws = wb.Sheets("CRU")
iLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
arCRU = ws.Range("A2:A" & iLastRow).Value2
Set wsData = wb.Sheets("Master")
iLastRow = wsData.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = wsData.Range("A1:Y" & iLastRow)
' make folder for workbooks
foldername = wb.Path & "\" & wb.Name & " " & Format(Now, "yyyy-mm-dd hh-mm-ss")
MkDir foldername
Application.ScreenUpdating = False
For n = 1 To UBound(arCRU)
sName = arCRU(n, 1)
Set wbNew = Workbooks.Add(xlWBATWorksheet) ' 1 sheet
wbNew.Sheets(1).Name = sName
rng.AutoFilter Field:=17, Criteria1:=sName
rng.Copy
wbNew.Sheets(1).Paste
wbNew.SaveAs foldername & "\" & sName & ".xlsx"
wbNew.Close False
Next
wsData.AutoFilterMode = False
Application.CutCopyMode = False
Application.ScreenUpdating = True
MsgBox n & " files created in " & foldername, vbInformation
End Sub
Last edited by a moderator: