• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Userform Input data to Next blank Row for each data entry

Korekurd

New Member
1597168415458.png


were Textbox1 = Employee Number
and Textbox2 = Line Chooses

VBA Code:

>>> use code - tags <<<
Code:
Private Sub CommandButton1_Click()

Dim EmpID$, dDate$, Rg As Range, i&, x&
EmpID = TextBox1.Value
If TextBox1.Value = "" Or TextBox2.Value = "" Then
   MsgBox "PLEASE FILL IN EMPLOYEE NUMBER & ENTER BID LINE CHOOSES ... ", vbExclamation: Exit Sub
End If

If Not IsNumeric(EmpID) Then MsgBox "INVALID EMPLOYEE NUMBER ENTER NUMBER WITHOUT THE E", vbExclamation: Exit Sub

With Sheets("SHIFT BIDS")
   For i = 1 To 2
      dDate = IIf(i = 1, TextBox1.Value, TextBox2.Value)
      If Len(dDate) > 0 Then
         Set Rg = .Columns(1).Find(EmpID, lookat:=xlWhole)
         If Rg Is Nothing Then
            MsgBox "EMPLOYEE NUMBER DOES NOT EXIST PLEASE TRY AGAIN", vbExclamation: Exit Sub
         Else
            Set Rg = IIf(i = 1, ThisWorkbook.Sheets("Bid Data").Cells(, "D").Resize(500), ThisWorkbook.Sheets("Bid Data").Cells(, "E").Resize(, 500))
            If InStr(dDate, ".") Then
               For x = 0 To UBound(Split(dDate, "."))
               Next
               For x = 0 To UBound(Split(dDate, "."))
                  Rg.Find("", after:=Rg.Cells(Rg.Count)) = Split(dDate, ".")(x)
               Next
            Else
               Rg.Find("", after:=Rg.Cells(Rg.Count)) = dDate
            End If
         End If
      End If
   Next
End With
TextBox1.Value = ""
TextBox2.Value = ""
TextBox1.SetFocus
End Sub
[code]

when I type in 9324 in Textbox1 and 1.2.3 and textbox2 and press enter on the first set of data then type 6475 textbox1 and 10.9.8 in textbox2 and press enter for the second set of data The results I get with the above code is as follows:
[TABLE]
[TR]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[TH]H[/TH]
[TH]I[/TH]
[TH]J[/TH]
[/TR]
[TR]
[TD]9324[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]10[/TD]
[TD]9[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]6475[/TD]
[TD][/TD]

[TD][/TD]

[TD][/TD]

[TD][/TD]

[TD][/TD]

[TD][/TD]
[/TR]
[/TABLE]

What I need done is the second data of textbox2 to go with second data of textbox1 so results should be:
[TABLE]
[TR]
[TH]D[/TH]
[TH]E[/TH]
[TH]F[/TH]
[TH]G[/TH]
[TH]H[/TH]
[TH]I[/TH]
[TH]J[/TH]
[/TR]
[TR]
[TD]9324[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD][/TD]

[TD][/TD]

[TD][/TD]
[/TR]
[TR]
[TD]6475[/TD]
[TD]10[/TD]
[TD]9[/TD]
[TD]8[/TD]
[TD][/TD]

[TD][/TD]

[TD][/TD]
[/TR]
[/TABLE]


any help is greatly appreciated.
 
Last edited by a moderator:
.
I tried to sort out your macro code layout. If you original code in your workbook is not indented as it should be, that is the cause of your
problems. Without indenting it is very easy to lose track of the IF/END IF, FOR/NEXT, etc. etc.

This is as far as I got with trying to straighten things out :

Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim EmpID$, dDate$, Rg As Range, i&, x&
EmpID = TextBox1.Value

    If TextBox1.Value = "" Or TextBox2.Value = "" Then
        MsgBox "PLEASE FILL IN EMPLOYEE NUMBER & ENTER BID LINE CHOOSES ... ", vbExclamation: Exit Sub
    End If

    If Not IsNumeric(EmpID) Then MsgBox "INVALID EMPLOYEE NUMBER ENTER NUMBER WITHOUT THE E", vbExclamation: Exit Sub
    
    With Sheets("SHIFT BIDS")
    
        For i = 1 To 2
            dDate = IIf(i = 1, TextBox1.Value, TextBox2.Value)
            
            If Len(dDate) > 0 Then
            Set Rg = .Columns(1).Find(EmpID, lookat:=xlWhole)
                If Rg Is Nothing Then
                    MsgBox "EMPLOYEE NUMBER DOES NOT EXIST PLEASE TRY AGAIN", vbExclamation: Exit Sub
                Else
                    Set Rg = IIf(i = 1, ThisWorkbook.Sheets("Bid Data").Cells(, "D").Resize(500), ThisWorkbook.Sheets("Bid Data").Cells(, "E").Resize(, 500))
                    If InStr(dDate, ".") Then
                        For x = 0 To UBound(Split(dDate, "."))
                        Next
                    
                        For x = 0 To UBound(Split(dDate, "."))
                            Rg.Find("", after:=Rg.Cells(Rg.Count)) = Split(dDate, ".")(x)
                            Next
                    Else
                        Rg.Find("", after:=Rg.Cells(Rg.Count)) = dDate
                    End If
                End If
            End If
        Next
    End With
TextBox1.Value = ""
TextBox2.Value = ""
TextBox1.SetFocus
End Sub


If you original code is properly indented, please post it here for review.
 
.
Here is another method of writing data to rows :

Code:
Option Explicit
Private Sub btnCancel_Click()
    Unload Me
End Sub

Private Sub btnOK_Click()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim newRow As Long
    
    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    'The next two lines can be expanded as many times as needed for all the entry fields in your project
    
    ws.Cells(newRow, 1).Value = Me.txtFirstName.Value
    ws.Cells(newRow, 2).Value = Me.txtSurname.Value
    
End Sub
Sub CommandButton1_Click()
    Selection.EntireRow.Delete
End Sub

With just a small amount of editing, you can utilize it for your project.
 

Attachments

  • Simple Database.xlsm
    20.8 KB · Views: 14
Back
Top