VBA code with cell.Value and SaveAs works in Windows, not Mac, help

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!

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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Just a quick glance at your code, but try replacing this:

& "" &

with this:

& Application.PathSeparator &

in your SaveAs line
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top