Excel Save to code

Monster1979

New Member
Joined
May 26, 2013
Messages
3
Hi everybody.

I have made an invoice in excel that make a new invoice number when opening etc. Everything is working fine but i need to save the invoice in a specified folder, which is also working fine. My problem is when i copy my whole work folder to usb and work on it from my laptop it still saves that file "With the written code" To D:\Drive and not to the given usb I:\ drive. Is there any code that i can write to save it to the current drive you are working from. Here is a sample of my code.

Private Sub Workbook_AfterSave(ByVal Success As Boolean)


ActiveSheet.Copy
NEWFN = "D:\Manie Hansen\Invoices\" & "Invoice " & Range("M11").Value & " " & Range("E19").Value & ".xlsx"
ActiveWorkbook.SaveAs NEWFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close


End Sub

What i want is for the d:\Manie Hansen\..... to change to whatever drive number the USB get on another pc. For example I:\Manie Hansen. without having to change the code from pc to pc.

Thank You:)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello.

Save to pathdir where your workbook is stored in:

Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)

ActiveSheet.Copy
NEWFN = Application.Path & "\" & "Invoice " & Range("M11").Value & " " & Range("E19").Value & ".xlsx"
ActiveWorkbook.SaveAs NEWFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close

End Sub
 
Upvote 0
Hi There thanks a lot it, it now save to the c:\ Drive and not the usb but i see what direction i need to go to. Thank you very much.
 
Upvote 0
This function returns a collection of USB drive's letters.
Code:
Public Function GetUSBDrives() As Collection
    Dim drive As Object, fso As Object
    Dim pUSBCol As New Collection
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each drive In fso.Drives
        If (drive.DriveType = 1) And drive.IsReady Then
            pUSBCol.Add drive.DriveLetter
        End If
    Next
End Function
 
Upvote 0
Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)

ActiveSheet.Copy
NEWFN = ThisWorkbook.Path & "\" & "Invoice " & Range("M11").Value & " " & Range("E19").Value & ".xlsx"
ActiveWorkbook.SaveAs NEWFN, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close

End Sub

ThisWorkbook.Path will take the dir() where your file is stored. When this is on USB stick it will take this.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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