Hi all,
I have some code to add rows to the bottom of a spreadsheet. It asks the user how many rows they want to add, and then puts them in. I want it to find the last row of data and put in new rows above that, but it's finding the last row of data and inserting the new rows below that.
Not sure what the fix is here, anybody see something obvious? Thanks for any help.
-------------------------------------
Sub AddRow()
ActiveSheet.Unprotect Password:="Thirdparty"
Dim vRows As Long
Dim sht As Worksheet, shts() As String, i As Long
Cells.Find(What:="New Site", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
'ActiveCell.Offset(-1, -1).Select
ActiveCell.EntireRow.Select
vRows = _
Application.InputBox(prompt:= _
"How many rows do you want to add?", Title:="Add Rows", _
Default:=1, Type:=1)
If vRows = False Then Exit Sub
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedSheets
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name
Selection.Resize(rowsize:=2).Rows(2).EntireRow. _
Resize(rowsize:=vRows).Insert Shift:=xlDown
Selection.AutoFill Selection.Resize(rowsize:=vRows + 1), _
xlFillDefault
On Error Resume Next
Selection.Offset(1).Resize(vRows).EntireRow. _
SpecialCells(xlConstants).ClearContents
Next sht
Worksheets(shts).Select
ActiveSheet.Protect Password:="Thirdparty"
End Sub
I have some code to add rows to the bottom of a spreadsheet. It asks the user how many rows they want to add, and then puts them in. I want it to find the last row of data and put in new rows above that, but it's finding the last row of data and inserting the new rows below that.
Not sure what the fix is here, anybody see something obvious? Thanks for any help.
-------------------------------------
Sub AddRow()
ActiveSheet.Unprotect Password:="Thirdparty"
Dim vRows As Long
Dim sht As Worksheet, shts() As String, i As Long
Cells.Find(What:="New Site", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
'ActiveCell.Offset(-1, -1).Select
ActiveCell.EntireRow.Select
vRows = _
Application.InputBox(prompt:= _
"How many rows do you want to add?", Title:="Add Rows", _
Default:=1, Type:=1)
If vRows = False Then Exit Sub
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedSheets
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name
Selection.Resize(rowsize:=2).Rows(2).EntireRow. _
Resize(rowsize:=vRows).Insert Shift:=xlDown
Selection.AutoFill Selection.Resize(rowsize:=vRows + 1), _
xlFillDefault
On Error Resume Next
Selection.Offset(1).Resize(vRows).EntireRow. _
SpecialCells(xlConstants).ClearContents
Next sht
Worksheets(shts).Select
ActiveSheet.Protect Password:="Thirdparty"
End Sub