Hi
I am new to VBA. I know a few basic syntax.
I have the following code.
1) There are many If statements. But everything checks the same condition. Can I reduce the code, like using a for loop or something?
2) I also intend to add a "Clear Values" button, which will clear all values without closing the userform. How can I clear all values? For eg, if I press clear values button, I need all textboxes to be cleared (empty). I could do textbox1.text = ""
textbox2.text = ""
textbox3.text = ""
Is ther an easier method.
3) any good( and free) links to learn VBA(beginners)
Private Sub CommandButton1_Click()
Code:Dim error As Integer error = 0 Dim error_msg As String error_msg = " { Choose an answer!}" If (ComboBox1.Value = "") Then error = 1 Label1.Caption = Label1.Caption & error_msg End If If (ComboBox2.Value = "") Then error = 1 Label2.Caption = Label2.Caption & error_msg End If If (ComboBox3.Value = "") Then error = 1 Label3.Caption = Label3.Caption & error_msg End If If (ComboBox4.Value = "") Then error = 1 Frame1.Caption = Frame1.Caption & error_msg End If If (ComboBox5.Value = "") Then error = 1 Frame2.Caption = Frame2.Caption & error_msg End If If (ComboBox6.Value = "") Then error = 1 Frame3.Caption = Frame3.Caption & error_msg End If If (ComboBox7.Value = "") Then error = 1 Frame4.Caption = Frame4.Caption & error_msg End If If (ComboBox8.Value = "") Then error = 1 Frame5.Caption = Frame5.Caption & error_msg End If If (ComboBox9.Value = "") Then error = 1 Frame6.Caption = Frame6.Caption & error_msg End If If (error = 1) Then MsgBox "Please choose answers", vbInformation Exit Sub End If Worksheets("dbase").Range("a1").End(xlDown).Select ActiveCell.Offset(1, 0) = Now() ActiveCell.Offset(1, 1) = ActiveCell.Offset(0, 1) + 1 ActiveCell.Offset(1, 2) = ComboBox1.Value ActiveCell.Offset(1, 3) = ComboBox2.Value ActiveCell.Offset(1, 4) = ComboBox3.Value ActiveCell.Offset(1, 6) = ComboBox4.Value ActiveCell.Offset(1, 7) = TextBox1.Value ActiveCell.Offset(1, 8) = ComboBox5.Value ActiveCell.Offset(1, 9) = TextBox2.Value ActiveCell.Offset(1, 10) = ComboBox6.Value ActiveCell.Offset(1, 11) = TextBox3.Value ActiveCell.Offset(1, 12) = ComboBox7.Value ActiveCell.Offset(1, 13) = TextBox4.Value ActiveCell.Offset(1, 14) = ComboBox8.Value ActiveCell.Offset(1, 15) = TextBox5.Value ActiveCell.Offset(1, 16) = ComboBox9.Value ActiveCell.Offset(1, 17) = TextBox6.Value End Sub
Last edited by Leith Ross; 06-17-2009 at 03:41 AM. Reason: Added Code Tags
Hello ovonelmonk,
Welocme to the Forum!
To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.
How to wrap your Code
1. Select all your code using the mouse.
2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.
3. You can also do this manually by placing the tags
[code] at the start of the line,
[/code] at the end of the line.
As a member, You have agreed to follow the forum posting rules. Please take so time to familiarize yourself with the Do's and Don'ts here in the Forum, just click on the link below...
Forum Rules
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks