Hello
Is it possible to replace multiple comma separated values in excel cell by values in reference column?
Please refer the attachment.
Example
If I type A2, A5 in cell C2 then it should replace the value as Apple, Dog.
Hello
Is it possible to replace multiple comma separated values in excel cell by values in reference column?
Please refer the attachment.
Example
If I type A2, A5 in cell C2 then it should replace the value as Apple, Dog.
To Sheet1 code module
![]()
Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Range, x, i As Long If Intersect(Target, Columns("c")) Is Nothing Then Exit Sub Application.EnableEvents = False For Each r In Intersect(Target, Columns("c")) If r.Row > 1 Then If r.Value <> "" Then x = Split(r.Value, ",") For i = 0 To UBound(x) x(i) = Trim$(x(i)) If IsAddress(x(i)) Then x(i) = Range(x(i)).Value Next r.Value = Join(x, ", ") End If End If Next Application.EnableEvents = True End Sub Function IsAddress(ByVal txt As String) As Boolean With CreateObject("VBScript.RegExp") .Pattern = "^[a-zA-Z]+\d+$" IsAddress = .test(txt) End With End Function
I guess, code proposed by Jindon can be used like this...
![]()
Private Sub Worksheet_Change(ByVal Target As Range) Dim x, i As Long If Intersect(Target, Columns("c")) Is Nothing Then Exit Sub Application.EnableEvents = False If Target.Row > 1 Then If Target <> "" Then x = Split(Target, ",") For i = 0 To UBound(x) x(i) = Trim$(x(i)) If IsAddress(x(i)) Then x(i) = Range(x(i)).Value Next Target = Join(x, ", ") End If End If Application.EnableEvents = True End Sub Function IsAddress(ByVal txt As String) As Boolean With CreateObject("VBScript.RegExp") 'To make sure the address belongs only to the column A .Pattern = "^[aA]\d+$" ' OR "^[a-zA-Z]{1,3}\d+$" to make sure max three column letters are accepted only as valid address IsAddress = .test(txt) End With End Function
Regards
sktneer
Treat people the way you want to be treated. Talk to people the way you want to be talked to.
Respect is earned NOT given.
Not good enough![]()
' OR "^[a-zA-Z]{1,3}\d+$" to make sure max three column letters are accepted only as valid address
e.g
xxx100000000000000000000000
If you want to restrict to A2:Ax, this should be good enough
![]()
Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Range, x, i As Long If Intersect(Target, Columns("c")) Is Nothing Then Exit Sub Application.EnableEvents = False For Each r In Intersect(Target, Columns("c")) If r.Row > 1 Then If r.Value <> "" Then x = Split(r.Value, ",") For i = 0 To UBound(x) x(i) = Trim$(x(i)) If (x(i) Like "[Aa][1-9]*") * (Not Mid$(x(i), 2) Like "*[!0-9]*") * _ (Val(Mid$(x(i), 2)) <= Rows.Count) Then x(i) = Range("a" & Val(Mid$(x(i), 2))).Value End If Next r.Value = Join(x, ", ") End If End If Next Application.EnableEvents = True End Sub
Last edited by jindon; 03-05-2018 at 06:02 AM.
this activates userform if any cell in column C is selected
Kind regards![]()
Private Sub CommandButton1_Click() Unload Me End Sub Private Sub ListBox1_Click() If ActiveCell = "" Then ActiveCell = ListBox1.Value Else ActiveCell = ActiveCell & ", " & ListBox1.Value End If End Sub Private Sub UserForm_Initialize() With Sheets("Sheet1") lr = .Range("A" & Rows.Count).End(xlUp).Row ReDim arr(1 To lr - 1, 1 To 2) For i = 2 To lr arr(i - 1, 1) = .Cells(i, 1).Value arr(i - 1, 2) = .Cells(i, 1).Address(0, 0) Next ListBox1.List = arr End With End Sub
Leo
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks