range.formula changing format?

ra_weiss

New Member
Joined
Oct 18, 2008
Messages
35
I have looked everywhere within my workbook/sheet for what could be causing the following code to enter its result rounded to the nearest dollar.

******************
Sub Office_Pay_Totals()

Dim i As Long
Dim j As Long

i = 0
ActiveSheet.Unprotect Password:="1"
Range("E31").Formula = "=Sumif(N9:N28,""x"",E9:E28)"
Let i = Range("e31")

For j = 9 To 28

ActiveSheet.Cells(j, 14).Activate
If ActiveSheet.Cells(j, 14) = "X" Then
ActiveSheet.Unprotect Password:="1"
ActiveSheet.Cells(j, 5).Activate
ActiveCell.BorderAround Weight:=xlMedium
ActiveSheet.Unprotect Password:="1"

ActiveSheet.Cells(j, 14).Clear

End If

Next j

On Error Resume Next
Let Range("e31") = i
On Error Resume Next
ActiveSheet.Protect Password:="1"

End Sub

*******************
Cell E31 has $0.00 formatting, but when ever I run this script the result is changed to $0 formatting. As a kicker, another PC (also WIN7) does not do this. Does anyone have ideas?

(The script looks in column N for any instances of "X", and if found adds all the corresponding row figures in column E and enters the total in cell E31)

And, yes, I'm a beginner.
 

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.
You've declared the variable i as a Long Integer, so you'll lose the decimal part of the result when you assign it to that variable.

You could declare it as Decimal, or you could just put the result straight into the cell without using a variable.
 
Upvote 0
Actually I misread your formula and assumed that since you mentioned % you expected dollars and cents.

Looks like it's only dollars though, so an integer should be fine.

So I'm not 100% sure what the problem is, though that does seem to be quite a lot of code for what I think is quite simple.

For a start you should only need to unprotect the sheet once.
 
Upvote 0
The protect/unprotect routine is for usability.

K, go for it. I have "N" column that if an "X" is placed, the routine must find corresponding row data in "E" column, then total all instances into "E31". I'd like to see an alternative. Thanks.
 
Last edited:
Upvote 0
Sorry I'm not quite following.

What has column K got to do with it?:eek:

Also if you want a total wouldn't you use SUMIF not COUNTIF?
 
Upvote 0
<style type="text/css">
table.tableizer-table {border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px;} .tableizer-table td {padding: 4px; margin: 3px; border: 1px solid #ccc;}
.tableizer-table th {background-color: #104E8B; color: #FFF; font-weight: bold;}
</style>

<table class="tableizer-table">
<tr class="tableizer-firstrow"><th></th><th> </th><th> </th><th> </th></tr> <tr><td> </td><td>$525.00</td><td> </td><td>X</td></tr> <tr><td> </td><td>$21.00</td><td> </td><td> </td></tr> <tr><td> </td><td>$908.00</td><td> </td><td>X</td></tr> <tr><td> </td><td>$27.00</td><td> </td><td>X</td></tr> <tr><td> </td><td>$49.00</td><td> </td><td> </td></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> <tr><td>SUBT</td><td>$70.00</td><td> </td><td> </td></tr> <tr><td>X</td><td>$1,460.00</td><td> </td><td> </td></tr> <tr><td>TOTAL</td><td>$1,530.00</td><td> </td><td></td></tr></table>

Any row with an "X" gets totaled in a seperate subtotal, and also has a border added to the cell. (the border is not shown in my example)

Edit: clean up
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,874
Members
452,949
Latest member
Dupuhini

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