sspatriots
Well-known Member
- Joined
- Nov 22, 2011
- Messages
- 585
- Office Version
- 365
- Platform
- Windows
I'm posting 4 codes that I have on this Userform of mine, because when I pull up a record using the "cmdFindPO_Click" code to pull up a record, I want to be able to edit one of the other fields and the select the "cmdUpdatePOData_Click" button to update the record in my worksheet. Right now, when I select the "cmdFindPO_Click" button nothing happens. Not sure what I have wrong here. Any advice would be greatly appreciated. Thanks, Steve
VBA Code:
Private Sub cmdClearForm_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End Sub
Private Sub cmdCreateNewPO_Click()
Dim ws As Worksheet
Dim LO As ListObject
Dim Lastrow As Long
Dim C As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set LO = ws.ListObjects("Table1")
With LO.Range.Columns(2) 'column_to_check is relative to the LO.Range
Set C = .Find(what:="*", after:=.Cells(1), LookIn:=xlValues, _
searchorder:=xlByRows, searchdirection:=xlPrevious)
If Not C Is Nothing Then
Debug.Print C.Row + 1 'last empty row
TextBox1.Text = C.Offset(1, -1).Value
TextBox1.Text = Format(TextBox1.Text, "0000")
If C.Offset(1, -1).Value = "" Then
MsgBox "There is no allocated job number available" & vbLf & _
"Please have additional Job numbers allocated." & vbLf & _
"Will now be exiting this form."
Exit Sub
Else
'do stuff`
Lastrow = C.Row + 1
With ws
' .Cells(Lastrow, 1).Value = TextBox1.Text
.Cells(Lastrow, 2).Value = TextBox2.Text
.Cells(Lastrow, 3).Value = TextBox3.Text
.Cells(Lastrow, 4).Value = TextBox4.Text
End With
End If
End If
End With
'''''' TextBox2.Value = ""
'''''' TextBox3.Value = ""
'''''' TextBox4.Value = ""
End Sub
Private Sub cmdFindPO_Click()
Dim PO_No As String
Dim Lastrow As Long
Dim i As Integer
PO_No = Trim(TextBox1.Text)
TextBox1.Text = Format(TextBox1.Text, "0000")
Lastrow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow 'The 2 means we are starting in the second row
If Worksheets("sheet1").Cells(i, "A").Value = PO_No Then
TextBox2.Text = Worksheets("Sheet1").Cells(i, 2).Value
TextBox3.Text = Worksheets("Sheet1").Cells(i, 3).Value
TextBox4.Text = Worksheets("Sheet1").Cells(i, 4).Value
End If
Next
End Sub
Private Sub cmdUpdatePOData_Click()
Dim ws As Worksheet
Dim tb As ListObject
Dim frm As Object 'UserForm
Dim PO_No As String
Dim i As Long
Dim Lastrow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
Set tb = ws.ListObjects("Table1")
Set frm = FindUpdateorCreatePOUF
With frm
PO_No = Trim(.TextBox1.Text)
TextBox1.Text = Format(TextBox1.Text, "0000")
For i = 1 To tb.DataBodyRange.Rows.Count
If tb.ListColumns("PO No").DataBodyRange.Cells(i).Value = PO_No Then
tb.ListColumns("First Name").DataBodyRange.Cells(i).Value = .TextBox2.Text
tb.ListColumns("Last Name").DataBodyRange.Cells(i).Value = .TextBox3.Text
tb.ListColumns("Occupation").DataBodyRange.Cells(i).Value = .TextBox4.Text
Exit For
End If
Next
End With
End Sub