1

I have 119 .csv files. I want to place them all on one worksheet and place the file name in every cell of a column for data coming from that file. So every line brought over from that file with have the file name in column A.

I am using MAC Excel 16. The path structure in MAC OS is different than Windows. I recorded a macro loading data from one csv file and the path generated is shown below.

The code I found:

Sub ImportCSV()
'Author:    Jerry Beaucaire
'Date:      10/16/2010
'Summary:   Import all CSV files from a folder into a single sheet
'           adding a field in column A listing the CSV filenames

Dim wbCSV   As Workbook
Dim wsMstr  As Worksheet:   
Dim fPath   As String:          
Dim fCSV    As String
Set wsMstr = ThisWorkbook.Sheets("Sheet1")

fPath = "/Users/bensimmons/Downloads/GMI/"

Application.ScreenUpdating = False 
fCSV = Dir(fPath & "*.csv")

Do While Len(fCSV) > 0
     
    Set wbCSV = Workbooks.Open(fPath & fCSV)
     
    Columns(1).Insert xlShiftToRight

    Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name

    ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
    wbCSV.Close False

    fCSV = Dir
Loop
 
Application.ScreenUpdating = True
End Sub

I am receiving a Compile error:

"Sub or Function not defined (ThisWorkbook 7:26)"

Thanks to the comments below I've updated the code:

Sub ImportCSV()

Dim wbCSV   As Workbook
Dim wsMstr  As Worksheet:
Dim fPath   As String:
Dim fCSV    As String
Set wsMstr = ThisWorkbook.Sheets("Sheet1")
fPath = "/Users/bensimmons/Downloads/GMI/"
Application.ScreenUpdating = False 
fCSV = Dir(fPath, MacID("CSV"))
Do While Len(fCSV) > 0
    Set wbCSV = Workbooks.Open(fPath & fCSV)
    Columns(1).Insert xlShiftToRight
    Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
    ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
    wbCSV.Close False
    fCSV = Dir
Loop

Application.ScreenUpdating = True
End Sub

I'm not getting any errors but nothing is happening when I run the macro.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ben Simmons
  • 329
  • 1
  • 6
  • 17
  • If I'm not mistaken, the [Dir function](https://msdn.microsoft.com/en-us/library/office/gg278779.aspx) is not compatible with a OSX file system. That's actually the eighth code line by my count and it's 27 characters wide not 26 but it seems to be the culprit.. –  Sep 27 '15 at 05:04
  • Ah- good catch. I'm wondering if it should be "ls" instead of Dir. I'll test out and get back. – Ben Simmons Sep 27 '15 at 05:16
  • No- "ls" didn't fix it. – Ben Simmons Sep 27 '15 at 05:18
  • 1
    Seems to be the `Dir(strPath, MacID("TEXT"))` and possibly wildcards. See [this](http://stackoverflow.com/questions/10045474/dir-function-not-working-in-mac-excel-2011-vba). –  Sep 27 '15 at 05:20
  • Why is the updated code not producing errors but not performing any actions? – Ben Simmons Sep 27 '15 at 11:39
  • @BenSimmons did the new code enter the loop? You can check that by inserting a breakpoint inside it and check the value of fCSV – A.S.H Sep 27 '15 at 12:03

0 Answers0