+ Reply to Thread
Results 1 to 5 of 5

SHG - VBA macro for date generation

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2008
    Posts
    42

    SHG - VBA macro for date generation

    Private Sub Workbook_Open()
            Const sAPPLICATION As String = "Excel"
            Const sSECTION As String = "Invoice"
            Const sKEY As String = "Invoice_key"
            Const nDEFAULT As Long = 1&
            Dim nNumber As Long
            
            With ThisWorkbook.Sheets("Job Ticket")
                With .Range("E10")
                    If IsEmpty(.Value) Then
                        .Value = Date
                        .NumberFormat = "dd-mmm-yy"
                    End If
                End With
                With .Range("Z10")
                    If IsEmpty(.Value) Then
                        nNumber = GetSetting(sAPPLICATION, sSECTION, sKEY, nDEFAULT)
                        .NumberFormat = "@"
                        .Value = Format(nNumber, "0000")
                        SaveSetting sAPPLICATION, sSECTION, sKEY, nNumber + 1&
                    End If
                End With
            End With
    End Sub
    SHG - You were able to help me last time with this code, I'd appreciate it if you could lend me your expertise one more time.

    The code works however I noticed that my date wasn't changing. After looking at the code I realized why. The line "If IsEmpty(.Value) Then .Value = Date" will only generate a new date if the cell is empty. How do I amend the code so that the current date is generated when the workbook opens, regardless of whether or not there is data in the cell?

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You need to disble the value testing

    To disable the testing place a ' in front of the If and the End if commands

     ' If IsEmpty(.Value) Then
       .Value = Date
       .NumberFormat = "dd-mmm-yy"
      '   End If
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    04-22-2008
    Posts
    42
    Okay, I did that and it fixed my date problem but now it won't incrementally increase the invoice number every time the document opens. What next?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Same as Rylo suggested for the last one, Justin:
    Private Sub Workbook_Open()
        Const sAPPLICATION As String = "Excel"
        Const sSECTION As String = "Invoice"
        Const sKEY  As String = "Invoice_key"
        Const nDEFAULT As Long = 1&
        Dim nNumber As Long
    
        With ThisWorkbook.Sheets("Job Ticket")
            With .Range("E10")
                .Value = Date
                .NumberFormat = "dd-mmm-yy"
            End With
    
            nNumber = GetSetting(sAPPLICATION, sSECTION, sKEY, nDEFAULT)
            With .Range("Z10")
                .NumberFormat = "@"
                .Value = Format(nNumber, "0000")
            End With
    
            SaveSetting sAPPLICATION, sSECTION, sKEY, nNumber + 1&
        End With
    End Sub
    Last edited by shg; 04-29-2008 at 01:14 PM. Reason: Justin, not Jason ...

  5. #5
    Registered User
    Join Date
    04-22-2008
    Posts
    42
    Thanks guys, it works perfect now. I appreciate all of your help.

+ 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