Adding Two Cell Values to Include Comma Separator

phoenixsuun

New Member
Joined
Jan 31, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet with two values that need to be added and printed. I currently have this:

VBA Code:
myVar = Range("C8").Value + Range("S8").Value

This code adds the values together but when I print myVar, there is no comma. How can I make sure there is a comma in my numbers?

Example: 1,000 + 50 = 1,050 NOT 1050
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the Board!

Number variables do not have any formatting, only values. You need to apply the format either in to cell (if returning it in a cell), or in the MsgBox you return (if returning it to the screen), i.e.
VBA Code:
    myVar = Range("C8").Value + Range("S8").Value
    MsgBox Format(myVar, "#,##0")
You could also store the variable as text, but then you would not be able to use it in mathematical calculations.
 
Upvote 0
Solution
Welcome to the Board!

Number variables do not have any formatting, only values. You need to apply the format either in to cell (if returning it in a cell), or in the MsgBox you return (if returning it to the screen), i.e.
VBA Code:
    myVar = Range("C8").Value + Range("S8").Value
    MsgBox Format(myVar, "#,##0")
You could also store the variable as text, but then you would not be able to use it in mathematical calculations.

Thanks @Joe4! That did the trick. I ended up with a statement that looks like this:

VBA Code:
Print #1, "<div>There are: " & Format(myVar, "#,##0") & " animals located here.</div>"
 
Upvote 0
You are welcome!

Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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