I have an excel workbook and I am running some code to remove extra spaces from one cell in several sheets. When I enter something in cell "E4", it removes every instance of " " and replaces it with "". This works as anticipated.
I am trying to get this to work for more than one sheet, by using named ranges. In the sheets, I created a named range "ConfigurationInput" with a scope of sheet.
The following code is in one of the sheets, and it functions as expected:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E4")) Is Nothing Then
Call RemoveSpaces(Range("E4"))
End If
End Sub
This calls the following Sub, located in a module:
Sub RemoveSpaces(RngRemove)
Dim rng As Range
Set rng = RngRemove
rng = WorksheetFunction.Substitute(rng, " ", "")
End Sub
I am trying to replace "E4" with the named range "ConfigurationInput", and I am receiving the following error which crashes Excel:
Does anybody have a suggestion? I tried simplifying to a single cell and using .Address but that didn't work either:
If Target.Address = Range("ConfigurationInput").Address Then
I finally figured it out:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("ConfigurationInput")) Is Nothing Then
Call FixConfig(Range("ConfigurationInput"))
End If
Application.EnableEvents = True
End Sub
I had to wrap the code with Application.EnableEvents = False
and Application.EnableEvents = True