View Full Version : Adding a Row Using VBA


hrbreton
02-05-2011, 08:42 AM
Hi All,

I have a spreadsheet (testing - attached) that has several sections with buttons that add and remove rows for that section. In my attached spreadsheet, the add button is only functional for Objectives. My code adds a new row after whatever cell is selected. I need it to go to a cell I've named "OBJ", select the row above it, copy and paste so the cell "OBJ" gets moved down to the next row. The code I have so far is :

Sub AddRowDeleteConstants()
On Error GoTo NoConstants
With ActiveCell.EntireRow
.Copy
.Insert Shift:=xlDown
.SpecialCells(xlCellTypeConstants).ClearContents
End With
Exit Sub
NoConstants:
MsgBox "There were no constants to delete"
End Sub

Any help would be greatly appreciated.

Thanks.

lagbolt
02-05-2011, 04:54 PM
Hey there, welcome to the forum!
To work with a named cell, check out the names collection. In your file I can do this in the immediate window ...
? thisworkbook.Names("OBJ").RefersTo
='union 2011 2012'!$A$35
So you can determine where that named cell is which seems important. Or get the row using ...
? thisworkbook.Names("OBJ").RefersToRange.Row

hrbreton
02-06-2011, 08:55 AM
Solution found. Thanks to those who read my post and especially thanks for replies. The code I am using is:

Application.EnableEvents = False
Application.ScreenUpdating = False
With ActiveSheet
.Protect Password:="PASSWORD", _
UserInterFaceOnly:=True
Application.GoTo reference:="OBJ"
ActiveCell.Offset(-1, 0).Range("A1:AM1").Select
Selection.Copy
Application.GoTo reference:="OBJ"
Selection.Insert Shift:=xlDown
Application.GoTo reference:="OBJ"
ActiveCell.Offset(-1, 0).Range("A1:AM1").Select
Selection.ClearContents
End With