Code to insert rows throws a "400" error

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
308
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a button on one of my sheets to allow the user to add rows. An input box pops up and asks them how many rows they would like to add.

Here's my code:

Code:
Sub AddRows_Button()

Dim NumberOfRowsInLog As Integer
Dim LastRowInLog As Integer
Dim RowsToAdd As Variant

NumberOfRowsInLog = Range("Number_of_Rows_currently").Value
LastRowInLog = 8 + NumberOfRowsInLog

RowsToAdd = InputBox("How many rows would you like to add?")

If Not IsNumeric(RowsToAdd) Then
    MsgBox "You must enter a number in order to add rows."

Else
    Range("B" & LastRowInLog + 1).Select
    ActiveCell.EntireRow.Resize(Int(RowsToAdd)).Insert

End If

The line that beings with ActiveCell is throwing the error. I am 99% sure I tested this code the other day and it worked fine. So I am not sure why it suddenly doesn't work. Any ideas?

BTW, I'm using Excel 2016.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try
Code:
ActiveCell.EntireRow.Resize(RowsToAdd).Insert
 
Upvote 0
Unfortunately, that did not solve it.

I've also tried changing that line and adding a line to convert the Input Box result to Int separately:

Code:
Dim RowsToAddInteger As Integer
RowsToAddInteger = CInt(RowsToAdd)
ActiveCell.EntireRow.Resize(RowsToAddInteger).Insert

Same error.

Then I tried rewriting the Insert line completely:

Code:
Dim ActiveRow As Integer
ActiveRow = ActiveCell.Row
Rows(ActiveRow & ":" & RowsToAddInteger).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

Same error.

I am at a loss for what could be wrong.
 
Last edited:
Upvote 0
Any possibility that your activecell is fewer rows from the bottom of the sheet than RowsToAdd?
 
Upvote 0
I just ran across a post on another forum with someone having a similar problem, and someone suggested that maybe they had some data in the last row of the sheet. I couldn't see how that could possibly be the case for me, but I went all the way to the bottom of the sheet, and deleted the last 300 rows. Tried the AddRows button after that, and it worked. I am at a total loss as to how any data could have gotten that far down in my sheet. Crazy!
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
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