VBA for tables

mib1019

Board Regular
Joined
Nov 9, 2017
Messages
66
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I know this is simple. I appreciate your time to help me!

I have a table listobject called "TimeData".

Need VBA to insert row above the current row. And VBA to delete the current row.

Going to put both on buttons above the table. Need the VBA to test that the row is within the range between the header and the total row.

Thanks!
MB
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
something like this maybe:
VBA Code:
Sub listRowClear()
    Dim sh As Worksheet
    Dim obj As ListObject
    Dim ac As Range
    Dim i As Long
   
    Set sh = ActiveSheet
    Set ac = ActiveCell
    Set obj = sh.ListObjects("TimeData")
   
    With obj
        If Intersect(ac, .DataBodyRange) Is Nothing Then GoTo ep
        i = ac.Row - .HeaderRowRange.Row
        .ListRows(i).Delete
        .ListRows.Add i, True
    End With

ep:
    On Error Resume Next
    Set sh = Nothing
    Set obj = Nothing
    Set ac = Nothing
End Sub
I put both of your requirements in one code. Leave this line .ListRows(i).Delete to delete the active row, ot this to insert one: .ListRows.Add i, True
 
Upvote 0
Solution
Here's a couple of links to sites dealing with tables that will help you to understand the parts of tables.
 
Upvote 0
One issue...if the table is filtered, the insert code doesn't work. 'Runtime error 1004-Can't move cells in a filtered range or table.'
 
Upvote 0
Well it really can't.
How do you want to handele this situation - ignore it, get a warning of remove the filtering?
 
Upvote 0
Thanks for your help. I added a test for filter and turned it off if necessary. I'm all set!
 
Upvote 0
Bobsan42, I want to get User OK to delete the table row. Please tell me why if he answers No to the message box, it still deletes the row. Thanks!
Debug is telling me that Response is True regardless of which button clicked. Driving me crazy!

VBA Code:
Sub Delete_Current_Row()

    Dim Response As Boolean
    
    Response = MsgBox("Delete this row?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete Input Row")
    Debug.Print Response
    
    If Response = vbYes Then
        Dim sh As Worksheet
        Dim obj As ListObject
        Dim ac As Range
        Dim i As Long
   
        Set sh = ActiveSheet
        Set ac = ActiveCell
        Set obj = sh.ListObjects("TimeData")
        With obj
            If Intersect(ac, .DataBodyRange) Is Nothing Then GoTo ep
                i = ac.Row - .HeaderRowRange.Row
                .ListRows(i).Delete
        End With
    Else 'vbNo
        Exit Sub
    End If
    
ep:
    On Error Resume Next
    Set sh = Nothing
    Set obj = Nothing
    Set ac = Nothing
     
    
End Sub

Thanks!
 
Upvote 0
You have defined response as boolean.
Leave it as variant or msgbox.... Something.
Vbno is not false or 0 so in converts to boolean as True.
So simplest is:
VBA Code:
Dim Response
A tip: to see the value of vbNo type ?vbNo in the immediate window and press enter.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top