Worksheet_Change Producing Error on Insert Row

LBinGA

Board Regular
Joined
Jan 29, 2014
Messages
57
Hi all:

I have the following code on my sheet. If "Yes" is entered into Column A, the entire Row is moved to the Archive sheet, perfectly.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim rngDest As Range
Set rngDest = Worksheets("Prospect Archive").Range("rngDest")
Dim rngTrigger As Range
 
' Limit the trap area to range of cells in which completed dates are entered as defined above
If Not Intersect(Target, Sheets("Prospect List").Range("rngTrigger")) Is Nothing Then
 
' Only trigger if the value entered is TRUE
    
     If Target.Value = "Yes" Then
'Ensure subsequent deletion of 'moved' row does NOT cause the Change Event to run again and get itself in a loop!
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.FormatConditions.Delete
        Selection.Cut
        rngDest.Insert Shift:=xlDown
        Selection.Delete
' Reset EnableEvents
        Application.EnableEvents = True
    End If
End If

It produces Run-Time Error 13: Type Mismatch if the user tries to either manually add or delete a row or use the following code on a button to add a row:

Code:
Sub InsertCompany()Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range


Set rEmpList = Range("C1:C1000")


sNewName = InputBox("Enter Name of New Company")
On Error Resume Next 'if employee needs to go at start of list, Match will return [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=NA"]#NA[/URL] 
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
On Error GoTo 0 'revert to normal error handling (crashing)
rEmpList(lPosition + 1).EntireRow.Insert
rEmpList(lPosition + 1).Value = sNewName
rEmpList(lPosition + 1).Activate


End Sub

Debugging brings me to this line in the Worksheet_Change code: If Target.Value = "Yes" Then

I have change the Dim of RngTrigger to String & Boolean. No difference

The sheet is NOT formatted as a Table because when it is, the Column A code "Yes" code no longer works.

This feels like it should be fairly straightforward. I'm probably missing something uber easy. :eek: Can anyone help?

Thanks,

LBinGA
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Add this to the top of your change event
Code:
   If Target.CountLarge > 1 Then Exit Sub
 
Upvote 0
Works perfectly! Thank you!

Any thoughts on why the code to move to the Archive sheet no longer works if I Format the sheet as a Table?

The "Yes" just sits in column A staring at me. :LOL:
 
Upvote 0
Fraid not, I've very rarely had the need to work with tables.
 
Upvote 0
No problem leaving it as is then, I suppose. Now that I have the add Row working, I don't think it's an issue. :)

Thanks again~
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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