-1

I have a couple hundred excel workbooks all with the exact same format and structure. I need to prep each file to be processed later by:

  1. Deleting one row,
  2. Adding three columns
  3. Adding Placeholder text to one of the columns (this will be replaced later by the filename with a script)
  4. saving the file as tab delimited with the filename it originally had.(the purpose of the tab delimited change is that i want it in this format so i can replace the placeholder text in #3 with the filename)

Ideally I could open all of the workbooks I need to run this on and run the macro on each one, saving each file with its original name as a tab delimited file, with all of the changes i made in 1-3.

I keep getting a syntax error on this code:

Sub Macro4_eggplant()

    Dim newname as string
    newname = ThisWorkbook.FullName
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Columns("C:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "Filename.xls"
    Range("C2").Select
    Selection.AutoFill Destination:=Range("Table4[Column1]")
    Range("Table4[Column1]").Select
    Range("D5").Select

    ActiveWorkbook.SaveAs Filename:= _
        "/Users/matthewhughes/Desktop/" "newname" ".txt" _
        , FileFormat:=xlText, CreateBackup:=False
End Sub
Community
  • 1
  • 1
Mhughes
  • 3
  • 1
  • 5

1 Answers1

0

ThisWorkbook.FullName will return the fully qualified filename of the workbook containing your macros. Your following code (after fixing the syntax error, i.e. Desktop/" "newname" ".txt" should be Desktop/" & newname & ".txt") is expecting just the base filename (i.e. without path and extension), and should probably be referring to the ActiveWorkbook rather than the macro workbook.

Sub Macro4_eggplant()

    Dim newname as String
    With ActiveSheet
        newname = Left(.Parent.Name, InstrRev(.Parent.Name, ".") - 1)
        .Rows(1).Delete Shift:=xlUp
        .Columns("C:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range("C2").FormulaR1C1 = "Filename.xls"  ' or = .Parent.Name ?
        .Range("C2").AutoFill Destination:=.Range("Table4[Column1]")

        .Parent.SaveAs Filename:= "/Users/matthewhughes/Desktop/" & newname & ".txt", _
                       FileFormat:=xlText, _
                       CreateBackup:=False
    End With
End Sub

(I assume Table4 exists somewhere.)

YowE3K
  • 23,852
  • 7
  • 26
  • 40
  • Thanks for that explanation. I still seem to be getting an error though with: "Run-time error '1004': Cannot access 'filename.txt'." With [filename] being the name of the newly created file – Mhughes Sep 17 '16 at 00:05
  • @Mhughes Does the directory `/Users/matthewhughes/Desktop` exist? (The error you described will occur if you try to save a file to a non-existent directory, as I just found out the hard way when I tried to run the code after creating a temporary directory but making a mistake when I typed the directory name into the code.) Does the operating system you are running on require a drive letter to be specified as part of the path (.e.g I used "D:\Data\Temp\20160917_1314" as my path)? – YowE3K Sep 17 '16 at 03:26
  • I found the error! filename needed to be FileName :) – Mhughes Sep 19 '16 at 20:31
  • @Mhughes - But ... but ... but ... VBA is not case sensitive !?!?!?!? Is it?? – YowE3K Sep 19 '16 at 20:40
  • Sooo, http://stackoverflow.com/questions/2300983/is-vb-really-case-insensitive - Im using a mac, so maybe the compiler is not correcting for me.. – Mhughes Sep 19 '16 at 21:40
  • @Mhughes - everything on that question points to the fact that VBA treats variables case-insensitively. The IDE will try to automatically change the case of a variable to the case used in the Dim statement or, if there is no declaration statement, to the case that it was last typed in as. It **may** be different on a Mac, but it seems strange. (Note: a variable `filename` and the name of a named parameter such as `Filename:=` are different, and may have different cases.) – YowE3K Sep 19 '16 at 22:00