Forum Discussion
Mailutoronto
Aug 15, 2021Copper Contributor
How do you create table that automatically add or delete rows?
Suppose I have the tools list and its changes tabulated below: Then in the bottom table I recorded a change of adding 2 bolts. How do you make the tool table automatically add a row in the top...
- Aug 15, 2021
One way is to use VBA in order to achieve this functionality.
In the attached, you will find two tables named "Main" and "Change" where table "Main' contains some records. And once you start adding records in the Change Table, the Main Table will update accordingly.
The Sheet1 Module Contains the following Change_Event Code in it.
Private Sub Worksheet_Change(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub Dim tblMain As ListObject Dim tblChange As ListObject Dim n As Variant Dim r As ListRow Set tblMain = ActiveSheet.ListObjects("Main") Set tblChange = ActiveSheet.ListObjects("Change") On Error GoTo Skip If Not Intersect(Target, tblChange.DataBodyRange.Cells) Is Nothing Then Application.EnableEvents = False If Target.Column = tblChange.DataBodyRange.Columns(1).Column Then n = Application.Match(Target.Value, tblMain.DataBodyRange.Columns(1), 0) If IsError(n) Then If tblMain.DataBodyRange.Cells(1) = "" Then Set r = tblMain.ListRows.Add(1) Else Set r = tblMain.ListRows.Add End If r.Range(1, 1).Value = Target End If Target.Offset(0, 2).Value = Date Target.Offset(0, 2).NumberFormat = "dd-mmm-yyyy" ElseIf Target.Column = tblChange.DataBodyRange.Columns(2).Column Then n = Application.Match(Target.Offset(0, -1).Value, tblMain.DataBodyRange.Columns(1), 0) If Not IsError(n) Then Set r = tblMain.ListRows(n) r.Range(1, 2).Value = r.Range(1, 2).Value + Target.Value If r.Range(1, 2).Value = 0 Then tblMain.ListRows(n).Delete End If End If Target.Offset(0, 1).Value = Date Target.Offset(0, 1).NumberFormat = "dd-mmm-yyyy" End If End If Skip: Application.EnableEvents = True End Sub
Subodh_Tiwari_sktneer
Aug 15, 2021Silver Contributor
One way is to use VBA in order to achieve this functionality.
In the attached, you will find two tables named "Main" and "Change" where table "Main' contains some records. And once you start adding records in the Change Table, the Main Table will update accordingly.
The Sheet1 Module Contains the following Change_Event Code in it.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim tblMain As ListObject
Dim tblChange As ListObject
Dim n As Variant
Dim r As ListRow
Set tblMain = ActiveSheet.ListObjects("Main")
Set tblChange = ActiveSheet.ListObjects("Change")
On Error GoTo Skip
If Not Intersect(Target, tblChange.DataBodyRange.Cells) Is Nothing Then
Application.EnableEvents = False
If Target.Column = tblChange.DataBodyRange.Columns(1).Column Then
n = Application.Match(Target.Value, tblMain.DataBodyRange.Columns(1), 0)
If IsError(n) Then
If tblMain.DataBodyRange.Cells(1) = "" Then
Set r = tblMain.ListRows.Add(1)
Else
Set r = tblMain.ListRows.Add
End If
r.Range(1, 1).Value = Target
End If
Target.Offset(0, 2).Value = Date
Target.Offset(0, 2).NumberFormat = "dd-mmm-yyyy"
ElseIf Target.Column = tblChange.DataBodyRange.Columns(2).Column Then
n = Application.Match(Target.Offset(0, -1).Value, tblMain.DataBodyRange.Columns(1), 0)
If Not IsError(n) Then
Set r = tblMain.ListRows(n)
r.Range(1, 2).Value = r.Range(1, 2).Value + Target.Value
If r.Range(1, 2).Value = 0 Then
tblMain.ListRows(n).Delete
End If
End If
Target.Offset(0, 1).Value = Date
Target.Offset(0, 1).NumberFormat = "dd-mmm-yyyy"
End If
End If
Skip:
Application.EnableEvents = True
End Sub
- MailutorontoAug 17, 2021Copper ContributorThis includes the function that I imagined. However, it does include flows. No need to fix because I think I'll have to learn VBA now and I wasn't exactly expecting this since almost all of my previous sheets were well handled by Excel without VBA/Macros.
The rows auto add and auto deleted based on variables that is quite narrow and leads to some malfunctions. I'll have to include more rules myself.
Can you point me to certain topics in VBA to learn what you did? or do I have to learn everything from scratch?