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
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,386
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Just a quick glance at your code, but try replacing this:

& "" &

with this:

& Application.PathSeparator &

in your SaveAs line
 

Watch MrExcel Video

Forum statistics

Threads
1,122,162
Messages
5,594,615
Members
413,917
Latest member
devansh02

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
Top