How to change a cell from "General" to "Number" and add a comma?

Asked

Viewed 5,785 times

3

I am learning VBA and I had a difficulty to transform a column that has values formatted as "General" for number, besides, I need these values to be expressed with comma.

Current values (sample):

Dias colunas que preciso transformar

I know there is a simpler way to do this without using VBA, but I would like to learn and acquire more knowledge in the tool.

  • How many decimal places after the comma?

1 answer

2


To format, the Property Range.Numberformat is used.

Dim rng As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Planilha1")
Set rng = ws.Range("B:B", "E:E")

With rng
    .NumberFormat = "#,##0.00"
End With

Explanation of the code

Spreadsheet (Worksheet)

Declares the worksheet to be used in VBA

 Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Planilha1")

Range

Declares the range to be used, in this case columns B and E.

 Dim rng As Range: Set rng = ws.Range("B:B", "E:E")

Logic With (with)

Use with to realize what’s inside With and End With only within the Range rng

  With rng
  End With

Format

Format for numbers with two decimal places

.NumberFormat = "#,##0.00"

Other way to use and declare this code

Dim rng As Range

Set rng = Planilha1.Range("B:B", "E:E")

rng.NumberFormat = "#,##0.00"

Check the Numberformat

To check some cell formats, click More Number Formats

Mais formatos de Números

Check in the Custom Category, some examples of Numberformat

NumberFormat

Browser other questions tagged

You are not signed in. Login or sign up in order to post.