Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ensure value is always two digits --> macro using cell value 1

Status
Not open for further replies.

y2k1981

Programmer
Joined
Aug 2, 2002
Messages
773
Location
IE
I have the following code to to a Save As action on a file:
Code:
    Range("A3").Select
        Range("A1").Formula = Now()
    Range("B1").FormulaR1C1 = "=DAY(RC[-1])"
    Range("C1").FormulaR1C1 = "=MONTH(RC[-2])"
    Range("D1").FormulaR1C1 = "=YEAR(RC[-3])"
    MyDay = Range("B1").Value
    MyMonth = Range("C1").Value
    MyYear = Range("D1").Value

This is used to archive a file every day with the date, however, I'd like the name to bein the format
Code:
MyFileName MM-DD-YY.xls
ALWAYS, but, say for example on the first of march, the file name will be
Code:
MyFileName M-D-YY.xls
. How can I change the above code so that the values in the three cells are always two digits?
 
set the format in the cells to "00" and then pick up the cell TEXT rather than the cell VALUE Rgds
Geoff

Vah! Denuone Latine loquebar? Me ineptum. Interdum modo elabitur
 
thanks for your response. That works perfet for the date and month, however, the value for the year cell si 2003, and chaning the formatting doesn't appear to be having any effect. Can you help me out a little there?

second question, how do I pick up the text instead of the value, is it just
Code:
MyDay = Range("B1").Text
????


 
Perhaps a slightly different way round.....

DateString = format(Now(),"dd-mm-yy")

would ensure 2 digits for each part and mean that you don't have to pull values from cells Rgds
Geoff

Vah! Denuone Latine loquebar? Me ineptum. Interdum modo elabitur
 
That's a good idea, but I'm still stuck!! This is the piece of code I've written now:
Code:
    MyDateString = Format(Now(), MM - DD - YY)
    MsgBox (MyDateString)
    'ActiveWorkbook.SaveAs Filename:="C:\My Documents\archive\MyFileName " & MyDateString & ".xls"
As you can see, I commented out the save as for now, and used a msgbox to alert me of the value, but its returning 37672 - which I presume is the amount of days today is since jan 1st 1900, but it's now what I want. can you help me out?

 
sorry, I just spotted my problem, I forgot to put MM-DD-YY in " And ". it's sorted now, thanks for all your help and patience.

Big shiney star for you!!
 
was thinking more along the lines of:

ActiveWorkbook.SaveAs Filename:="C:\My Documents\archive\MyFileName " & format(now(),"dd-mm-yy") & ".xls"
Rgds
Geoff

Vah! Denuone Latine loquebar? Me ineptum. Interdum modo elabitur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top