VBA Code Im Stuck on dividing a variant by 100 to get percentage

szita2000

Board Regular
Joined
Apr 25, 2012
Messages
101
Office Version
  1. 365
Platform
  1. Windows
Hi Guys.

It is Friday, and yay for that but for the love of god I can't work this one out.

I am trying to crunch some reporting data from a htm file.

The bit I'm stuck is that I have a Variant declared, which starts it's life out as a string (due to a comma as a decimal in the data)
But then I use worksheetfunction.substitute to replace this with a decimal and that converts it into a Variant/Long Datatype.

When I do some calculus with these values (one divided by the other) I expect to see a 0.000 result.

However excel returning This in the immediate window for me:

On the 28 day, Machine A01 - PRCalc = 0.98366048
On the 28 day, Machine A01 - PDTCalc = 5.41666666666667E-03
On the 28 day, Machine A01 - UPDTCalc = 1.09228533333334E-02

The bold bits should read 0.54% and 1.09%

Here is the piece of code doing the simple calculation

Code:
'PRCalc calculation
    If T > 1 Then
        PRCalc = (U2 * A) * 1
    Else
        PRCalc = PR
    End If

'PDT calculation
        PDTCalc = PDT / SPP
    
'UPDT calculation
    If T > 1 Then
        UPDTCalc = 1 - ((PDT / SPP) + PRCalc)
    Else
        UPDTCalc = 1 - PRCalc - PDTCalc
    End If


Then I tried this:
Code:
'PRCalc calculation
    If T > 1 Then
        PRCalc = (U2 * A) * 1
    Else
        PRCalc = PR
    End If


'PDT calculation
        PDTCalc = (PDT / SPP) [B][COLOR=#ff0000]/ 100[/COLOR][/B]
'UPDT calculation
    If T > 1 Then
        UPDTCalc = (1 - ((PDT / SPP) + PRCalc)) [B][COLOR=#ff0000]/ 100[/COLOR][/B]
    Else
        UPDTCalc = (1 - PRCalc - PDTCalc) [COLOR=#ff0000][B]/ 100[/B][/COLOR]
    End If

But the results are the same.
How can I divide these numbers to show percentages?

Any help much appreciated.

Thanks

Thomas

Let me know if you need it, I can post the whole code (which is admittedly a bit clunky... :) )
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hey guys.

Thanks very much for the help.

I wrote out the values to a cell.value in the report and it is correct!

Before I tagged the text on to the value itself "PR% value " & PDTCalc so it formatted as text hence given me the scientific formatting.
Now I removed the text and the value displays correctly.

Thanks!
 
Upvote 0
You could format within VBA like:

Code:
myCell = "PR% value " & Format(PDT / SPP, "0.00%")
 
Upvote 0
Hi Steve.

I never thought of that!
I tried
Code:
With PDTCalc[INDENT].Numberformat = "0.00%"[/INDENT]
[INDENT].value = .value[/INDENT]
End With

But I got an error saying With requires an object.

Then I tried to Set the variable at the calculation, but that thrown an error too.

I will try this now.

Thanks
 
Upvote 0
You use numberformat to format a cell then place the value in the cell. eg

Code:
With Sheets("Sheet1").Range("A1")[INDENT].Numberformat = "0.00%"
.value = PDTCalc
[/INDENT]
End With
 
Upvote 0
Aaaah.

And the Cell would have been my object then.
No?

Makes sense now.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,533
Members
448,969
Latest member
mirek8991

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