+ Reply to Thread
Results 1 to 5 of 5

VBA to open file with a variable name

  1. #1
    Forum Contributor
    Join Date
    04-20-2020
    Location
    New Jersey, NJ
    MS-Off Ver
    MS Office 365
    Posts
    159

    VBA to open file with a variable name

    Hey all,

    Kinda new at this excel/VBA gig.
    Trying to automate some tasks.

    The date can be changed manually in the Excel Spreadsheet.

    For ex. in this Spreadsheet

    B2 is 05
    B3 is 27
    B4 is 2020

    This is the code I'm using to open a file whose name changes based on date.


    Sub openvarpt2()

    Dim varCellday As Long
    Dim varCellmonth As Long
    Dim varCellyear As Long


    varCellday = Range("B2").Value
    varCellmonth = Range("b3").Value
    varCellyear = Range("b4").Value

    Workbooks.Open "M:\R\2020\EFR - " & varCellmonth & "." & varCellday & "." & varCellyear & ".xlsm"

    End Sub

    the file path is: M:\R\2020\EFR - 05.27.2020.xlsm

    I keep getting an error msg because the "05" gets picked up by the code as only a "5". I formatted it to text already in Excel.

    By the way, it works when i change the code to:

    Workbooks.Open "M:\R\2020\EFR - " & "0" & varCellmonth & "." & varCellday & "." & varCellyear & ".xlsm"

    Any thoughts?

    Thanks!
    Last edited by jack.a; 05-28-2020 at 09:26 AM. Reason: Solved

  2. #2
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,914

    Re: VBA to open file with a variable name


    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.
    Try this:
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-20-2020
    Location
    New Jersey, NJ
    MS-Off Ver
    MS Office 365
    Posts
    159

    Re: VBA to open file with a variable name

    Works perfectly! Thank you!

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    04-20-2020
    Location
    New Jersey, NJ
    MS-Off Ver
    MS Office 365
    Posts
    159

    Re: VBA to open file with a variable name

    is there any reason you can think of that if i apply the same code to save the file as something specific the formatting would turn it all into zeroes?

    the file saves as - "EFR -00.00.0"

    filepath: M:\R\2020\EEFR -00.00.0.xlsm

    cells

    F2 - 5
    F3 - 27
    F4 - 2020


    Please Login or Register  to view this content.
    Last edited by jack.a; 05-28-2020 at 11:52 AM.

  5. #5
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,914

    Re: VBA to open file with a variable name

    Chane to:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Using Workbook.Open to open an excel file with a variable path
    By walt737 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2019, 01:01 PM
  2. VBA open file with variable name with a variable file path.
    By BLTnoBacon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-02-2018, 09:19 PM
  3. Code to open fixed with file file with variable name
    By jlax34 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2015, 11:49 PM
  4. [SOLVED] Open file with variable name
    By vio.coman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-29-2014, 03:11 PM
  5. [SOLVED] Macro - Master file to import data from another open file with variable file name
    By jdodz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2012, 10:56 PM
  6. [SOLVED] Open file from a folder and assign that file to an Object variable
    By pratyu in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-22-2012, 02:11 AM
  7. Open variable file name
    By Ace of Clubs in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-21-2010, 12:47 AM

Tags for this Thread

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