Consulting

Results 1 to 5 of 5

Thread: Solved: How to write format code in VBA?

  1. #1

    Solved: How to write format code in VBA?

    I created a vlookup VBA code and it works:

    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, _
        searchdirection:=xlPrevious).Row
        
        Range("E2:E" & lRow).Formula = "=vlookup(C2,etc!A:B,2,False)"
        
        End With
    My problem is, the cell returned 40262 for 25-Mar-10.

    How will I add formatting on my code?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Try changing the number format of the cells to a date.

  3. #3
    actually i want to incorporate the formatting in my VBA code so all I have to do is to run the code.

    I know it's kinda like format(xxx,dd-mm-yy,xxx,xxx)

    I just don't know what expressions or symbols or data to put on those xxx.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Actually I think you want .NumberFormat. Something like:
    [vba]
    '...preceeding code...

    lRow = Range("A:A").Find(what:="*", LookIn:=xlValues, SearchOrder:=xlByRows, _
    searchdirection:=xlPrevious).Row

    With Range("E2:E" & lRow)
    .NumberFormat = "d-mmm-yy"
    .Formula = "=vlookup(C2,etc!A:B,2,False)"
    End With
    End With
    [/vba]

    An easy way to get the correct arg is to open a new wb and record a quick macro, changing the active cell's format to how you want the date to display.

  5. #5
    Thanks GTO!

Posting Permissions

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