I have a VBA macro that validates user entered data (I didn\'t use data validation/conditional formatting on purpose).
I am using Worksheet_Change
event
There are two a bit another approaches both based on the following template.
Range
type.In the first approach the whole range of interest is assigned to the variable and range's size is saved. Then in a sheet change event handler the following cases must be processed:
Address
property => the pinned range is no longer exist;In the second approach a “marker” range is assigned to the variable (see example below) and the range address is saved in order to determine movements or shifts in any direction. Then in a sheet change event handler the following cases must be processed::
Address
property => the pinned “marker” range is no longer exist;abs(new_row - saved_row) > 0 or abs(new_col-saved_col) > 0
=> the pinned range was moved or shifted.Pros:
UsedRange
property is not usedCons:
WithEvents
-variable of object must be assigned to Nothing
in a workbook close event handler in order to unsubscribe form the event.The following example shows that both approaches could work. Define in a module:
Private m_st As Range
Sub set_m_st()
Set m_st = [$A$10:$F$10]
End Sub
Sub get_m_st()
MsgBox m_st.Address
End Sub
Then run set_m_st
(simply place a cursor in the sub and call Run
action) to pin range $A$10:$F$10
. Insert or delete a row or cell above it (don't confuse with changing cell(s) value). Run get_m_st
to see a changed address of the pinned range. Delete the pinned range to get "Object required" exception in get_m_st
.