The error specifically is this: Run-time error ' 1004' Insert method of Range class failed
It has frustrated me till no end because I couldn't make heads nor tails out of why it was occurring for the specific sub where it was happening when I also had the same EXACT code in another sub that does the exact same thing and it never trips up an error and has always worked fine... hmm.
First, here is what the code does... (hopefully this will make it easier to follow along with how and why I have the code the way I do...) (hopefully! )
On a userform for entering a new record, the user has a list of names in a combobox that they can select from:
If the name that they need to enter isn't found in the combobox list, then they can hit another command button (indicated above by the little red arrow) that will open a 'popup' form where the new name can be entered.
This will open up a popup form where the new name can be added:
After entering a new name, and hitting the 'Add The New Name' command button, the code adds the new entry to the specific 'named table range' (which is "Table2"), then closes out the 'popup form' and adds the new name to the combobox field that they were on.
Its upon clicking on the 'Add The New Name" button where the code breaks.
To cut to the chase, the issue turned out to be that when I have 2 named table ranges next to each other AND on the same worksheet, I get the error.
And here is the error and the line where it breaks:
(edit... the code above references "Table24"... this is really Table2. I was just changing many things around trying to find out why/how the error was occurring and I copied the code from one of those changes.)
Here is the worksheet where the two named table ranges are and what they look like: (Where the 'named table range' in column A is "Table1", and the one in column B is "Table2")
I finally figured out the problem after trying all kinds of different ways to execute the code. What finally allowed the code to work, was when I moved one of the named ranges onto a separate worksheet, everything then suddenly worked just fine. (and didn't matter which named table range either, just moving either one of them onto a different sheet allowed both of them to work.)
When I would go to move the other one back to the original sheet, it went right back to not allowing one of them not to work... btw, it was always "Table1" that always seemed to work fine and "Table2" was the one that would always trip up the error.
So it looks like (I think this is why) the reason why its always Table2 that trips the error is because its the shorter of the two. (Table1 goes to row 169 and Table2 only goes to 74)
And finally, here is the code from the screenshot above. This code is from the popup-form for adding a new name to be added to the list. And this is where the break occurs when, if i have both of the named table ranges on the same worksheet, it trips the error when it gets to the ".Rows(k).EntireRow.Insert" line.
Again, just to be clear, both sets of code (for both popup forms and their respective named table ranges), will work fine IF they are not on the same worksheet. If they are on different sheets, then everything runs just fine.
This isn't very efficient (as well as not making much sense to me) and I would like to figure out how to fix this so that they both can be on the same worksheet and not trip up any errors.
Many, many thanks for any help/suggestions anyone can offer.
It has frustrated me till no end because I couldn't make heads nor tails out of why it was occurring for the specific sub where it was happening when I also had the same EXACT code in another sub that does the exact same thing and it never trips up an error and has always worked fine... hmm.
First, here is what the code does... (hopefully this will make it easier to follow along with how and why I have the code the way I do...) (hopefully! )
On a userform for entering a new record, the user has a list of names in a combobox that they can select from:
If the name that they need to enter isn't found in the combobox list, then they can hit another command button (indicated above by the little red arrow) that will open a 'popup' form where the new name can be entered.
This will open up a popup form where the new name can be added:
After entering a new name, and hitting the 'Add The New Name' command button, the code adds the new entry to the specific 'named table range' (which is "Table2"), then closes out the 'popup form' and adds the new name to the combobox field that they were on.
Its upon clicking on the 'Add The New Name" button where the code breaks.
To cut to the chase, the issue turned out to be that when I have 2 named table ranges next to each other AND on the same worksheet, I get the error.
And here is the error and the line where it breaks:
(edit... the code above references "Table24"... this is really Table2. I was just changing many things around trying to find out why/how the error was occurring and I copied the code from one of those changes.)
Here is the worksheet where the two named table ranges are and what they look like: (Where the 'named table range' in column A is "Table1", and the one in column B is "Table2")
I finally figured out the problem after trying all kinds of different ways to execute the code. What finally allowed the code to work, was when I moved one of the named ranges onto a separate worksheet, everything then suddenly worked just fine. (and didn't matter which named table range either, just moving either one of them onto a different sheet allowed both of them to work.)
When I would go to move the other one back to the original sheet, it went right back to not allowing one of them not to work... btw, it was always "Table1" that always seemed to work fine and "Table2" was the one that would always trip up the error.
So it looks like (I think this is why) the reason why its always Table2 that trips the error is because its the shorter of the two. (Table1 goes to row 169 and Table2 only goes to 74)
And finally, here is the code from the screenshot above. This code is from the popup-form for adding a new name to be added to the list. And this is where the break occurs when, if i have both of the named table ranges on the same worksheet, it trips the error when it gets to the ".Rows(k).EntireRow.Insert" line.
Again, just to be clear, both sets of code (for both popup forms and their respective named table ranges), will work fine IF they are not on the same worksheet. If they are on different sheets, then everything runs just fine.
This isn't very efficient (as well as not making much sense to me) and I would like to figure out how to fix this so that they both can be on the same worksheet and not trip up any errors.
Many, many thanks for any help/suggestions anyone can offer.
VBA Code:
Private Sub cmdAddName_Click()
Dim ws As Worksheet
Set ws = Worksheets("LookUpLists")
Dim k As Long
Dim d As Long
With Sheets("LookUpLists")
k = .Range("Table2").Row
d = .Range("Table2").Column
Do While Not IsEmpty(.Cells(k, d))
k = k + 1
Loop
.Rows(k).EntireRow.Insert
.Rows(k).EntireRow.FillDown
.Cells(k, d + 0).value = Me.txtNewNAME.value
With Sheets("LookUpLists").ListObjects("Table2")
.Resize .Range.Resize(.Range.Rows.Count + 1)
End With
ActiveWorkbook.Worksheets("LookupLists").Activate
With ActiveWorkbook.Worksheets("LookupLists").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("Table2").Columns, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("Table2")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
MsgBox "The new NAME you entered: " & UCase(Me.txtNewNAME) & vbNewLine & "has been added to the NAME List database." & vbNewLine & " " & vbNewLine & "Please reopen the 'ENTER NEW INCIDENT' form to re-enter your incident."
frmIncidentEntry.cboIssuedBy.value = Me.txtNewNAME.value
Unload Me
End With
End Sub