VBA Text() function not formatting correctly

nathanpere

New Member
Joined
Sep 14, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I am new to writing in the VBA and have been using the WorksheetFunction.Text() command to format data. The data I am using is taken from PowerBI and for some reason my text function will not work correctly.

VBA Code:
Sub FormatAsText()

    Dim cel As Range, rng_percent As Range, rng_minute As Range
    Set rng_percent = Range("B3,D3:E3,B7,D7:E7,B8,D8:E8,B13,D13:E13")
    Set rng_minute = Range("B12,D12:E12,B5,D5:E5,B6,D6:E6")
    
        For Each cel In rng_percent
            With cel
                cel.Value = WorksheetFunction.Text(cel.Value, "0.0%")
            End With
        Next cel
        
         For Each cel In rng_minute
            With cel
                cel.Value = WorksheetFunction.Text(cel.Value, "mm:ss")
            End With
        Next cel

End Sub

This is the code I am using right now, however the output of my text still seems to be in the format "0.00%" as shown in the image below.

Does anybody know why this is and how I can change it to the format of 1 decimal point?

Also if you have any ways to make my code more efficient it would be much appreciated.

Thanks!
 

Attachments

  • text_format.PNG
    text_format.PNG
    9.6 KB · Views: 11

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try this
VBA Code:
For Each cel In rng_percent
            With cel
                .NumberFormat = "0.00%"
            End With
        Next cel
        
         For Each cel In rng_minute
            With cel
               .NumberFormat = "h:mm"
            End With
        Next cel
 
Upvote 1
Solution
A little shorter:
VBA Code:
Sub FormatAsText()

    Dim rng_percent As Range, rng_minute As Range
    Set rng_percent = Range("B3,D3:E3,B7,D7:E7,B8,D8:E8,B13,D13:E13")
    Set rng_minute = Range("B12,D12:E12,B5,D5:E5,B6,D6:E6")
   
    rng_percent.NumberFormat = "0.00%"
    rng_minute.NumberFormat = "h:mm"

End Sub
 
Upvote 1

Forum statistics

Threads
1,224,885
Messages
6,181,588
Members
453,055
Latest member
cope7895

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top