简体   繁体   中英

Excel VBA format numbers to specific value in UserForm

I have UserForm that dispalys values out of cells on my sheet "Price calculations". First column A on sheet "Price calculation" has text in rows from 72 to 88. All the rest of the columns D, E, F, G, H... etc has numbers I want to format.

I want to format cells to specific format for values with 17 rows k=17 (columns D, E, F, G, H... etc) is my code. I want to use format Format(XXX.Value, "#,##0.00") so my numbers will look like 71 000,00 instead of 71000

Private Sub UserForm_Initialize()

Dim vDB As Variant, a As Variant, c As Variant
Dim Ws As Worksheet
Dim i As Integer, j As Integer, n As Integer
Dim k As Integer

Set Ws = ThisWorkbook.Sheets("Price calculation")

a = Array("a", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n") 'column characters
c = Array(409, 444, 480, 516, 552, 588, 624, 660, 696, 732, 768, 804) 'label numbers

For i = LBound(a) To UBound(a)
    If i = 0 Then
        k = 16
    Else
        k = 17
    End If
    vDB = Ws.Range(a(i) & 72).Resize(k)
    n = 0
    For j = c(i) To c(i) + k - 1
        n = n + 1
        Me.Controls("Label" & j).Caption = vDB(n, 1)
    Next j
Next i

Use

Worksheets("YourSheetName").Range("D72:N88").NumberFormat = "#,##0.00"

to format the cells D72:N88 in worksheet YourSheetName .

Or

Me.Controls("Label" & j).Caption = Format$(vDB(n, 1), "#,##0.00")

If you want to format on your labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM