Results 1 to 2 of 2

Programming Basics - Data Validation

Threaded View

  1. #1
    Registered User
    Join Date
    06-17-2009
    Location
    Earth,Solar System
    MS-Off Ver
    Excel 2007
    Posts
    1

    Programming Basics - Data Validation

    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()
    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 02:41 AM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1