+ Reply to Thread
Results 1 to 8 of 8

Date issue with macro

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Date issue with macro

    Hello again my friends

    Hope all are well.
    I was wondering if someone could assist me with. I have a speadsheet which has a macro button assigned, where once clicked it will save that spreadsheet under the date listed in a cell (depicted as NOW()). The sheet saves under the current date at the time of saving (great...) but when I open the sheet, the date is showing as the current day (due to the NOW () reference attached). I was wondering is there of manipulating my code so that the date within a saved spreadsheet will remain as it was the day it was saved? here is the code attached:
    Sub Daily_Save()
    '
    'Macro created by Ivor 29/06/10
    'Designed to save the file daily, under the current date automatically
    
    Dim dailytaskfile As String
        Dim dailytaskpath As String
        Dim activeextract As String
        'activeextract = ActiveWorkbook
        ActiveWorkbook.Save
        dailytaskpath = Range("F3").Value
        dailytaskfile = Range("G3").Value
            
        Application.StatusBar = "Saving file ..."
        ActiveWorkbook.Save
    'Reconciliation directory
        
        'ActiveWorkbook.ReadOnlyRecommended = False
        'ActiveWorkbook.WritePassword = ""
        
        ActiveWorkbook.SaveAs FileName:= _
            dailytaskpath & dailytaskfile, FileFormat:= _
            xlNormal, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
    
    End Sub
    Thanks guys

    Ivor
    Last edited by Ivor; 07-05-2010 at 07:35 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Date issue with macro

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Re: Date issue with macro

    Apologies Roy and all. I have now resent in the correct format.

    Apologies again

    Ivor

    Sub Daily_Save()
    '
    'Macro created by Ivor 29/06/10
    'Designed to save the file daily, under the current date automatically
    
    Dim dailytaskfile As String
    Dim dailytaskpath As String
    Dim activeextract As String
    'activeextract = ActiveWorkbook
    ActiveWorkbook.Save
    dailytaskpath = Range("F3").Value
    dailytaskfile = Range("G3").Value
    
    Application.StatusBar = "Saving file ..."
    ActiveWorkbook.Save
    'Reconciliation directory
    
    'ActiveWorkbook.ReadOnlyRecommended = False
    'ActiveWorkbook.WritePassword = ""
    
    ActiveWorkbook.SaveAs FileName:= _
    dailytaskpath & dailytaskfile, FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Date issue with macro

    Try something like this in your code above ...

    dailytaskpath = Range("F3").Value
    dailytaskfile = Format(Date, "ddmmyyyy") & "_" & Format(Time, "hhmmss")
    Range("G3").Value = dailytaskfile

  5. #5
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Re: Date issue with macro

    Quote Originally Posted by pb71 View Post
    Try something like this in your code above ...

    dailytaskpath = Range("F3").Value
    dailytaskfile = Format(Date, "ddmmyyyy") & "_" & Format(Time, "hhmmss")
    Range("G3").Value = dailytaskfile
    Hello

    Thank you for your code. Whilst this may save the file with the date and time of saving, when I re-open the file (say on another day), the date within the file will still have changed to the current day.

    Ok say for example the command 'NOW()' resides in cell A1 on my spreadsheet, although that command will always display the current date, what I need is a piece of code which once the document has been saved, the date displayed by the 'NOW()' command will remain the same (i.e. disabling the 'NOW()' command). So something like:

    Upon saving the spreadsheet, then the date displayed in cell A1 remains as it is (therefore disabling the 'NOW()' command.

    Is there such a code?

    Sorry if I have confused the issue with my explanation.

    Ivor

  6. #6
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Date issue with macro

    The date_time in cell G3 should remain the same when you open the workbook saved using the macro and will only change if you run the macro again. It sounds like you are reopening the latest file (saved in your specified location) and running the macro from this file.

    With, for example:

    dailytaskpath = "C:\"
    dailytaskfile = Format(Date, "ddmmyyyy") & "_" & Format(Time, "hhmmss")
    How about something like this, using the workbook name to define the date and time stamp in cell A1 when the workbook is opened:

    Private Sub Workbook_Open()
    
    Dim date_time As String
    Dim format_date_time As Date
    
    date_time = Application.Substitute(ThisWorkbook.Name, ".xls", "")
    
    format_date_time = Left(date_time, 2) & "/" & _
    Mid(date_time, 3, 2) & "/" & Mid(date_time, 5, 4) & " " _
    & Mid(date_time, 10, 2) & ":" & _
    Mid(date_time, 12, 2) & ":" & Mid(date_time, 14, 2)
    
    With Sheet1.Range("A1")
    .Value = format_date_time
    .NumberFormat = "dd/mm/yyyy hh:mm:ss"
    End With
    
    End Sub
    Copy and paste the code into the ThisWorkBook module in the Visual Basic Editor.
    Last edited by pb71; 07-02-2010 at 01:43 PM. Reason: Added workbook open event code

  7. #7
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Date issue with macro

    I've taken another look at your code above and something like this should do it:

    Sub Daily_Save()
    
    Dim dailytaskfile As String
    Dim dailytaskpath As String
    Dim datetime As Date
    
    dailytaskpath = Range("F3").Value
    dailytaskfile = Range("G3").Value
    datetime = Date & " " & Time
    With Sheet1.Range("A1")
    .Value = datetime
    .NumberFormat = "dd/mm/yyyy hh:mm"
    End With
    
    ActiveWorkbook.SaveAs Filename:= _
    dailytaskpath & dailytaskfile & "_" & _
    Format(Date, "ddmmyyyy") & Format(Time, "hhmm"), _
    FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    
    End Sub
    No need for the workbook open event.
    Attached Files Attached Files
    Last edited by pb71; 07-02-2010 at 03:16 PM. Reason: Example workbook attached

  8. #8
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Talking Re: Date issue with macro

    Quote Originally Posted by pb71 View Post
    I've taken another look at your code above and something like this should do it:

    Sub Daily_Save()
    
    Dim dailytaskfile As String
    Dim dailytaskpath As String
    Dim datetime As Date
    
    dailytaskpath = Range("F3").Value
    dailytaskfile = Range("G3").Value
    datetime = Date & " " & Time
    With Sheet1.Range("A1")
    .Value = datetime
    .NumberFormat = "dd/mm/yyyy hh:mm"
    End With
    
    ActiveWorkbook.SaveAs Filename:= _
    dailytaskpath & dailytaskfile & "_" & _
    Format(Date, "ddmmyyyy") & Format(Time, "hhmm"), _
    FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    
    End Sub
    No need for the workbook open event.
    Well, what can I say!!! Thank you very much PB71, your code works perfectly and is ideal for me to work from. As a newbie to VBA its hard enough getting to grips with 'how to even start writing code from an idea', so by using your example I can start to understand where to start from.

    Thanks again for your help

    Cheers and have a great week sir

    Ivor

+ 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