Need help making macro work for Mac users

bchaabi

New Member
Joined
Oct 1, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I found a macro online that allows you to select several text (.txt) files, and add them to your current workbook as extra sheets. I need help making it work for mac users.

This code allows me to open library on macs, but I can't select text files. I believe this is the line of the code I have to change :
VBA Code:
" {""public.comma-separated-values-text""} " & _

but when I try to change it it just doesn't work. I also don't have a mac and I always have to get someone with a mac to test it for me.

VBA Code:
Sub CombineTextFiles()

    Dim FilesToOpen
    Dim x As Integer
    Dim wkbAll As Workbook
    Dim wkbTemp As Workbook
    Dim sDelimiter As String
    Dim answer As Integer
    
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    answer = MsgBox("Before moving forward, all other workbooks must be closed" _
    & vbCrLf & "Do you wish to continue?", vbYesNo + vbQuestion)
    
    If answer = vbYes Then 'do nothing
    Else: Exit Sub
    End If

    sDelimiter = ","

    #If Mac Then
    FilesToOpen = Select_File_Or_Files_Mac()
    #Else
    FilesToOpen = Application.GetOpenFilename(fileFilter:="Text Files (*.txt), *.txt", _
      MultiSelect:=True, Title:="Select the CDR Text Files to Open")
    #End If
    
'    FilesToOpen = Application.GetOpenFilename _
'      (fileFilter:="Text Files (*.txt), *.txt", _
'      MultiSelect:=True, Title:="Select the CDR Text Files to Open")

    If TypeName(FilesToOpen) = "Boolean" Then
        MsgBox "No Files were selected"
        GoTo ExitHandler
    End If

    x = 1
    Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
    wkbTemp.Sheets(1).Copy
    Set wkbAll = ActiveWorkbook
    wkbTemp.Close (False)
    wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
      Destination:=Range("A1"), DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, _
      Tab:=False, Semicolon:=False, _
      Comma:=False, Space:=False, _
      Other:=True, OtherChar:=","
    x = x + 1

    While x <= UBound(FilesToOpen)
        Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
        With wkbAll
            wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
            .Worksheets(x).Columns("A:A").TextToColumns _
              Destination:=Range("A1"), DataType:=xlDelimited, _
              TextQualifier:=xlDoubleQuote, _
              ConsecutiveDelimiter:=False, _
              Tab:=False, Semicolon:=False, _
              Comma:=False, Space:=False, _
              Other:=True, OtherChar:=sDelimiter
        End With
        x = x + 1
    Wend
    
wkbAll.Sheets.Copy After:=Workbooks(2).Sheets(Workbooks(2).Worksheets.Count)
wkbAll.Close False



ExitHandler:
    Application.ScreenUpdating = True
    Set wkbAll = Nothing
    Set wkbTemp = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler


End Sub

Function Select_File_Or_Files_Mac() As String()
    Dim MyPath As String
    Dim MyScript As String
    Dim MyFiles As String
    Dim MySplit As Variant
    Dim N As Long
    Dim FName As String
    Dim mybook As Workbook

    On Error Resume Next
    MyPath = MacScript("return (path to documents folder) as String")
    'Or use MyPath = "Macintosh HD:Users:Ron:Desktop:TestFolder:"

    ' In the following statement, change true to false in the line "multiple
    ' selections allowed true" if you do not want to be able to select more
    ' than one file. Additionally, if you want to filter for multiple files, change
    ' {""com.microsoft.Excel.xls""} to
    ' {""com.microsoft.excel.xls"",""public.comma-separated-values-text""}
    ' if you want to filter on xls and csv files, for example.
    MyScript = _
    "set applescript's text item delimiters to "","" " & vbNewLine & _
               "set theFiles to (choose file of type " & _
             " {""public.comma-separated-values-text""} " & _
               "with prompt ""Please select a file or files"" default location alias """ & _
               MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
               "set applescript's text item delimiters to """" " & vbNewLine & _
               "return theFiles"

    MyFiles = MacScript(MyScript)
    Dim returnList() As String
    On Error GoTo 0

    If MyFiles <> "" Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With

        'MsgBox MyFiles
        MySplit = Split(MyFiles, ",")
        ReDim returnList(LBound(MySplit) To UBound(MySplit))
        For N = LBound(MySplit) To UBound(MySplit)

            returnList(N) = MySplit(N)

        Next N
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        Select_File_Or_Files_Mac = returnList
    Else
        ReDim returnList(0 To 0)
        returnList(0) = "False"
        Select_File_Or_Files_Mac = returnList
    End If
End Function
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,217,143
Messages
6,134,868
Members
449,895
Latest member
rainbow1112

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