Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA Sum returns Wrong Format

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default VBA Sum returns Wrong Format

If you are always going to use F46 as your destination cell, the simple
option is simply to format that cell.

If your code may dynamically place results in difference cells and you need
to format within VBA then the following might work for you. I suggest when
it comes to things that you can do manually but are trying to replicate in
code, use the macro recorder to give you a push in the right direction-
that's where this code comes from. Either select the cell in code, or change
the select statement to reflect your target range.

Selection.NumberFormat = "#,##0.00"

HTH,
Keith

"code_hungry" wrote in message
...
If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default VBA Sum returns Wrong Format

Dim MySum As Currency

"code_hungry" wrote:

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default VBA Sum returns Wrong Format

I think if you change your Sub's last statement to this, instead of what you
have, it will do what you want...

Range("F46").Value = FormatCurrency(MySum)

Rick


"code_hungry" wrote in message
...
If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA Sum returns Wrong Format

You declared MySum as an Integer. Make it Double.

ps. I wouldn't use Sum for the procedure name.

code_hungry wrote:

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default VBA Sum returns Wrong Format

Well, I must say, I am surprised that worked. On the VBA side of things, the
Currency data type does not contain the currency symbol or nor the thousands
separator (which can be seen by adding a Debug.Print MySum statement to the
subroutine).

While I am sure what you posted will do what the OP probably actually
wanted, I would note that answers using your Dim statement will not be the
same as those produced but the OP's original code. His Dim'ming the MySum
variable as Integer would mean no decimal dollars are returned to the
worksheet whereas your Currency data type will do so.

Rick


"JLGWhiz" wrote in message
...
Dim MySum As Currency

"code_hungry" wrote:

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the
value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default VBA Sum returns Wrong Format

Thank you all for your reply. The Currency solution was the one that worked.
Because you were so quick to reply, I was able to give this project back to
my boss on the same day. He was happy.

Thanks again.

"JLGWhiz" wrote:

Dim MySum As Currency

"code_hungry" wrote:

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA Sum returns Wrong Format

I would have used double.

code_hungry wrote:

Thank you all for your reply. The Currency solution was the one that worked.
Because you were so quick to reply, I was able to give this project back to
my boss on the same day. He was happy.

Thanks again.

"JLGWhiz" wrote:

Dim MySum As Currency

"code_hungry" wrote:

If Anyone could give me a hand....

Cell E33 value is "$2,793.70"
Cell F33 value is "$450.00"
My code rounds up the result and returns the value 3244. I need the value
to be but I need it "3,243.70"

What do I have to change in the code below?

Thanks

Sub sum()
Dim MySum As Integer
Dim MyRange As Range

Set MyRange = ActiveSheet.Range("E33:F33")
MySum = Application.WorksheetFunction.sum(MyRange)
Range("F46").Value = MySum

End Sub
--


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange? VBA code returns wrong date format (not a date) zzxxcc Excel Programming 8 October 12th 07 09:13 AM
cell returns wrong value garyww Excel Worksheet Functions 1 August 17th 06 02:38 PM
Row() function returns wrong row and more.. Kim Excel Worksheet Functions 5 July 11th 06 02:11 AM
Lookup returns wrong value motorjobs Excel Worksheet Functions 5 June 21st 06 11:49 PM
SUM returns wrong result MarkN Excel Worksheet Functions 5 October 20th 05 03:59 AM


All times are GMT +1. The time now is 07:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"