randyharris
Board Regular
- Joined
- Oct 6, 2003
- Messages
- 88
Hi, I've got a piece of code I found online and it works awesome in Excel for Windows. It takes a long list of data and saves out separate files for each unique value in column A. However, when running it on Excel 2019 for Mac, it does not use the unique values in Column A for the file name, but rather Book1.xlsx, Book2,xlsx, etc...
I would really love to make this work properly in either platform if you could help! I think it may be the 'cell.Value' part that is working in Windows but not Mac - or maybe with the SaveAs command - unsure how to make it adaptable to work properly on both platforms.
Thanks!
I would really love to make this work properly in either platform if you could help! I think it may be the 'cell.Value' part that is working in Windows but not Mac - or maybe with the SaveAs command - unsure how to make it adaptable to work properly on both platforms.
Thanks!
Code:
'Split into different workbooks (files)
On Error Resume Next
Application.DisplayAlerts = False
Dim new_book As Workbook
Dim newsheet As Worksheet
With ThisWorkbook.Sheets("EPace Data") 'Replace the sheet name with the raw data sheet name
Set newsheet = ThisWorkbook.Sheets("cal")
If newsheet Is Nothing Then
Worksheets.Add.Name = "cal"
Else
ThisWorkbook.Sheets("cal").Delete
Worksheets.Add.Name = "cal"
End If
.Columns("a").Copy
With ThisWorkbook.Sheets("cal")
.Range("a1").PasteSpecial (xlPasteAll)
.Columns("a").RemoveDuplicates Columns:=1, Header:=xlYes
End With
For Each cell In ThisWorkbook.Sheets("cal").Columns("a").Cells
i = i + 1
If i <> 1 And cell.Value <> "" Then
.AutoFilterMode = False
.Rows(1).AutoFilter Field:=1, Criteria1:=cell.Value
Set new_book = Workbooks.Add
.UsedRange.Copy
new_book.Sheets(1).Range("a1").PasteSpecial (xlPasteAll)
new_book.SaveAs FileName:=ThisWorkbook.Path & "\" & cell.Value & ".xlsx"
new_book.Sheets(1).UsedRange.Columns.AutoFit
new_book.Save
new_book.Close
End If
Next cell
ThisWorkbook.Sheets("cal").Delete
End With