PDA

View Full Version : Solved: multiply values from a textbox and combobox in a userform



Dodgeitorels
05-21-2009, 04:15 AM
Hi everyone,
My name is Jeff and I think I am the noobiest of noobs with vba (Excel 2007).

I have created a userform to calculate ganged weights of trusses.
The userform has 10 comboboxes that are all poulated with the same indexlist. If I choose an item from the first combobox (gangedweightbox1) it then puts a weight value for that selection into textbox1. I then choose a quantity from another combobox (combobox1).
This is the part that I am having trouble with...... I want another textbox (textbox11) to show the product of textbox1 and combobox1. Can anyone please help me with the code to make this happen and tell me where to put the code (be nice now lol). Thank you very much in advance.

MaximS
05-21-2009, 04:30 AM
try that in combobox1:


Private Sub combobox1_Change()
textbox11 = combobox1.Value * textbox1.Value
End Sub

Dodgeitorels
05-21-2009, 05:04 AM
Thank you. That did exactly what I wanted for all 10 ganged weightbox1 through 10. Now I have new problem(s). :confused:
On same userform I also have a button labeled "RESET" which I want to clear all data from textboxes and comboboxes. The code I currently have for the reset button is:

Private Sub CommandButton1_Click()
For Each x In Me.Controls
If TypeName(x) = "TextBox" Or TypeName(x) = "ComboBox" Then x.Text = ""
Next
End Sub

However when I click reset I get debug message saying runtime error '13' type mismatch. Also when I click reset and then go back to gangeweightbox1 it starts at a blank space at the end of indexlist and I have to scroll up. I apologize for my ignorance and maybe I am trying to do more than I should be with VBA. Any ideas? and again ty very much for the quick response to first question. If you would like to see what I actually have here just let me know. There is nothing for personal information in the program.

MaximS
05-21-2009, 05:27 AM
try that:


Private Sub CommandButton1_Click()
For Each x In Me.Controls
Select Case TypeName(x)
Case "TextBox"
x.Text = ""
Case "ComboBox"
x.Clear
End Select
Next
End Sub

Dodgeitorels
05-21-2009, 05:33 AM
Again ty, but the x.Clear part causes error. I am totally lost here.

GTO
05-21-2009, 05:42 AM
Just an FYI/request...

Might you be able to post attachments in .xls format? A lot o' us do not have 2007. Not only might others be able to contribute, but we all can learn along the way.

Mark

MaximS
05-21-2009, 06:08 AM
ok, now i know what is causing the problems.

convert all combobox_change as follows:


Private Sub ComboBox1_Change()
If ComboBox1.Value <> "" Then
TextBox11 = ComboBox1.Value * TextBox1.Value
End If
End Sub


and put that into reset:

Private Sub CommandButton1_Click()
For Each x In Me.Controls
Select Case TypeName(x)
Case "TextBox"
x.Text = ""
Case "ComboBox"
x.Object.Value = ""
End Select
Next
End Sub

Dodgeitorels
05-21-2009, 09:30 AM
A very big thank you MaximS, works like a charm. After going through it all everything seems to work fine. Except one little thing and I will spend some time on this before I post a question. lol

GTO, my apologies. Again, I am the noobiest of noobs here. If you would take the time to tell me how to make an xls file I will glady do it.

Dodgeitorels
05-21-2009, 12:21 PM
ok, so I have tried for a few hours with no success. It should be a very simple thing to do however it has kicked my but. All solutions I have received with this project have worked wonders for me.
What I need now is this:
Using the same file I posted about earlier on this thread, I need to "Sum" the totals of textbox11 through 20 and put the "Sum" into textbox21. Please note that textbox11 through 20 are constantly changing so I need textbox21 to refresh totals.

MaximS
05-21-2009, 10:37 PM
now add below code to each textbox_Change between 11 and 20:


Dim x As Long
If TextBox11.Value <> "" Then
x = CLng(TextBox11.Value) + CLng(TextBox12.Value) + CLng(TextBox13.Value) + _
CLng(TextBox14.Value) + CLng(TextBox15.Value) + CLng(TextBox16.Value) + _
CLng(TextBox17.Value) + CLng(TextBox18.Value) + CLng(TextBox19.Value) + _
CLng(TextBox20.Value)
TextBox21.Value = x
End If

Dodgeitorels
05-22-2009, 02:49 AM
MaximS, you have done it again. After inserting the code into textbox11 through 20 I tried it. Didn't work, Then I realized I had to change textbox number in code to match the textbox it was inserted into......worked like a champ........until I hit reset......then went into debug mode. Finally I realized that I had to add some code to the reset code and now works just like it should. Many many thanks to you. Attached is the working file so any can see what I (we)did to make this file work. Hopefully it will be able to help someone else that needs the help.