Consulting

Results 1 to 6 of 6

Thread: Solved: Naming a File

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location

    Solved: Naming a File

    I have an excel macro that is intended to save a subset of data to a report under a unique, yet deliberate file name. Here's an excerpt:

    [VBA]' Creates a temporary base file name.
    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\0903 2009 Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(FILE_NAME, "<date>", Format(Date, "yymm")), _
    FileFormat:=xlNormal
    ActiveWindow.Close
    [/VBA]
    In the code above this, I declared a variable (string) named IB that contains the value of the year -- 2009 in the example above.

    I save a new copy of this file at the beginning of each new month, so ideally I'd like the macro to look at the current system date and express that as yymm -- 0903 in the example above.

    The code I've got isn't cutting it -- any pointers?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    ' Creates a temporary base file name.
    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\<date> Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(FILE_NAME, "<date>", Format(Date, "yymm yyyy")), _
    FileFormat:=xlNormal
    ActiveWindow.Close
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    This works great, except that yyyy is not always the current year. Instead, it is declared in an Input Box and saved to a variable named IB (dimensioned as a string if that makes a difference). Typical values are 2008, 2009, and 2010.

    So, instead of:

    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\<date> Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(FILE_NAME, "<date>", Format(Date, "yymm yyyy")), _
    FileFormat:=xlNormal
    ActiveWindow.Close

    Can I incorporate the string variable into the file name much the same as I incorporate <date> with the code above?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]


    ' Creates a temporary base file name.
    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\<date> <year> Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(Replace(FILE_NAME, "<date>", Format(Date, "yymm"),"<year>",IB)), _
    FileFormat:=xlNormal
    ActiveWindow.Close
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    I have a compile error now:

    "Argument not optional." Text in red is highlighted with the error??

    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\<date> <year> Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(Replace(FILE_NAME, "<date>", Format(Date, "yymm"),"<year>",IB)), _
    FileFormat:=xlNormal
    ActiveWindow.Close

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, messed up my brackets

    [vba]

    ' Creates a temporary base file name.
    Const FILE_NAME As String = _
    "S:\Open Provisioning Reports\<date> <year> Open Provisioning - Pkg.xls"

    ActiveWorkbook.SaveAs _
    Filename:=Replace(Replace(FILE_NAME, "<date>", Format(Date, "yymm")), "<year>", IB), _
    FileFormat:=xlNormal
    ActiveWindow.Close
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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