PDA

View Full Version : Save As button using VBA Code



gary2014
04-13-2015, 10:03 AM
Hi,
I have created 50 excel sheet in which I have set 4 Buttons in each sheet.
1. Reset
2. Korfball
3. Sepaktakraw
4. Kabbadi

13160
Now I want to add another button "Save as" in all 50 sheets.

Once the user clicks on the "Save as" button in one sheet, only that particular sheet should be saved on desktop or in G Drive by name "MCR" and date.

Please help me with this.

mperrah
04-13-2015, 03:27 PM
See if this is close to what you are looking for.

Sub vba52280()
Dim preName As String, sPath As String, ThisIsNow As String, sFileName As String

preName = "MCR"
sPath = "G:\"
ThisIsNow = Format(Now(), "yyyymmdd")
sFileName = preName & ThisIsNow & ".xlsm"
MsgBox sFileName ' can be omitted

With ActiveSheet
.Select
.Copy
ActiveWorkbook.SaveAs sPath & sFileName, FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End With
End Sub


-mark
(nod to Yongle for source)

gary2014
04-14-2015, 04:54 AM
Hi,
Many thanks for the reply. I need little more of your guidance in this regard.

I am not sure if I am doing it correct. Let put it in a step by step manner of what I did.

Step 1: In the excel sheet, I clicked on Developer tab > Insert > Command Button.
13165




Step 2: Renamed the command button as "SaveAs".
Step 3: Right click on "SaveAs" button and then click on View Code.
(The following appears)

13166

Please guide me if I am doing it incorrect.

Thank you.

Yongle
04-14-2015, 05:40 AM
That looks correct. Just put the code in below the line beginning Private Sub (do not change the name of the sub)

gary2014
04-14-2015, 06:07 AM
Hi,
It gives an error which is as follows:

13170

Yongle
04-14-2015, 06:45 AM
Just delete line 2 (beginning with Sub VBA...)

gary2014
04-14-2015, 06:58 AM
Excellent!
Its working now.
Thank you very much.

Yongle
04-14-2015, 08:54 AM
Great news - please click on Thread Tools (at top of thread) and mark the thread as "Solved"