VBA SaveAs

LFZ12

New Member
Joined
Sep 27, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a pretty simple Macro to open a txt file, clean it up and Save As a .xlsm in its original location. I recorded the macro on one file, I cant figure out how to edit it so it will pull the current file name, not the one I recorded the macro with. Can anyone help? Do I have to insert a module or procedure or can I just edit what I have? The green text is what I need to be changed. I'm new to this and still trying to teach myself. Thank you!

Rich (BB code):
Sub Macro33()
'
' Macro33 Macro
'

'
    ChDir "C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022"
    Workbooks.OpenText Filename:= _
        "C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\*PM*.txt", _
        Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(3, 1), Array(10, 1), Array(35, 1), Array(41, 1), Array(105, 1), Array( _
        116, 1)), TrailingMinusNumbers:=True
    Cells.Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$G$7285").AutoFilter Field:=1, Criteria1:=Array( _
        "BID", "SID", "TOT"), Operator:=xlFilterValues
    Selection.Delete Shift:=xlUp
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("C:C").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:F").Select
    Selection.Delete Shift:=xlToLeft
    Range("D1").Select
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\PM11.12.22.xlsm", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    Application.Left = 334
    Application.Top = 133.6
    ActiveWindow.Close
End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Right after this "Open file" command:
VBA Code:
Workbooks.OpenText Filename:= _
"C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\*PM*.txt", _
Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(3, 1), Array(10, 1), Array(35, 1), Array(41, 1), Array(105, 1), Array( _
116, 1)), TrailingMinusNumbers:=True
you can capture the name of the active file and change the extension from "txt" to "xlsm" like this:
VBA Code:
    Dim fName As String
    fName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 3) & "xlsm"
    MsgBox fName
(that MsgBox is not needed in your final code, it is just there to show you what we built).

You can then use it in your Save command, i.e.
VBA Code:
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\" & fName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
 
Upvote 0
Solution
Right after this "Open file" command:
VBA Code:
Workbooks.OpenText Filename:= _
"C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\*PM*.txt", _
Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(3, 1), Array(10, 1), Array(35, 1), Array(41, 1), Array(105, 1), Array( _
116, 1)), TrailingMinusNumbers:=True
you can capture the name of the active file and change the extension from "txt" to "xlsm" like this:
VBA Code:
    Dim fName As String
    fName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 3) & "xlsm"
    MsgBox fName
(that MsgBox is not needed in your final code, it is just there to show you what we built).

You can then use it in your Save command, i.e.
VBA Code:
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\GZall\Desktop\Laura's Project\Zall Stuff\2022\" & fName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Thank You! Perfect!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,085
Messages
6,123,030
Members
449,092
Latest member
ikke

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