Improving range references to Table rows (dynamic instead of fixed)

GTS

Board Regular
Joined
Aug 31, 2009
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hello,

The following is part of a larger macro. The end result is that I want to take the currently selected (active) row and move it to the top of the table (row 9 in my case). I'm doing this by inserting a blank row at the top, then copying the active row to the blank row at the top, then deleting the original row.

1) There is probably a cleaner way to do this. Eg How would I simply "move active row to top"?
2) I'm referencing exact columns in my ranges. eg A9:BA9. If the table size changes (new column added) or header row moves, then this macro segment fails. I'm struggling with how to reference the table row dynamically. My table name is Cap_Equip. Worksheet name is Cap Equip. Something along the line of ThisWorkbook.Worksheets("Cap Equip").Range("Cap_Equip[Active Row]") and ThisWorkbook.Worksheets("Cap Equip").Range("Cap_Equip[First Data Row]")

Appreciate the help.

VBA Code:
Dim Tbl As ListObject
   On Error Resume Next
   Set Tbl = Selection.ListObject
   On Error GoTo 0
  
   Dim rowCurrent As Long
    rowCurrent = Selection.Row
    newCurrent = rowCurrent + 1
    
    If rowCurrent <> 8 And rowCurrent <> 9 And Not Tbl Is Nothing Then 'Checking not on header or row 1.
        Range("A9:BA9").Insert Shift:=xlDown 'insert blank row at top
        Range("A" & newCurrent & ":BA" & newCurrent).Copy Destination:=Range("A9:BA9") 'copy selected row to top of table
        Range("A" & newCurrent).Delete 'delete the row that has been copied to the top
    End If
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Tbl.ListRows.Add(1) will insert a new row at the top of the table and return a ListRow object that you can use to copy to.
 
Upvote 0
Thanks Rory. I see how that works for my first line of code after the IF - It inserts a blank row at the top of the table, very nice.
I'm lost though with the second part of your answer, and how I can adapt the next 2 lines of code so that they will work regardless of the # of columns in the table.
Sorry, but my coding skills are limited.
 
Upvote 0
Something like this:

VBA Code:
   Dim Tbl As ListObject
   On Error Resume Next
   Set Tbl = Selection.ListObject
   On Error GoTo 0
  
   If Not Tbl Is Nothing Then
      Dim rowCurrent As Long
      rowCurrent = ActiveCell.Row - Tbl.DataBodyRange.Row + 1
      
      If rowCurrent > 1 Then 'Checking not on header or row 1.
         Dim newRow As ListRow
         Set newRow = Tbl.ListRows.Add(1)
         With Tbl.ListRows(rowCurrent + 1)
            .Range.Copy newRow.Range(1)
            .Delete
         End With
      End If
   End If
 
Upvote 0
Solution
Wow... Wish I could write code like that. Works perfectly, thank you so much.
MrExcel forums are the best.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,111
Members
453,021
Latest member
Justyna P

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