1

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
9
  • And what's stopping you? Commented Nov 27, 2018 at 10:29
  • 1
    Lack of knowledge I guess Commented Nov 27, 2018 at 10:40
  • 2
    Ha, good answer. I asked because you seem familiar with Format. But not sure what you are doing anyway. Are you trying to format elements of an array? Commented Nov 27, 2018 at 10:45
  • 1
    If you want to format on your labels you are probably looking for: Me.Controls("Label" & j).Caption = Format$(vDB(n, 1), "#,##0.00") Commented Nov 27, 2018 at 10:49
  • 1
    That seems to derive from a particular school of programming. Not sure any of us are entirely clear what is being asked here. Commented Nov 27, 2018 at 11:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Format$(vDB(n, 1), "#,##0.00") - it is actually this one. I didn't know you have to insert $ after Format O_o
@user7202022 The issue was that you asked to "format cells" but you actually wanted to format the "labels". You should be clear with the terms you use! • Actually the only difference is that Format$() returns a String while Format() returns a Variant. Both should work but Format$() is a little faster in this case.
Ok. Thank you! I have to improve my English language skills as well. A lot to improve tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.