+ Reply to Thread
Results 1 to 4 of 4

How do I copy a file to the same directory?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-07-2013
    Location
    Milwaukee, Wi
    MS-Off Ver
    Excel 2013
    Posts
    5

    How do I copy a file to the same directory?

    Hey everybody,

    I've been using Excel for awhile now, and recently starting using vba. I think I'm getting in over my head, but I wanted to give it a shot. What I want to do with this macro is copy a file to the same directory using the same file name, but adding the text from an input box to the end of it with an underscore. Here it is...

    Sub Macro3()
    
     Dim strOrder As String
     Dim varGetPath As Variant
     
       strOrder = InputBox("Enter the Order number.")
       varGetPath = ThisWorkbook.FullName
       
        ThisWorkbook.SaveAs FileName:= _
            ("varGetpath" & "_strOrder"), FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
            
    End Sub
    A couple of unexpected things are happening here. 1. The file is being saved as "varGetpath_strOrder.xlsm" and not as the file name with the inputbox text and 2. It's saving in my C:/Users/Libraries/Documents folder instead of the desktop folder it originated from. I thought it would be pretty simple to do this, but I'm sure I'm missing a good chunk of code. I'm using the new office 365.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do I copy a file to the same directory?

    Anytime you put " " around something, Excel treats it simply as text you want to use. Try removing the " " around your variables so the values IN the variables will be used instead.

    Also, you're inserting a numeric value INTO the middle of the filename, so you'll need to get specific about what goes where. So maybe this, and I'm using the SaveCopyAs method so as to not affect the original file.

    Sub Macro3()
    Dim strOrder As String, fPATH As String, fNAME As String
    
        strOrder = Application.InputBox("Enter the Order number.", "Number", Type:=1)
        If strOrder = 0 Then Exit Sub
        fPATH = ThisWorkbook.Path & Application.PathSeparator
        fNAME = Replace(ThisWorkbook.Name, ".", "_" & strOrder & ".")
    
        ThisWorkbook.SaveCopyAs Filename:=fPATH & fNAME
            
    End Sub
    Last edited by JBeaucaire; 07-07-2013 at 01:52 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-07-2013
    Location
    Milwaukee, Wi
    MS-Off Ver
    Excel 2013
    Posts
    5

    Re: How do I copy a file to the same directory?

    It worked, sort of. The file was saved in the correct directory (yes! Thank you!), but the input box text is being added to the file name after the .xlsm. Hmmm...

  4. #4
    Registered User
    Join Date
    07-07-2013
    Location
    Milwaukee, Wi
    MS-Off Ver
    Excel 2013
    Posts
    5

    Re: How do I copy a file to the same directory?

    Hey, I just tried your code, and it worked just the way I needed it to. Thank You!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1