I've got a Worksheet_Change event that can get triggered in multiple occasions depending on what column the end user is working with.
My problem is that when the Line3 part of my code gets triggered and a cell in the ContractTable[City] column autofills after a value being manually entered in in the cell right at its left (ContractTable[Address]), then the Line2 part of my code stops working, meaning that the ContractTable[Address] column won't get filled when something is entered in a cell within the ContractTable[Department] column.
[TABLE="width: 500"]
<tbody>[TR]
[TD]Department[/TD]
[TD]Address[/TD]
[TD]City[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Any ideas why?
Also, when Line2 does work and Address gets filled by the event, City doesn't: isn't a value within Address being changed either manually or by worksheet_change supposed to trigger the event again (Line3) and allow City to be filled too?
Hope I've made myself clear enough.
I've left out the part of my code that doesn't give me problems.
My problem is that when the Line3 part of my code gets triggered and a cell in the ContractTable[City] column autofills after a value being manually entered in in the cell right at its left (ContractTable[Address]), then the Line2 part of my code stops working, meaning that the ContractTable[Address] column won't get filled when something is entered in a cell within the ContractTable[Department] column.
[TABLE="width: 500"]
<tbody>[TR]
[TD]Department[/TD]
[TD]Address[/TD]
[TD]City[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Any ideas why?
Also, when Line2 does work and Address gets filled by the event, City doesn't: isn't a value within Address being changed either manually or by worksheet_change supposed to trigger the event again (Line3) and allow City to be filled too?
Hope I've made myself clear enough.
I've left out the part of my code that doesn't give me problems.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range, myMatch As Variant
If Not Intersect(Target, Range("ContractTable[Position]")) Is Nothing Then GoTo Line1 Else GoTo Line2
Line1: (...)
Line2: If Not Intersect(Target, Range("ContractTable[Department]")) Is Nothing Then
Set rng = Intersect(Target, Range("ContractTable[Department]"))
[INDENT]Else GoTo Line3[/INDENT]
For Each c In rng
If Len(c.Value) = 0 Then
c.Offset(0, 1).Value = ""
Else
myMatch = Application.Match(c.Value, Worksheets("#DeparmentAddresses").Range("AddressTable[Department]"), 0)
If Not IsError(myMatch) Then
c.Offset(0, 1).Value = Application.Index(Worksheets("#DepartmentAddresses").Range("AddressTable[Address]"), myMatch)
End If
End If
Next
[TABLE="width: 245"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Line3: If Not Intersect(Target, Range("ContractTable[Address]")) Is Nothing Then[/TD]
[/TR]
[TR]
[TD] Set rng = Intersect(Target, Range("ContractTable[Address]"))[/TD]
[/TR]
[TR]
[TD] For Each c In rng[/TD]
[/TR]
[TR]
[TD] If Len(c.Value) = 0 Then[/TD]
[/TR]
[TR]
[TD] c.Offset(0, 1).Value = ""[/TD]
[/TR]
[TR]
[TD] Else[/TD]
[/TR]
[TR]
[TD] myMatch = Application.Match(Split(c.Value, " ")(UBound(Split(c.Value))), Worksheets("#CityAbbreviations").Range("CityTable[Abbreviation]"), 0)[/TD]
[/TR]
[TR]
[TD] If Not IsError(myMatch) Then[/TD]
[/TR]
[TR]
[TD] c.Offset(0, 1).Value = Application.Index(Worksheets("#CityAbbreviations").Range("CityTable[City]"), myMatch)[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] Next[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
</tbody>[/TABLE]