+ Reply to Thread
Results 1 to 9 of 9

Referring to one cell as a filename, and save it.

  1. #1
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Referring to one cell as a filename, and save it.

    Hello!

    I am trying to make a macro which is doing the following.

    1. Defining a standard path:
    C:\test\macro

    2. open a *.txt file within this path, the name of the file is given in cell "a1".

    3. Save it with a new filename, the filename is given in cell "b1".

    Anyone knows how to do this? And does it matter if the filetype varies?

    Cheers

  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: Referring to one cell as a filename, and save it.

    Do you need to open the file or are you just trying to rename/move the original file?
    Or are you trying to create a second copy of the file with a new name, but it's still a text file?
    Or are we opening a .txt file and saving as an Excel file?
    _________________
    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
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Re: Referring to one cell as a filename, and save it.

    I dont need to open it, but need to create a second copy, still as a text file.

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

    Re: Referring to one cell as a filename, and save it.

    The most basic method requiring you put the full file name in A1 and B1 would be:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Smile Re: Referring to one cell as a filename, and save it.

    Thank you!

    It is getting close, but still, it doesn't keep the original. What are the magic words?

  6. #6
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Referring to one cell as a filename, and save it.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Re: Referring to one cell as a filename, and save it.

    Thanks.

    Now I am trying to make a second macro which moves the renamed file into a new path.

    This was my effort:

    Sub Move()

    Dim fPath As String: fPath = "C:\Test\Macro\"
    Dim fPath2 As String: fPath = "C:\Test\Macro\Move\"
    Dim fNEW As String: fNEW = Range("B2")

    FileCopy fPath & "\" & fNEW, fPath2 & "\" & fNEW

    End Sub

    But it says "File not found" when running the macro. Any idea what is wrong?

  8. #8
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Re: Referring to one cell as a filename, and save it.

    Nevermind I saw what I did wrong. Thanks for your help.

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

    Re: Referring to one cell as a filename, and save it.

    My original technique demonstrated putting the final \ in the fPath strings. The suggestion from Ajaykgark had you put them in again manually which is unnecessary.

    Please Login or Register  to view this content.

    =======
    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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