+ Reply to Thread
Results 1 to 6 of 6

Replacing values from a Cell by Reference values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-05-2018
    Location
    japan
    MS-Off Ver
    2016
    Posts
    1

    Replacing values from a Cell by Reference values

    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.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Replacing values from a Cell by Reference values

    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

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Replacing values from a Cell by Reference values

    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.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Replacing values from a Cell by Reference values

    ' OR "^[a-zA-Z]{1,3}\d+$" to make sure max three column letters are accepted only as valid address
    Not good enough

    e.g
    xxx100000000000000000000000

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Replacing values from a Cell by Reference values

    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.

  6. #6
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Replacing values from a Cell by Reference values

    this activates userform if any cell in column C is selected

    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
    Kind regards
    Leo

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. replacing cell values with their corresponding ASCII code
    By Lmadahali in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-27-2014, 09:58 AM
  2. replacing cell values with their corresponding ASCII code
    By Lmadahali in forum Excel General
    Replies: 1
    Last Post: 10-16-2014, 06:23 PM
  3. Replacing cell values
    By wjk221 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-06-2014, 04:58 PM
  4. Replies: 1
    Last Post: 10-01-2013, 02:41 AM
  5. replacing cell values
    By abhimaan in forum Excel General
    Replies: 1
    Last Post: 05-26-2012, 06:58 PM
  6. Replacing matched cell values
    By los7 in forum Excel General
    Replies: 4
    Last Post: 06-09-2009, 01:54 AM
  7. [SOLVED] Replacing Linked Cell Values w/ Current Values
    By TomCat in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-10-2006, 07:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1