Consulting

Results 1 to 8 of 8

Thread: Userform for Entering Data in Excel VBA

  1. #1
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    5
    Location

    Userform for Entering Data in Excel VBA

    Hello All,
    I have to make a Userform in Excel VBA for entering value from "Textbox1" and write that value in the specific places(in a table) in the worksheet. But when I enter a number, it changes all the cells to the last number. I can't find my error. I would be appreciated if someone could help me.
    I attached my excel file. Thanks in advance
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    there are many many errors in your code, see comments

    Private Sub CommandButton1_Click()
        
        IniRow = 6 ' this value is constant, you have to check the last non empty row 
        IniCol = 2
        Time = ComboBox3.List(ComboBox3.ListIndex)
        Concentration = ComboBox2.List(ComboBox2.ListIndex)
        Name = ComboBox1.List(ComboBox1.ListIndex)
        Measurment = TextBox1.Value
        ' you have to check the optionbuttons to find the true
    
        
        If Name = "Encanto Park Lake" Then
            Worksheets("Encanto Park Lake").Activate
                If Nutrient = OptionButton1.Value And Concentration = "20" And Time = "First" Then ' nutrient is not defined
                  Lines(1) = Measurment
            Else
    Worksheets("Rio Salado").Activate ' this is missing
                If Nutrient = OptionButton1.Value And Concentration = "2" And Time = "First" Then
                  Lines(2) = Measurment
                 
                    End If
                End If
            End If
            
            
            
         Cells(IniRow, IniCol).Value = Lines(1)
         Cells(IniRow, IniCol + 1).Value = Lines(2)
         CloseAgrowth
      
    End Sub
    
    
    Private Sub CommandButton2_Click()
        CloseAgrowth
    End Sub
    to do
    1) select the correct Sheet
    2) find the true optionbutton or use combobox for selection
    3) find the last used row in column B or J or R , based on true optionbutton
    4) paste entered value in the last row and correct column

  3. #3
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    3
    Location
    What are InRow and InCol ?

  4. #4
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    5
    Location
    Thank you for your comment. I am a beginner in VBA programming.

  5. #5
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    5
    Location
    Thank you for your comment. I am a beginner in VBA programming.
    I tried to change my macro base on your comments but it still write zero in all cells.
    Would you please help me more?

    Private Sub CommandButton1_Click()
    Dim Name As String
    Dim Concentration As Integer
    Dim Measurment As Integer
    Dim Data As Integer
    Dim Nutrient As String
    Dim Time As String

    Dim i As Integer, j As Integer
    Dim Lines(6) As Integer







    Time = ComboBox3.List(ComboBox3.ListIndex)
    Concentration = ComboBox2.List(ComboBox2.ListIndex)
    Name = ComboBox1.List(ComboBox1.ListIndex)
    Measurment = TextBox1.Value



    If Name = "Encanto Park Lake" Then
    Worksheets("Encanto Park Lake").Activate
    If Nutrient = OptionButton1.Value And Concentration = "20" And Time = "First" Then
    Lines(1) = Measurment
    Else
    If Nutrient = OptionButton1.Value And Concentration = "2" And Time = "First" Then
    Lines(2) = Measurment

    End If
    End If
    End If

    Range("B6").Value = Lines(1)
    Range("C6").Value = Lines(2)


    If Name = "Rio Salado River" Then
    Worksheets("Rio Salado").Activate
    If Nutrient = OptionButton1.Value And Concentration = "20" And Time = "First" Then
    Lines(3) = Measurment
    Else
    If Nutrient = OptionButton1.Value And Concentration = "2" And Time = "First" Then
    Lines(4) = Measurment

    End If
    End If
    End If



    Range("B6").Value = Lines(3)
    Range("C6").Value = Lines(4)

    CloseAgrowth

    End Sub


    Quote Originally Posted by patel View Post
    there are many many errors in your code, see comments

    Private Sub CommandButton1_Click()
        
        IniRow = 6 ' this value is constant, you have to check the last non empty row 
        IniCol = 2
        Time = ComboBox3.List(ComboBox3.ListIndex)
        Concentration = ComboBox2.List(ComboBox2.ListIndex)
        Name = ComboBox1.List(ComboBox1.ListIndex)
        Measurment = TextBox1.Value
        ' you have to check the optionbuttons to find the true
    
        
        If Name = "Encanto Park Lake" Then
            Worksheets("Encanto Park Lake").Activate
                If Nutrient = OptionButton1.Value And Concentration = "20" And Time = "First" Then ' nutrient is not defined
                  Lines(1) = Measurment
            Else
    Worksheets("Rio Salado").Activate ' this is missing
                If Nutrient = OptionButton1.Value And Concentration = "2" And Time = "First" Then
                  Lines(2) = Measurment
                 
                    End If
                End If
            End If
            
            
            
         Cells(IniRow, IniCol).Value = Lines(1)
         Cells(IniRow, IniCol + 1).Value = Lines(2)
         CloseAgrowth
      
    End Sub
    
    
    Private Sub CommandButton2_Click()
        CloseAgrowth
    End Sub
    to do
    1) select the correct Sheet
    2) find the true optionbutton or use combobox for selection
    3) find the last used row in column B or J or R , based on true optionbutton
    4) paste entered value in the last row and correct column

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Change the name on the tab of sheet Rio Salado to Rio Salado River to match what you've put in the combobox, then put this to replace your CommandButton1_Click event handler:
    Private Sub CommandButton1_Click()
    Dim NutrientOffset As Long
    
    If OptionButton1 Then NutrientOffset = 0
    If OptionButton2 Then NutrientOffset = 1
    If OptionButton3 Then NutrientOffset = 2
    NutrientOffset = NutrientOffset * 8
    Worksheets(ComboBox1.Value).Range("B6").Offset(ComboBox3.ListIndex, NutrientOffset + ComboBox2.ListIndex) = TextBox1.Value
    CloseAgrowth
    End Sub
    There are no checks to ensure that the user has selected a nutrient or put a value in the Textbox.
    If this is an assignment I suspect that handing this in will raise an eyebrow, maybe two!
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    5
    Location
    Thank you very much

  8. #8
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    5
    Location
    Thank you very much for your help.
    I really really appreciate it.



    Quote Originally Posted by p45cal View Post
    Change the name on the tab of sheet Rio Salado to Rio Salado River to match what you've put in the combobox, then put this to replace your CommandButton1_Click event handler:
    Private Sub CommandButton1_Click()
    Dim NutrientOffset As Long
    
    If OptionButton1 Then NutrientOffset = 0
    If OptionButton2 Then NutrientOffset = 1
    If OptionButton3 Then NutrientOffset = 2
    NutrientOffset = NutrientOffset * 8
    Worksheets(ComboBox1.Value).Range("B6").Offset(ComboBox3.ListIndex, NutrientOffset + ComboBox2.ListIndex) = TextBox1.Value
    CloseAgrowth
    End Sub
    There are no checks to ensure that the user has selected a nutrient or put a value in the Textbox.
    If this is an assignment I suspect that handing this in will raise an eyebrow, maybe two!

Tags for this Thread

Posting Permissions

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