Consulting

Results 1 to 3 of 3

Thread: Solved: Reference and format a variable in one expression?

  1. #1

    Solved: Reference and format a variable in one expression?

    Hi!

    I'm trying to use VB to put a formula into a cell that combines text and variables to create a header.

    Here's what I've got so far:

    [VBA]
    Dim StartDate, EndDate as Long

    If Cells(4, 10).Value = "" Then
    StartDate = Cells(3, 10).Value

    Else: StartDate = Cells(4, 10).Value
    End If

    If Cells(4, 12).Value = "" Then
    EndDate = Cells(3, 12).Value

    Else: EndDate = Cells(4, 12).Value
    End If


    If StartDate <> EndDate Then

    Cells(1, 1).Value = "Stock Out Report for " & StartDate & " To " & EndDate

    Else: Cells(1, 1).Value = "Stock Out Report for " & StartDate

    End If
    [/VBA]

    What this returns for me is" "Stock Out Report for (DATE SERIAL) To (DATE SERIAL)" ... So, specifically the problem is that instead of a date, I get the serial number.

    How can I format these variables so I get dd-mmm-yy?

    Thanks for your help!!

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

    [vba]

    Cells(1, 1).Value = "Stock Out Report for " & Format(StartDate, "dd-mmm-yyyy") & " To " & Format(EndDate , "dd-mmm-yyyy")
    [/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
    That worked perfectly!!

    Thanks so much!

Posting Permissions

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