Capture error of value

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code will display the value if ONE cell is changed:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Debug.Print Target.Value
   
End Sub

If multiple cells are selected and a value is entered by pressing SHIFT + ENTER, the above code returns an error, as expected.

I want to capture that error like this:

Code:
If IsError(Target.Value) Then .....

but it seems it never to hit the error part, even though if I typed:

Code:
Target.Value

in the Immediate Window, it does show an error message:

Code:
Run-time error '91'

Object variable or With block variable not set

How can I capture such an error?

Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
This is how you can loop through a multi-cell range triggered by this code to get the value of each cell:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
   
    For Each cell In Target
        Debug.Print cell.Value
    Next cell
  
End Sub
So if you want to check for errors, add that code before the For/Next lines, referencing "cell", not "Target".
 
Upvote 0
Solution
This is how you can loop through a multi-cell range triggered by this code to get the value of each cell:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
  
    For Each cell In Target
        Debug.Print cell.Value
    Next cell
 
End Sub
So if you want to check for errors, add that code before the For/Next lines, referencing "cell", not "Target".
Thanks
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,227
Members
448,878
Latest member
Da9l87

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