Macro Save File Name With Specific Name

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all..
how to edit this macro code:
Excel Formula:
Sub SaveToFolder()
ChDir "C:\Test\"
MyName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=Sheets("Sheet1").Cells(1, 1) & " - " & MyName
End Sub
the macro working ok, i want to save file name like this:
LPK-3-014 (original file name)
hitung LPK-3-014 ok (after run macro)

thank in advance.
.sst
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Just change this line:
VBA Code:
ActiveWorkbook.SaveAs Filename:=Sheets("Sheet1").Cells(1, 1) & " - " & MyName
to this:
VBA Code:
ActiveWorkbook.SaveAs Filename:="hitung " & Sheets("Sheet1").Cells(1, 1) & " - " & MyName & " ok"
 
Upvote 0
hi
Just change this line:
VBA Code:
ActiveWorkbook.SaveAs Filename:=Sheets("Sheet1").Cells(1, 1) & " - " & MyName
to this:
VBA Code:
ActiveWorkbook.SaveAs Filename:="hitung " & Sheets("Sheet1").Cells(1, 1) & " - " & MyName & " ok"
Joe4,..your code not fully work
after run macro the result :
1.xlsx (original name file)
hitung - 1.xlsx ok (after run macro)- "the extension file is gone
 
Upvote 0
hitung - 1.xlsx ok (after run macro)- "the extension file is gone
Its there, it is just before the "ok".

Try this version:
VBA Code:
Sub SaveToFolder()
ChDir "C:\Test\"
MyName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
ActiveWorkbook.SaveAs Filename:="hitung " & Sheets("Sheet1").Cells(1, 1) & " - " & MyName & " ok.xlsx"
End Sub
 
Upvote 0
You are welcome.
 
Last edited:
Upvote 0
The code I gave you works specifically for files that have file extensions exactly 4 characters long. However, we can generalize so it would work for any 3 character file extensions (or any number, really), like this:
VBA Code:
Sub SaveToFolder()
    Dim e As Long
    Dim ext As String

    ChDir "C:\Test\"
    e = InStrRev(ActiveWorkbook.Name, ".")  'find position of last period in file name
    ext = Mid(ActiveWorkbook.Name, e + 1)  'extract file name extension
    MyName = Left(ActiveWorkbook.Name, e - 1)  'extract original file name without extension
    ActiveWorkbook.SaveAs Filename:="hitung " & Sheets("Sheet1").Cells(1, 1) & " - " & MyName & " ok." & ext
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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