Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim idFind As Range
Dim lr As Long
'Check for company id in field 1
If Len(Trim(TextBox1.Text)) = 0 Then
MsgBox ("You must enter a Company ID")
Exit Sub
End If
'Verify a worksheet exists for company name
If Not Evaluate("=ISREF('" & Left(Trim(TextBox1.Text), 1) & "'!A1)") Then
MsgBox ("A sheet is not available for that Company ID")
Exit Sub
End If
'Find the row with the Company ID
Set ws = Sheets(Left(Trim(TextBox1.Text), 1))
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
Set idFind = ws.Range("A1:A" & lr).Find(Trim(TextBox1.Text), , xlValues, xlWhole)
If Not idFind Is Nothing Then
TextBox2.Text = ws.Range("B" & idFind.Row)
TextBox3.Text = ws.Range("C" & idFind.Row)
TextBox4.Text = ws.Range("D" & idFind.Row)
TextBox5.Text = ws.Range("E" & idFind.Row)
TextBox6.Text = ws.Range("F" & idFind.Row)
TextBox7.Text = ws.Range("G" & idFind.Row)
TextBox8.Text = ws.Range("I" & idFind.Row)
TextBox9.Text = ws.Range("K" & idFind.Row)
End If
End Sub
Private Sub CommandButton2_Click()
Dim ws As Worksheet
Dim lr As Long
Dim nextNum As Integer
Dim strNum As String
'Check for company name in field 2
If Len(Trim(TextBox2.Text)) = 0 Then
MsgBox ("You must enter a Company Name")
Exit Sub
End If
'Verify a worksheet exists for company name
If Not Evaluate("=ISREF('" & Left(Trim(TextBox2.Text), 1) & "'!A1)") Then
MsgBox ("A sheet is not available for that company")
Exit Sub
End If
'Get last row and create new number
Set ws = Sheets(Left(Trim(TextBox2.Text), 1))
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
If lr = 1 Then
strNum = ws.Name & "-1"
Else
nextNum = CLng(Split(ws.Range("A" & lr), "-")(1)) + 1
strNum = ws.Name & "-" & nextNum
End If
'Popuate new row
ws.Range("A" & lr + 1) = strNum
ws.Range("B" & lr + 1) = TextBox2.Text
ws.Range("C" & lr + 1) = TextBox3.Text
ws.Range("D" & lr + 1) = TextBox4.Text
ws.Range("E" & lr + 1) = TextBox5.Text
ws.Range("F" & lr + 1) = TextBox6.Text
ws.Range("G" & lr + 1) = TextBox7.Text
ws.Range("I" & lr + 1) = TextBox8.Text
ws.Range("K" & lr + 1) = TextBox9.Text
End Sub
Bookmarks