Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Abs function doesn't work with textbox when show message

0

I search for way to correct  result in message box

when show message box I would deal minus amount in textbox1 as plus amount even the real amount is minus in textbox1.

example :Abed name contains minus value =-2,000.00 when select name from combobox1 and when write in textbox2 =300.00 then will not show message box because I consider minus value as pluse value in textbox1 then will not show message box as long amount in textbox1>textbox2 based on message box , the problem will deal minus value in this case will show message box consider textbox1<textbox2  despite of I use Abs function!

also when select Abdo from combobox1 will populate amount in textbox1=2,000.00 and when write in textbox2 =2,000 then should not show message box because will equel zero , , the problem will  show message box showin value is zero.

Private Sub TextBox2_AfterUpdate()
Dim tot As Double
tot = CDbl(Abs(TextBox1.Value)) - CDbl(TextBox2.Value)
If TextBox1.Value < TextBox2.Value Then MsgBox " bigger than available , so you have plus amount about" & " " & Format(Abs(tot), "#,##0.00")
End Sub

how can I fix theses problems,please?

Answer
Discuss

Discussion

Abdo

Sorry but I'm confused- I just don't understand the wording of your question or what you are trying to do.

Why are you treating negative balances as positive ones sometimes? If Abed has a negative balance (say -2000), why would you stop more that that being paid (e.g. 2,500) to get to a positive balance?

Please explain what you're trying to stop happening (with two simple examples) and I should be able to help.
John_Ru (rep: 6537) Nov 23, '24 at 7:18 am
the minus  balances for the name should pay for me. when pay for me should decrease his balance  for instance if his balance -2,000.00 and pay for me 3,000.00 then will be -2,000.00-3,000.00=-5,000.00 in this case I want from his 5,000.00 , this is wrong ! he paid me so will be 2,000.00-3,000.00=-1,000.00 
but in message show 1,000.00 (this means he wants from me 1,000.00)
so the minus value in textbox1 should change to plus and when gives minus value after subtract textbox1-textbox2 should change to plus when show message box.
if name =2000 in textbox1 , textbox2 =3,000.00   will show minus value -1,000.00  this mean I want from his -1,000.00 without change to plus value
as to if the result is zero  also when textbxox1 > textbox2 then don't show message at all .
when show message this is just warning and informe me what happens after balance in textbox1< textbox2  
I'm talking about changing from minus to plus just when show message , don't change in textboxes.
Abdo M (rep: 20) Nov 23, '24 at 8:38 am
Still not clear (sorry) but I had a guess- please see my Answer
John_Ru (rep: 6537) Nov 23, '24 at 1:19 pm
Add to Discussion

Answers

0
Selected Answer

 Ok 

I got solution after amny tries . the matter is simple but seem to I 'm too lazy to think way.

I would share my thoughts.

will take two cases as in example: 

if the name contains minus value  in textbox1

example : show first in TB1=-4,000.00 , TB2=,1,000.00 from first sight will TB1<TB2

but what I want subtract TB1-TB2 ignoring minus in TB1 so when show message will deal TB1 as plus in this case will be =4,000.00-1,000.00

 so TB1> TB2 then don't show message as long  TB2 <TB1

and TB1=-4000,TB2=4,000 then message will deal TB1as plus in this case will be =4,000.00-4,000.00=0 so TB1=TB2 then don't show message as long  TB2=TB1

if the name contains plus in TB1 then no need change when show message as long there is no minus in TB1.

from first select name and TB1=4,000.00 ,TB2=2,000.00 then will not show message as long TB1>TB2, if TB1=4,000.00 and TB2=4000 then will not show message as long TB1=TB2

TB1=4,000.00 ,TB2=2,000.00 hen will not show message as long TB1>TB2 

for short word the message box show Exceeded the amount TB1 after write in TB2 ignoring minus value in TB1 after subtraction TB1-TB2

finally here is the code

Private Sub TextBox2_AfterUpdate()
Dim tot As Double
tot = CDbl(Abs(TextBox1.Value)) - CDbl(TextBox2.Value)
If tot < 0 Then MsgBox " bigger than available , so you have plus amount about" & " " & Format(Abs(tot), "#,##0.00")
End Sub

Sorry for the long post!

I hope this subject help members

Discuss

Discussion

Well done Abdo. You knew what you wanted (unlike me!) and found the simple solution. It's good that you've shared your work but I supect sadly that the wording might again be an obstacle to understanding. 
John_Ru (rep: 6537) Nov 25, '24 at 10:52 am
Add to Discussion
0

Abdo.

I'm still not clear what your question means (or your "clarification" in the Discussion to that) so I've made a guess...

In the attached, revised file, your code is modified as follows (comments and changes in bold):

Private Sub TextBox2_AfterUpdate()
    Dim tot As Double
    ' do nothing unless the TBs have values
    If TextBox2.Value = "" Or TextBox1.Value = "" Then Exit Sub
   
    tot = CDbl(TextBox2.Value) - CDbl(TextBox1.Value)
    
    'If TextBox2.Value > TextBox1.Value Then MsgBox " bigger than available , so you have plus amount about" & " " & Format(tot, "#,##0.00")
    
    ' Act depending on what TextBox1 is
    Select Case CDbl(TextBox1.Value)
        ' if it's negative
        Case Is < 0
            MsgBox "That payment in makes " & ComboBox1.Value & "'s balance " & Format(CDbl(TextBox1.Text) + CDbl(TextBox2.Text), "#,##0.00")
        ' if it's negative
        Case Is > 0
            If tot > 0 Then
                MsgBox ComboBox1.Value & "'s balance wasn't enough to pay out " _
                & CDbl(TextBox1.Value) & "- would give a balance of " & tot
            End If
        Case 0
            MsgBox ComboBox1.Value & "'s balance was 0"
    End Select
End Sub

You can now launch the form by the green button on the worksheet. The Message Box now changes depending on what the Name's balance is.

Note that I added a test (after the Dim statement) to avoid problems and did something similar in Private Sub CommandButton1_Click().

Hope this helps (and you select my Answer). If not please try to explain carfelyy why not.

Discuss

Discussion

Thanks john
I tested  but still some problems 
when abden =-2000 in textbox1 and write in textbox2 1000 then should not show message box because when show message will change minus to plus  in textbox1 2000-1000=1000 so textbox1 will be bigger more than textbox2 , not smaller based on condition in message box 
and when textbox1=-2000 and textbox2=2000 and when change minus to plus in textbox1 =2000-2000=0 so will not show message box based on condition in message box .
so two cases need fixing after change minus value to plus value in textbox1 
1- if textbox> textbox2 then doesn't show message
2- if textbox1=textbox2 then doesn't show message
Abdo M (rep: 20) Nov 23, '24 at 1:59 pm
Thanks Abdo but once again I don't really understand what you are trying to say.

I can't work out what you're trying to do (or even what your role is in these transactions - are you some kind of banker or lender?) 

I'd like to help but your explanations just confuse me and to be honest I'm on the point of giving up
John_Ru (rep: 6537) Nov 23, '24 at 2:57 pm
or even what your role is in these transactions - are you some kind of banker or lender?
this is just  relating balances for receivable,payable  . that will show me when I pay or receive balance more than available balances. this idea I make it from me .
I'd like to help but your explanations just confuse me and to be honest I'm on the point of giving up
I appreciate that , and I do my best to explain my goal but obvouisly we can't understand each other of us , this is my bad lucky. anyway thank you for your time.  
Abdo M (rep: 20) Nov 23, '24 at 3:13 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login