From: Jon M. on
Greetings all,
I have a worksheet where cell B8 and B10 perform a vLookup based on a data
validation in B6:

=IF(B6="","",(VLOOKUP(B6,Position,2,0)))
IF(B6="","",VLOOKUP(B6,AD_Groups,2,0))

I have the following code in place to perform a line break after each comma
in the returned value. It works great the first time, but then anytime I
change or add something to any cell in the range B1:B10 it adds another line
break to B8 and B10. Anything after B10 is fine and doesn't affect the
previous cells. I guess what I'm asking is if there is a way to alter this
code (which I found in a previous posting, just altered my range to B1) so
that it only happens if B6 is selected and changed? And only affects cells
B8 and B10, so any other cells that may have commas are unaffected?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Range
Dim t As Range
Set t = Target
Set a = Range(Range("B1"), Cells(Rows.Count, 1).End(xlUp))
Dim s1 As String
Dim s2 As String
s1 = ","
s2 = s1 & Chr(10)
If Intersect(t, a) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each t In a
t.Value = Replace(t.Value, s1, s2)
Next t
Application.EnableEvents = True
End Sub

As always I appreciate any help I can get.
--
Jon M.
From: Jon M. on
Also if I try to clear out my cells to start over, my formula's disappear and
I can't add them back. Each time I try the cell just clears itself out
again.
--
Jon M.


"Jon M." wrote:

> Greetings all,
> I have a worksheet where cell B8 and B10 perform a vLookup based on a data
> validation in B6:
>
> =IF(B6="","",(VLOOKUP(B6,Position,2,0)))
> IF(B6="","",VLOOKUP(B6,AD_Groups,2,0))
>
> I have the following code in place to perform a line break after each comma
> in the returned value. It works great the first time, but then anytime I
> change or add something to any cell in the range B1:B10 it adds another line
> break to B8 and B10. Anything after B10 is fine and doesn't affect the
> previous cells. I guess what I'm asking is if there is a way to alter this
> code (which I found in a previous posting, just altered my range to B1) so
> that it only happens if B6 is selected and changed? And only affects cells
> B8 and B10, so any other cells that may have commas are unaffected?
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim a As Range
> Dim t As Range
> Set t = Target
> Set a = Range(Range("B1"), Cells(Rows.Count, 1).End(xlUp))
> Dim s1 As String
> Dim s2 As String
> s1 = ","
> s2 = s1 & Chr(10)
> If Intersect(t, a) Is Nothing Then Exit Sub
> Application.EnableEvents = False
> For Each t In a
> t.Value = Replace(t.Value, s1, s2)
> Next t
> Application.EnableEvents = True
> End Sub
>
> As always I appreciate any help I can get.
> --
> Jon M.