Macro to "save as"

bluepenink

Well-known Member
Joined
Dec 21, 2010
Messages
585
Hello

Can someone pls help me with a macro to create new workbooks?

i want to save a workbook from cell N3 on worksheet called "Branch"

i want to path to be saved on desktop in a folder called "Bonus"

-the file name needs to be in the following way:

i.e.
Employee name from N3 THEN (vlookup on SE list for district) THEN V.1.0 THEN (today's date)

for example > save as >excel 03 > file name
John Doe (New York-Long Island) V.1.0 (Sep 6, 2011)

I have the district the person belongs to in the worksheet "SE List", so if you can pls help me with this macro that would be great, as there are over 100 employees i have to do this for in my main sheet. also, if you can define the name so if i expand teh selection, it would not throw off the macro.

pls and thx u so much!

Code:
Option Explicit
 
Sub SaveAsSpecial()
 
With ActiveWorkbook.Sheets("Bonus")
If .[N3] <> "" Then
ActiveWorkbook.SaveAs .[N3] & " (" & _
Application.VLookup(N3, Sheets("SE List").Range("C:E"), 3, 0) _
& ") V.1.0 (" & Format(Date, "MMM D, YYYY") & ").xls", xlNormal
End If
End With
 
End Sub

the above macro is not working and nor is saving a file in a folder...can u pls help
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
Sub SaveAsSpecial()
    
    Dim Employee As String, rngDistrict As Range
    
    Employee = ActiveWorkbook.Sheets("Bonus").Range("N3").Value
    
    If Employee = "" Then
        MsgBox "'Bonus'!N3 is empty.", vbExclamation, "No Employee"
    Else
        Set rngDistrict = Sheets("SE List").Range("C:C").Find(What:=Employee, _
                                                              LookIn:=xlValues, _
                                                              LookAt:=xlWhole, _
                                                              MatchCase:=False)
        If rngDistrict Is Nothing Then
            MsgBox "Cannot match """ & Employee & """ on sheet ""SE List""", _
                    vbExclamation, "No Employee Match"
        Else
            ActiveWorkbook.SaveAs Employee & " (" & rngDistrict.Offset(, 2).Value & _
                                  ") V.1.0 (" & Format(Date, "MMM D, YYYY") & ").xls", xlNormal
            MsgBox ActiveWorkbook.FullName, _
                    vbInformation, "Save Complete"
        End If
    End If
    
End Sub
 
Upvote 0
You have to declare your variables. I would record a macro and fill in all the stuff that is the same on the sheet and then edit the macro for the save as part of the code. Test each part of the code so you know what works. That would help a lot. I am new to this myself too but just keep at it. There is no better feeling than to say, "I did that".
Bill C
 
Last edited:
Upvote 0
Code:
Sub SaveAsSpecial()
 
    Dim Employee As String, rngDistrict As Range
 
    Employee = ActiveWorkbook.Sheets("Bonus").Range("N3").Value
 
    If Employee = "" Then
        MsgBox "'Bonus'!N3 is empty.", vbExclamation, "No Employee"
    Else
        Set rngDistrict = Sheets("SE List").Range("C:C").Find(What:=Employee, _
                                                              LookIn:=xlValues, _
                                                              LookAt:=xlWhole, _
                                                              MatchCase:=False)
        If rngDistrict Is Nothing Then
            MsgBox "Cannot match """ & Employee & """ on sheet ""SE List""", _
                    vbExclamation, "No Employee Match"
        Else
            ActiveWorkbook.SaveAs Employee & " (" & rngDistrict.Offset(, 2).Value & _
                                  ") V.1.0 (" & Format(Date, "MMM D, YYYY") & ").xls", xlNormal
            MsgBox ActiveWorkbook.FullName, _
                    vbInformation, "Save Complete"
        End If
    End If
 
End Sub


hello thx u so much ..makes sense, i appreciate the prompt response.

quick question, how can i change the path to where the file is saved?
thxs again bud!
 
Upvote 0
Put something like this before you save as.
Code:
ChDir "C:\MyFolder\MySubfolder\"
This doesn't change drive letters. Use ChDrive to do that.


Or include the path in the save as
Code:
strMyPath = "C:\MyFolder\MySubfolder\"
ActiveWorkbook.SaveAs [COLOR="Red"]strMyPath &[/COLOR] Employee & " (" & rngDistrict.Offset(, 2).Value & _
                                  ") V.1.0 (" & Format(Date, "MMM D, YYYY") & ").xls", xlNormal
 
Upvote 0
Alphafrog,
I am impressed at what you have done in this code and what incredible things excel can do. Good work!

Bill C
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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