Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Excel formatting

0

Hello! I am coming back with a request. I got some excel workbooks that need some formatting, and I wanted if there's a macro for it. I'll leave an example workbook so you can understand better. Basically, I got excel files with numbers, I add filter on the first row and I start to select numbers.

First, I select from the filter the natural numbers (without decimals), I select them all, I press format, and convert them from General to Number (with 0 decimals).

After that, I select from the filter only the numbers with 1 decimal, select them all, format cells again, convert from General to Number (with 1 decimal).

After that, I select from the filter only the numbers with 2 decimal, select them all, format cells again, convert from General to Number (with 2 decimal) and so on...

There are also 2 exceptions:

1) In the filters list appears sometimes (Blanks) - those I don't need to touch them. They should remain like that, not to be messed with. (photo example in excel below)

2)Numbers like: 2544.00000000 (photo example in excel below) - those need to be selected from the filter before everything, selected all afterwards, and when the exclamation icon appears, hit "convert to number" (photo example in excel below), and then selected again, format cells, convert from General to Number (with 0 decimals).

I know I kinda ask for a lot, but I don't know how to do it, so if you can make time to help me, it would mean a lot for me! Thank you!

Answer
Discuss

Discussion

Squishy. I'll answer you but must remind you that this is a Q&A Forum - you ask a question, someone answers. It isn't really for questions equivalent to "I need to do this complicated task- please write some code for me"- we expect you to have a go then come back when you get stuck. Please follow that approach next time.
John_Ru (rep: 6142) Dec 9, '21 at 11:57 am
You're litterally a god in excel :)
Thanks a lot! Works verry well! Also, what part should I extract so that it doesn't put the 1000 separator? I want them without the ","
Squishy (rep: 18) Dec 10, '21 at 3:04 am
Like: 5000 and not 5,000
Squishy (rep: 18) Dec 10, '21 at 3:15 am
And it also it formats the (Blanks). Is there a way to not touch them? I really need the (Blacks) cells to not be messed with
Squishy (rep: 18) Dec 10, '21 at 3:28 am
Squishy. Glad that worked for you.

In the Case Select section, you can remove the thousands separator from each format string, e.g. change "#,##0.0" to "###0.0". Personally I find that the separator avoids mis-reading large nunbers. 
John_Ru (rep: 6142) Dec 10, '21 at 4:04 am
So I need to delete this?
Select Case DP ' pick how to format the cell  based on DP                              Case 0                     .NumberFormat = "#,##0"                 Case 1                     .NumberFormat = "#,##0.0"                 Case Else                     .NumberFormat = "#,##0.00"                                  End Select
Squishy (rep: 18) Dec 10, '21 at 4:13 am
I mean, delete the "," form each case
Squishy (rep: 18) Dec 10, '21 at 4:15 am
If I do that, they go into "Custom" section. And I need them in "Number" section :(
Squishy (rep: 18) Dec 10, '21 at 4:18 am
Remove the ### part too and Excel will see them as having a Number format.

The code already ignore blanks- the If IsNumeric() test returns a False so it skips to End If before looping again. It leaves the formatting alone.
John_Ru (rep: 6142) Dec 10, '21 at 4:30 am
All well now?
John_Ru (rep: 6142) Dec 10, '21 at 5:37 am
First part yes. But it doesn't ignore the blanks, it auto-formats them into "0".
Squishy (rep: 18) Dec 10, '21 at 6:27 am
Squishy, sorry about that (don't know what happened when I tried it earlier).

See my revised Answer(/file) where the test line is now:
If IsNumeric(Cl.Value) And Not IsEmpty(Cl.Value) Then 'not a blank and a number (or can be converted to a number)?
but note that it has the thousands speparator as before- you know how to change that!
John_Ru (rep: 6142) Dec 10, '21 at 7:28 am
Yes, it works good now. Thank you!!
Squishy (rep: 18) Dec 13, '21 at 1:27 am
Add to Discussion

Answers

0
Selected Answer

Squishy

The commented code below may work for you. It loops through a range (in bold) on the active sheet, rounds the numbers to two decimal places (but not using the VBA function which has quirks), determines the number of decimal places then formats the cells as a Number to that many (using a Case Select statement rather then nested IFs). It ignores any blanks or text cells (which don't just contain a number as text):

Sub FormatByDP()

Dim Cl As Range, DP As Integer, DPsep As String, n As Long

DPsep = Application.DecimalSeparator ' get local decimal sepaartor (e.g. . or ,)

'Loop through cells in range
For Each Cl In Range("F2:M29")
    If If IsNumeric(Cl.Value) And Not IsEmpty(Cl.Value) Then 'not a blank and a number (or can be converted to a number)?
        n = n + 1 'count another cell
        With Cl
            .Value = WorksheetFunction.Round(.Value, 2) ' reliably round to 2 dps
            If InStr(1, .Value, DPsep) > 0 Then ' see if there's a decimal separator
                DP = Len(.Value) - InStr(1, .Value, DPsep) ' see no. decimal places remaining
                Else
                DP = 0
            End If

            Select Case DP ' pick how to format the cell  based on DP
                Case 0
                    .NumberFormat = "#,##0"
                Case 1
                    .NumberFormat = "#,##0.0"
                Case Else
                    .NumberFormat = "#,##0.00"
            End Select
        End With
    End If
Next Cl

MsgBox "Formatted " & n & " numbers in range"

End Sub
Note that to check the decimal places in the applied Number format, go the Format the cell, then choose "Custom" to see how it is actually formatted.

Hope this works for you.

Discuss

Discussion

Does this slight revision work for you Squishy?
John_Ru (rep: 6142) Dec 10, '21 at 8:35 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login