cody_q
cody_q

Reputation: 23

How to format Date & Time in VBA?

I have a userform to display excel worksheet named "results" using OWC Spreadsheet 11.0. The steps could be found from this link : https://stackoverflow.com/questions/17406319/display-a-part-of-an-excel-sheet-on-a-userform-using-vba/17407415#17407415

Now i encountered a problem that the date, time (start,end) all are converted into numeric. Therefore, is there a way to lock the format from the worksheet itself or a VBA code that could work the same way.

This is how the image looks

enter image description here

Upvotes: 2

Views: 5070

Answers (2)

kodi1911
kodi1911

Reputation: 722

Example 1:

Range("").NumberFormat = "dd-mm-yyyy"

or "dd/mm/yyyy" or whatever else combination.

Example 2:

Range("").Value = Format(Range("").Value, "dd-mm-yyyy")

or "dd/mm/yyyy" or whatever else combination.

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 149295

You need to format the relevant columns with the relevant format. For example

Private Sub CommandButton1_Click()
    Me.Spreadsheet1.Cells.Range("A1:B3").Value = _
    ThisWorkbook.Worksheets("Sheet1").Range("A1:B3").Value

    Spreadsheet1.Columns("B:B").NumberFormat = "m/d/yyyy"
End Sub

enter image description here

Upvotes: 2

Related Questions