My working code crashes Excel if I add a row. Why...

Blackov

New Member
Joined
Jun 7, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
So I have a dropdown menu in column A and B. With the next code, I change the content based on the selected value. Both codes work and don't give an error.
However... If I add or delete a row, Excel crashes. I found out that the second part of the code causes this. (so the code for column A)
Both codes active: Crash
Only code for column A active: Crash
Only code for column B active: fine.
Why? The only problem I can think of is that when you right-click to add a row, it automatically selects the entire row, which activates the first cell (in column A) which might mess with the code.
How can I fix this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo fout
    'om de inhoud van de cel aan te passen na een keuze door dropdown in de kolom TAG
    selectedNa = Target.Value
    If Target.Column = 2 Then
       selectedNum = Application.VLookup(selectedNa, Worksheets("Dropdown").Range("LookUp"), 2, False)
        If Not IsError(selectedNum) Then
            Target.Value = selectedNum
        End If
    End If
'---------------------------------------------------------
    'om de inhoud van de cel aan te passen na een keuze door dropdown in de kolom Systeem
    selectedNa2 = Target.Value
    If Target.Column = 1 Then
       selectedNum2 = Application.VLookup(selectedNa2, Worksheets("Dropdown").Range("LookUp2"), 2, False)
        If Not IsError(selectedNum2) Then
            Target.Value = selectedNum2
        End If
    End If
'----------------------------------------------------------
Exit sub
fout:
   Target.Value = ""
    Exit Sub
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Your routine is only going to work if 1 cell is selected, so make one of you first lines:
VBA Code:
    If Target.CountLarge > 1 Then Exit Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,177
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