VBA - Format how code returns value of Named Range - Excel 2010

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am working with a userform and need to have a named range value returned as a format number.

Currently my named range ("PrgB") returns the value = 51.515151

I need to have this format as a 2 digit number = 51 but cant seem to get this done. Any help is appreciated!

my partial code

Code:
Sub Code()


Dim pctCompl As Single
Dim Nm1 As Range


Set Nm1 = Range("PrgB")


MsgBox Nm1
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

You can use the INT function to chop off the decimal portion, i.e.
Code:
MsgBox INT(Nm1)
 
Upvote 0
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

Currently my named range ("PrgB") returns the value = 51.515151

I need to have this format as a 2 digit number = 51 but cant seem to get this done. Any help is appreciated!

Code:
MsgBox Nm1

Hi

Not clear.

You want
- to chop off de decimals 51.51 -> 51
- round to integer 51.51 -> 52
- integer always 2 digits 5.51 -> 05

??

Please clarify.
 
Upvote 0
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

Thanks for the response @pgc01 - I would like the format to just trim off the decimals to always show the value as a whole number. Hope that clarifies?

Hi

Not clear.

You want
- to chop off de decimals 51.51 -> 51
- round to integer 51.51 -> 52
- integer always 2 digits 5.51 -> 05

??

Please clarify.
 
Upvote 0
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

I would like the format to just trim off the decimals to always show the value as a whole number. Hope that clarifies?
Did you see my response above?
I believe I showed you how to do precisely that.
 
Upvote 0
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

Hi @Joe4 , I tried your response and although when I enter it as a msgbox I get the desired result but when I enter it as part of the running code to execute nothing happens? The intent is to update a progress bar with this number.

This thing is kicking my butt since I have never done this type of code.

Code:
Sub Code()


Dim pctCompl As Single
Dim Nm1 As Range


Set Nm1 = Range("PrgB")


Nm2 = Int(Nm1)


pctCompl = Nm2


UserForm1.Text.Caption = pctCompl & "% Completed"
UserForm1.Bar.Width = pctCompl * 2


DoEvents


End Sub




Did you see my response above?
I believe I showed you how to do precisely that.
 
Last edited:
Upvote 0
Re: VBA Help - Format how code returns value of Named Range - Excel 2010

Finally, got it to work. your fix worked fine, I was just putting it in the wrong place.

Code:
Sub Code()


Dim pctCompl As Single
Dim Nm1 As Range


Set Nm1 = Range("PrgB")


pctCompl = Int(Nm1)


UserForm1.Text.Caption = pctCompl & "% Completed"
UserForm1.Bar.Width = pctCompl * 2


DoEvents


End Sub


Did you see my response above?
I believe I showed you how to do precisely that.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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