Add NEW entry to Database:
Option Explicit
Dim ctl
Dim list As ListObject
Dim n As Long, f As Long, X As Long
Dim Cell As Range, Cell2 As Range, oneCell As Range
Dim sh As Worksheet, sheet As Worksheet
Dim config As Worksheet
Private Sub cbutNEW_Click()
'Add new whatever to next free row on Database
'Stop screen jumping
Application.ScreenUpdating = False
'Remove filter if applied to Database sheet (assumed Database is sheet 1):
If Table5.FilterMode Then
Table5.ShowAllData
With Table5
'Find first free row
n = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(n, 1).Value = tbox or cbox whatever.Value
.Cells(n, 2).value = tbox or cbox whatever.Value
.Cells(n, 3) .value= tbox or cbox whatever.Value
etc. . .
'If you want to resort Database alphabetically, assuming data is in Table format:
Table5.Select
.Range("Table5[[#Headers],[WHATEVER]]").Select
.Range(Selection, Selection.End(xlDown)).Select
.Range("Table5[#All]").Select
ActiveWorkbook.sheet1.ListObjects("Table1").Sort.SortFields. _
Clear
ActiveWorkbook.sheet1.ListObjects("Table1").Sort.SortFields. _
Add2 Key:=Range("Table5[WHATEVER]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.sheet1.ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
aPpLICATION.ScreenUpdating = True
End With
End Sub
FIND an existing entry and upload data to User Form, assuming Database is Sheet 1:
Private Sub cbutFIND_Click()
application.ScreenUpdating = False
'Find whatever in Database
'Find last record
Table5.Select
With tABLE5
n = .Cells(.Rows.Count, "A").End(xlUp).Row
'Select data in Col A (assuming that is where the Match has to be made?)
.Range("A2:A" & n).Select
'Find whatever in the list
Set Cell = Selection.Find(What:=tbox or cbox whatever.Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
Exit Sub
Else
'Copy active row into User Form
X = Cell.Row
tbox or cbox whatever = .Cells(X, whichever column matches)
tbox or cbox whatever = .Cells(X, whichever column matches)
tbox or cbox whatever= .Cells(X, whichever column matches)
etc. . . .
End If
End With
application.ScreenUpdating = False
End Sub
UPDATE the Database record after changing something in the User Form:
Private Sub cbutUPDATE_Click()
application.ScreenUpdating = False
'Find last row in whatever column of database (assumes Database is sheet1)
With Table5
f = .Cells(.Rows.Count, "A").End(xlUp).Row
'Find whatever in Database
Table5.Select
.Range("A2:A" & f).Select
'Find whatever in Database
Set Cell = Selection.Find(What:=tbox or cbox whatever, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Cell Is Nothing Then
Exit Sub
Else
'Find whatever in the list
X = Cell.Row
'Update database record
.Cells(X, 1).Value = tbox or cbox whatever. value
.Cells(X, 2).Value = tbox or cbox whatever. value
.Cells(X, 3).Value = tbox or cbox whatever. value
etc. . .
End If
End If
End With
application.ScreenUpdating = True
End Sub
Hope this helps
Ochimus
Bookmarks