PDA

View Full Version : Solved: subtract



whitewidow
02-21-2007, 05:12 PM
i have figures in two text box and i am trying to subtract from textbox1 and textbox2 and give the result in textbox3.
i have done the coding but does not work

Private Sub cmdcal_Click()
Dim market As Double
Dim average As Double
Dim result As Double
market = txtcurrent.Text
average = txthaver.Text
result = txthdiff.Text
resullt = markert - average


whats wrong with this code

Bob Phillips
02-21-2007, 05:17 PM
resullt = CDbl(Market) - CDbl(average)


and you would do well to add Option Explicit at the head of your code to avoid those typos.

mdmackillop
02-21-2007, 05:24 PM
Hi Whitewidow,
First of all, add an Option Explicit statement and you'll find some of your errors. Fix these first.

whitewidow
02-21-2007, 05:25 PM
resullt = CDbl(Market) - CDbl(average)


and you would do well to add Option Explicit at the head of your code to avoid those typos.

the above code has not worked

i tried this coding but still no luck

Option Explicit

Private Sub cmdcal_Click()
Dim market As Double
Dim average As Double
Dim result As Double
market = txthcurrent.Text
average = txthaver.Text
result = txthdiff.Text
result = market - average
End Sub

Tommy
02-21-2007, 05:34 PM
Hi whitewidow,

You may not be able to add text like a number. I convert the text to a double using the Val() function. :)
Option Explicit

Private Sub cmdcal_Click()
Dim market As Double
Dim average As Double
Dim result As Double
market = Val(txthcurrent.Text)
average = Val(txthaver.Text)
result = Val(txthdiff.Text) 'this one will be overwritten
result = market - average
End Sub

whitewidow
02-21-2007, 05:42 PM
Hi whitewidow,

You may not be able to add text like a number. I convert the text to a double using the Val() function. :)
Option Explicit

Private Sub cmdcal_Click()
Dim market As Double
Dim average As Double
Dim result As Double
market = Val(txthcurrent.Text)
average = Val(txthaver.Text)
result = Val(txthdiff.Text) 'this one will be overwritten
result = market - average
End Sub

this time no error but does not show the calculation when i click on command button in txthdiff.Text

Tommy
02-21-2007, 05:48 PM
Did I miss that? My Bad.
Private Sub cmdcal_Click()
Dim market As Double
Dim average As Double
Dim result As Double
market = Val(txthcurrent.Text)
average = Val(txthaver.Text)
result = Val(txthdiff.Text) 'this one will be overwritten
result = market - average
'added this line to send the result to the textbox
txthdiff.Text = CStr(result)
End Sub

lucas
02-21-2007, 05:50 PM
The problem that whitewidow is having is that she is trying to take the difference of two textboxes values and have them returned in the third.

Me.txthdiff.Value = Me.txtcurrent.Value - Me.txthaver.Value

The proplem is the format of the values in the first 2 textboxes I think.

? 320,000 is the format of the first two textboxes arrived at by:

Me.txthaver.Value = Format(Range("B34").Value, "? #,###,##0")




Edit: Guess I should add a link to the previous thread where whitewidow's file has been uploaded:
http://www.vbaexpress.com/forum/showthread.php?t=11576

whitewidow
02-21-2007, 06:00 PM
The problem that whitewidow is having is that she is trying to take the difference of two textboxes values and have them returned in the third.

Me.txthdiff.Value = Me.txtcurrent.Value - Me.txthaver.Value

The proplem is the format of the values in the first 2 textboxes I think.

? 320,000 is the format of the first two textboxes arrived at by:

Me.txthaver.Value = Format(Range("B34").Value, "? #,###,##0")


Edit: Guess I should add a link to the previous thread where the file has been uploaded:
http://www.vbaexpress.com/forum/showthread.php?t=11576


LUCAS the first part has worked but i need the format to be shwon as ? #,###,##0.

lucas
02-21-2007, 06:50 PM
LUCAS the first part has worked but i need the format to be shwon as ? #,###,##0.
I keep reading this and I have to ask if you mean the problem in the first thread has been solved. If so I understand that but other folks looking at this question can see the problem directly by going to the other thread and downloading the file.

If your saying the code you posted in post 1 of this thread works for you except for a formatting problem then thats different, but I doubt that's it.

Try to be as specific as you can when you post so that we will understand the message.

whitewidow
02-21-2007, 06:56 PM
I keep reading this and I have to ask if you mean the problem in the first thread has been solved. If so I understand that but other folks looking at this question can see the problem directly by going to the other thread and downloading the file.

If your saying the code you posted in post 1 of this thread works for you except for a formatting problem then thats different, but I doubt that's it.

Try to be as specific as you can when you post so that we will understand the message.

forget about the LAST THREAD, i am talking about this THREAD

when subtract from textbox1 and textbox2 the thirdtextbox should give me result in this format ? #,###,##0.

Bob Phillips
02-22-2007, 03:25 AM
I think lucas meant



Me.txthdiff.Text = Format(Range("B34").Value, "? #,###,##0")

whitewidow
02-22-2007, 03:30 AM
XLD here is what i have
Option Explicit
Private Sub cmdcal_Click()
Me.txthdiff.Value = Me.txthcurrent.Value - Me.txthaver.Value
End Sub


but i want the output to be in this format ? #,###,##0")

Bob Phillips
02-22-2007, 03:50 AM
And that is what the code snippet that I gave does

whitewidow
02-22-2007, 03:57 AM
And that is what the code snippet that I gave does

what your code does is it gets the same value as textbox 2.

what my code does is it calculates textbox1 and textbox2 and then outputs the result to textbox3 but i wan the format to be in this format ? #,###,##0")

Bob Phillips
02-22-2007, 04:06 AM
Option Explicit

Private Sub cmdcal_Click()
Me.txthdiff.Text = Me.txthcurrent.Text - Me.txthaver.Text
Me.txthdiff.Text = Format(Me.txthdiff.Value, "? #,###,##0")
End Sub


or, more directly



Option Explicit

Private Sub cmdcal_Click()
Me.txthdiff.Text = Format(Me.txthcurrent.Text - Me.txthaver.Text,"? #,###,##0")
End Sub

whitewidow
02-22-2007, 04:20 AM
THANK you very much XLD

mdmackillop
02-22-2007, 06:22 AM
Hi Whitewidow,
I think if you now go back and carefully read your original post, you'll see why it took 16 posts to get your solution.
Regards
MD