Adding a Row in a table cause an error

dikken20

Board Regular
Joined
Feb 25, 2009
Messages
130
Office Version
  1. 2010
Platform
  1. Windows
Hello,

Why do I get an error on this part (located inside the IF block below) :
VBA Code:
   Cells(RowCnt, 2).EntireRow.Insert = True



VBA Code:
Sub Adding_ROW()
Application.ScreenUpdating = False
    BeginRow = 2
    EndRow = 600
    Inst_Name = Application.InputBox("Enter an Instrument Name")
'   Inst_Serial = Application.InputBox("Enter an Instrument Serial")


    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, 2).Value > Inst_Name Then
            Cells(RowCnt, 2).EntireRow.Insert = True
                     
        End If
    Next RowCnt
  
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
What was the error message & number?
 
Upvote 0
What was the error message & number?

1617630527307.png
 
Upvote 0
Is the sheet protected?
 
Upvote 0
I think that you need to stop the for next loop when a row is inserted. When not then the insert row shall repeat because the next row is the same as the last row before inserting and always greater than the comparative string.
 
Upvote 0
Is the sheet protected?
Nope, it is not protected.


I think that you need to stop the for next loop when a row is inserted. When not then the insert row shall repeat because the next row is the same as the last row before inserting and always greater than the comparative string.
I tried many variations and one was what you suggested, but it's not that, I tried two methods to not getting into that situation you described. First was the operator of the If from < to >, second method was setting the RowCnt to RowCnt+2..
No matter what I do I get this runtime error.


Just to clarify, the "If" statement is checking whether the string in the cell is greater than the Inst_Name variable, purpose is to add a Row in a sorted table, So I guess what I'm asking is that maybe the operator ">" is not the right operator to use in strings? I hardly think that since it does add an empty row just at the right place..

So again, I'm at the same place with that runTime error for some reason.
Any Ideas?
 
Upvote 0
VBA Code:
        If Cells(RowCnt, 2).Value > Inst_Name Then
            Cells(RowCnt, 2).EntireRow.Insert
            RowCnt = EndRow
        End If
 
Upvote 0
There is an other problem. How to add a item after the last row.
VBA Code:
Sub Adding_ROW()
    Application.ScreenUpdating = False
    Dim BeginRow As Long
    Dim EndRow As Long
    Dim RowCnt As Long
    Dim Inst_Name As String
    BeginRow = 2
    EndRow = Cells(Rows.Count, 2).End(xlUp).Row
    Inst_Name = Application.InputBox("Enter an Instrument Name")
    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, 2).Value > Inst_Name Then
            Cells(RowCnt, 2).EntireRow.Insert
            Cells(RowCnt, 2) = Inst_Name
            RowCnt = EndRow
        ElseIf RowCnt = EndRow Then
            Cells(RowCnt + 1, 2) = Inst_Name
        End If
    Next RowCnt
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,065
Members
448,941
Latest member
AlphaRino

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