Dim ws As Worksheet
Dim r As Long, LastRow As Long
Const FirstRow As Long = 2
Dim EventEnable As Boolean
[HR][/HR]
Private Sub FirstRecord_Click()
Navigate Direction:=xlFirst
End Sub
[HR][/HR]Private Sub LastRecord_Click()
Navigate Direction:=xlLastCell
End Sub
[HR][/HR]Private Sub NextRecord_Click()
Navigate Direction:=xlNext
End Sub
[HR][/HR]Private Sub PrevRecord_Click()
Navigate Direction:=xlPrevious
End Sub
[HR][/HR]Private Sub CustomerID_Change()
If Not EventEnable Then Exit Sub
r = Me.CustomerID.ListIndex + 2
Navigate Direction:=xlRowItem
End Sub
[HR][/HR]Private Sub RowNumber_Change()
If Not EventEnable Then Exit Sub
If IsNumeric(RowNumber.Text) Then
r = Val(RowNumber.Text)
If r >= FirstRow Then Navigate Direction:=xlRowItem
Else
Navigate Direction:=xlNone
End If
End Sub
[HR][/HR]Private Sub UserForm_Initialize()
Set ws = ThisWorkbook.Worksheets("Customers")
With Me.CustomerID
.RowSource = ""
.List = ws.Range("CDB").Value
End With
'start at first record
Navigate Direction:=xlFirst
End Sub
[HR][/HR]Private Sub FindLastRow()
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
End Sub
[HR][/HR]Sub Navigate(ByVal Direction As XlSearchDirection)
Dim i As Integer
' Dim LastRow As Long
Dim ClearRecord As Boolean
FindLastRow
' LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Select Case Direction
Case xlFirst
r = FirstRow
Case xlPrevious
r = r - 1
Case xlNext
r = r + xlNext
Case xlLastCell
r = LastRow
Case xlRowItem
r = r
Case xlNone
ClearRecord = True
r = 1
End Select
If r < FirstRow Then r = FirstRow
If r > LastRow Then r = LastRow
EventEnable = False
'get record
For i = 1 To UBound(ControlNames)
Me.Controls(ControlNames(i)).Text = IIf(ClearRecord, "", ws.Cells(r, i).Text)
Next i
'set enabled status of navigate buttons
Me.NextRecord.Enabled = r < LastRow
Me.PrevRecord.Enabled = r > FirstRow
Me.LastRecord.Enabled = Me.NextRecord.Enabled
Me.FirstRecord.Enabled = Me.PrevRecord.Enabled
Me.SaveRecord.Enabled = Not ClearRecord
If Not ClearRecord Then Me.RowNumber.Text = FormatNumber(r, 0)
EventEnable = True
End Sub
[HR][/HR]Private Sub NewRecord_Click() 'Add New record
FindLastRow
RowNumber.Text = FormatNumber(LastRow, 0)
LastRow = LastRow + 1
ActiveSheet.Columns(1).Rows(LastRow).Select
ActiveCell.EntireRow.Insert
SaveRecord.Enabled = True
NewRecord.Enabled = False
End Sub
[HR][/HR]Private Sub Cancel_Click() 'Cancel button
Unload Customers
End Sub
[HR][/HR]Private Sub SaveRecord_Click() 'Save Button for registering new customers
PutData
NewRecord.Enabled = True
End Sub
[HR][/HR]Private Sub PutData()
If IsNumeric(RowNumber.Text) Then
r = CLng(RowNumber.Text)
Else
MsgBox "Illegal row number"
Exit Sub
End If
If r > 1 And r <= LastRow Then '>1
Cells(r, 1) = CustomerID.Text
Cells(r, 2) = CustomerName.Text
Cells(r, 3) = Address1.Text
Cells(r, 4) = Address2.Text
Cells(r, 5) = Prefix.Text
Cells(r, 6) = Zip.Text
Cells(r, 7) = City.Text
Cells(r, 8) = Country.Text
Cells(r, 9) = Delivery1.Text
Cells(r, 10) = Delivery2.Text
Cells(r, 11) = DelZip.Text
Cells(r, 12) = DelTown.Text
Cells(r, 13) = DelPoint.Text
Cells(r, 14) = BaseCurrency.Text
Cells(r, 15) = Region.Text
Cells(r, 16) = Contact.Text
Cells(r, 17) = Telephone.Text
Cells(r, 18) = Account.Text
Cells(r, 19) = VATRegNo.Text
Cells(r, 20) = RowNumber.Text
Else
MsgBox "Invalid row number"
End If
End Sub