I have a combobox that I move around on a worksheet based on which cell is selected. I have a named range that I use for the control's listfillrange. I update this range based on the value previously selected (the cell directly to the left of current selected). Here is the code I use to update the autofilter and copy the results to another sheet to update the range in the calling sub:
I also have a command button on my primary sheet that when clicked, inserts a copy of the current selection's row directly below itself. Here's the code for it:
Now, I can click the button 100 times with no issues but after I play with the dynamic combobox which updates the autofilter and then click the button, I sometimes get the aforementioned error. It's very strange because it only seems to happen once my combo selections get so many levels deep.
Example:
I start on the 1st line by selecting 2 levels deep. I then use the button to copy that line 20 times. I then pick one of those lines and change the levels to be 6 levels deep. When I click the button, I get the error.
I thought it might have something to do with the number of times the filter was updated but if I keep all rows 2 levels deep (select level 1/level 2, click, new level 2, click, new level2, click... and repeat), no error. The autofilter updates every time a selection is made, btw, because focus is shifted to the next cell after the current level is selected.
Here's the line it dies on:
I verified my hunch that the autofilter was causing the issue by bypassing it with the following change. The error never comes up using this.
I've looked all over the web at articles and postings for this error and modified my code accordingly but none seem to help. The code I've provided has been reverted back to the original since no other changes yielded any better results.
Code:
Public Sub UpdateFiltered(sCriteria As String)
Dim wsFiltered As Worksheet
Dim rCat As Range
Set wsFiltered = Worksheets("Filtered")
Set rCat = Range("Categories")
rCat.AutoFilter field:=2, Criteria1:=sCriteria
With wsFiltered
.UsedRange.Clear
rCat.Copy Destination:=.Cells(1, 1)
.Rows(1).Delete
End With
rCat.AutoFilter
End Sub
I also have a command button on my primary sheet that when clicked, inserts a copy of the current selection's row directly below itself. Here's the code for it:
Code:
Private Sub btnDuplicate_Click()
Dim c As Integer
Dim r As Integer
Dim wsSel As Worksheet
c = Selection.Column
r = Selection.Row
If Cells(r, 1) = "" Then Exit Sub
btnDuplicate.Enabled = False
Duplicated = True
cbGLN.Visible = False
cbCategory.Visible = False
Application.ScreenUpdating = False
Set wsSel = Worksheets("Selections")
With ActiveSheet.Cells(r, c)
.EntireRow.Copy
.Offset(1, 0).EntireRow.Insert shift:=xlDown
.Offset(1, 0).Select
.Select
End With
With wsSel.Cells(r, c)
.EntireRow.Copy
.Offset(1, 0).EntireRow.Insert shift:=xlShiftDown
End With
Duplicated = False
Cells(r + 1, 2).Clear
Cells(r + 1, 4).Clear
Cells(r + 1, 5).Clear
Application.CutCopyMode = False
Application.ScreenUpdating = True
btnDuplicate.Enabled = True
End Sub
Now, I can click the button 100 times with no issues but after I play with the dynamic combobox which updates the autofilter and then click the button, I sometimes get the aforementioned error. It's very strange because it only seems to happen once my combo selections get so many levels deep.
Example:
I start on the 1st line by selecting 2 levels deep. I then use the button to copy that line 20 times. I then pick one of those lines and change the levels to be 6 levels deep. When I click the button, I get the error.
I thought it might have something to do with the number of times the filter was updated but if I keep all rows 2 levels deep (select level 1/level 2, click, new level 2, click, new level2, click... and repeat), no error. The autofilter updates every time a selection is made, btw, because focus is shifted to the next cell after the current level is selected.
Here's the line it dies on:
Code:
.Offset(1, 0).EntireRow.Insert shift:=xlDown
I verified my hunch that the autofilter was causing the issue by bypassing it with the following change. The error never comes up using this.
Code:
Sub UpdateFiltered(sCriteria As String)
Dim wsFiltered As Worksheet
Dim rCat As Range
Dim c As Integer
Dim startrow As Integer
Dim endrow As Integer
c = Selection.Column
startrow = c * 10
endrow = startrow + 10
Set wsFiltered = Worksheets("Filtered")
Names.Add Name:="Categories", RefersToR1C1:="=Categories!R" & startrow & "C1:R" & endrow & "C3"
Set rCat = Range("Categories")
With wsFiltered
.UsedRange.Clear
rCat.Copy Destination:=.Cells(1, 1)
.Rows(1).Delete
End With
End Sub
I've looked all over the web at articles and postings for this error and modified my code accordingly but none seem to help. The code I've provided has been reverted back to the original since no other changes yielded any better results.