Percentage formating in macro

O

orquidea

Hi All:

I want to get the result in saskour20per as a percentage. I wrote the below
procedure which is generating the error "invalid qualifier".

Saskour20per.Value = (saskour20 / sask20)
Saskour20per.NumberFormat = "0.00%"

Could anyone help me to debug this macro?

Thanks in advance
Orquidea
 
J

Jim Cone

To help us help you...
How are the three variables declared?
sask20
saskour20
Saskour20per
What are they returning when the error occurs?
Which line generates the error?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"orquidea"
wrote in message
Hi All:
I want to get the result in saskour20per as a percentage. I wrote the below
procedure which is generating the error "invalid qualifier".

Saskour20per.Value = (saskour20 / sask20)
Saskour20per.NumberFormat = "0.00%"

Could anyone help me to debug this macro?
Thanks in advance
Orquidea
 
O

orquidea

Hi Jim
Thanks for replying back.
The three variables are declared as Double. When I run the macro I get the
message "invalid qualifier" and it highlights the row underlined. I don't
get any result. If I take off the word ".Value" and run the macro again it
gives me the same error and hightlights the same variable at the next row.

Saskour20per.Value = (saskour20 / sask20)
---------------
Saskour20per.NumberFormat = "0.00%"

I tested the macro before with the below procedure and I got the result in
decimals. The calculations are correct.

If sask20 <> 0 Then
Saskour20per = (saskour20 / sask20)
End If

Thanks
Orquidea
 
J

Jim Cone

A Double type variable does not have properties.
So .Value and .NumberFormat cannot be used with them.
However, .Value and .NumberFormat do apply to a range object (cells).

There is a "Format" function that will format strings or numbers, so this should work...
Dim strNum as String
strNum = Format(Saskour20per,"0.00%")
Msgbox strNum

For what it is worth: Paul Lomax in his book "VB & VBA in a Nutshell" states that
the "Format function is possibly the most complex single function in VB"
(try Format(Saskour20per,"Percent")
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"orquidea"
wrote in message
Hi Jim
Thanks for replying back.
The three variables are declared as Double. When I run the macro I get the
message "invalid qualifier" and it highlights the row underlined. I don't
get any result. If I take off the word ".Value" and run the macro again it
gives me the same error and hightlights the same variable at the next row.

Saskour20per.Value = (saskour20 / sask20)
---------------
Saskour20per.NumberFormat = "0.00%"

I tested the macro before with the below procedure and I got the result in
decimals. The calculations are correct.

If sask20 <> 0 Then
Saskour20per = (saskour20 / sask20)
End If

Thanks
Orquidea
 
O

orquidea

Hi Jim:

Finally it worked, what a relieve ! I was stocked in a macro which will
reduce 2 hrs. of work to 1 minute just because of this formating. Thanks a
lot.
I could't figure out your second sugestion, but I did with the first one.
The string is below.
Saskour20per = Format(Saskour20per, "0%")

Thanks a lot for your explanation as well. That helps me to understand
variables and not to make that mistake in the future.

Orquidea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top