VBA Selection.NumberFormat "... ?

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day Everyone!

I'm running a VBA code that tells the cell in a specific range its NumberFormat.

For Example:

VBA Code:
Range("B11:B180").Select
    Selection.NumberFormat = "0000"

My question arises when I'm trying to put ###,###,##0.000 "KG" as the NumberFormat.

For Example:

VBA Code:
Range("B11:B180").Select
    Selection.NumberFormat = "###,###,##0.000 "KG""

It comes up totally red on the second code and I cannot figure out why?

I'm thinking it has something to do with the use of the double brackets of " but I don't know how to rewrite this to make sense VBA wise.

Can you help?

Please post, if you can.

Thanks,
pinaceous
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I'm not familar with using something like "KG" in the numberformat line, but generally in VBA dealing with quotes, as they're apart of the syntax, you may need double or triple quotes, or even & if used in conjunction with a defined variable, before something works.

Is KG a variable?

""KG""

or "" & KG & "" or even """ & KG & """ depending on the VBA code syntax.... good luck!
 
Upvote 0
Try
VBA Code:
Range("B11:B180").NumberFormat = "###,###,##0.000 "" Kg"""
 
Upvote 0
This should also work (I used the shortcut notation for fixed, unchanging ranges, you don't need the extra # signs with the extra comma for that number format and you don't have to worry about the quotes)...
VBA Code:
[B11:B180].NumberFormat = "#,##0.000 \K\g"
 
Upvote 0
This should also work (I used the shortcut notation for fixed, unchanging ranges, you don't need the extra # signs with the extra comma for that number format and you don't have to worry about the quotes)...
VBA Code:
[B11:B180].NumberFormat = "#,##0.000 \K\g"
Ooooh... KG is kilograms? which gets added into the cell after the number? Interesting, never knew!
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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