Results 1 to 18 of 18

Thread: Split words in each cell in range and compare to see if in another range

  1. #1
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location

    Split words in each cell in range and compare to see if in another range

    Hi Guys ,
    What i'm trying to do is loop through my range eg: K15:K150 which is made up of comma seperated words and check each word in the cell against a name in another range B15:B150, if it's there ill save its address in another Range (Z) so i have a cell of adresses for that cell it was checking.
    so
    [VBA]VB:
    Col B ColK ColZ

    Jon Rick,Ben B4,B5
    Dave
    Rick Jon,Dave B2,B3
    Ben
    Todd Jon B2
    [/VBA]
    I only started splitting strings for single cell, so wouldn't really be of help to you guys here
    but was trying to adapt:
    [VBA]VB:
    Sub SplitValue()
    Dim LookInHere As String
    Dim Counter As Integer
    Dim SplitCatcher As Variant
    LookInHere = Range("K15").Value
    SplitCatcher = Split(LookInHere, ",")
    For Counter = 0 To UBound(SplitCatcher)
    If SplitCatcher(Counter) = Range("B15:B150").Text Then
    'get address and store in Z
    End If
    Next
    End Sub [/VBA]

    any ideas guys
    Last edited by Aussiebear; 05-29-2013 at 04:06 PM. Reason: setting out better

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    [vba]Sub SplitValue()
    Dim LookInHere As String
    Dim Counter As Integer
    Dim SplitCatcher As Variant
    For r = 15 To 19
    LookInHere = Range("K" & r).Value
    SplitCatcher = Split(LookInHere, ",")
    For Counter = 0 To UBound(SplitCatcher)
    For Each cell In Range("B15:B19")
    If SplitCatcher(Counter) = cell Then
    s = s & cell.Address(False, False) & ","
    Exit For
    End If
    Next
    Range("Z" & r) = Left(s, Len(s) - 1)
    Next
    s = ""
    Next
    End Sub[/vba]

  3. #3
    VBAX Contributor
    Joined
    Oct 2012
    Location
    Brisbane, Queensland, Australia
    Posts
    163
    Location
    Use [VBA]Dim i As Long, j As Long, n As Long
    Dim arrnames As Variant
    Dim strcells As String
    With ActiveSheet.Range("A1")
    For i = 14 To 149
    arrnames = Split(.Offset(i, 10), ", ")
    strcells = ""
    For j = LBound(arrnames) To UBound(arrnames)
    For n = 14 To 149
    If arrnames(j) = .Offset(n, 1) Then
    strcells = strcells & ", B" & n + 1
    End If
    Next n
    Next j
    If strcells <> "" Then
    strcells = Mid(strcells, 3)
    .Offset(i, 25) = strcells
    End If
    Next i
    End With
    [/VBA]

  4. #4
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location

    Smile


    Thanks for your time guys, much appreciated!

  5. #5

  6. #6
    snb
    Guest
    or

    [vba]
    Sub M_snb()
    for each it in columns(2).specialcells(2)
    columns(11).replace it,it.address(0,0)
    next
    end sub
    [/vba]

  7. #7
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    snb code is very good, but to save in column z as requested

    [vba]Sub M_snb()
    Columns(11).Copy Columns(26)
    For Each it In Columns(2).SpecialCells(2)
    Columns(26).Replace it, it.Address(0, 0)
    Next
    End Sub[/vba]

  8. #8
    Hi,

    Quote Originally Posted by snb
    or

    [vba]
    Sub M_snb()
    for each it in columns(2).specialcells(2)
    columns(11).replace it,it.address(0,0)
    next
    end sub
    [/vba]

    If in column B is Cristal and Cris or Ben and Beneton code does not work correct. Can you correct the code?

  9. #9
    snb
    Guest
    You must have overlooked post #1

  10. #10
    Quote Originally Posted by snb
    You must have overlooked post #1
    It seems you do not understand.
    In this range B15:B150 can be name as I mention before:
    Cristal,
    Cris
    Ben
    Beneton

    ect..

    and in column K will be wrong answer. Try and see...

  11. #11
    snb
    Guest
    No such name in post #1

  12. #12
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,710
    Location
    Quote Originally Posted by Tom Jones
    Hi,

    If in column B is Cristal and Cris or Ben and Beneton code does not work correct. Can you correct the code?
    Your question is wrong. Can you correct the question?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    In this range B15:B150 can be name as I mention before:
    Cristal
    Jon
    Cris
    Ben
    Dave
    Beneton
    Rick



    If the name is that the above result snb's code is wrong.
    Last edited by Tom Jones; 05-29-2013 at 02:44 PM.

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,121
    Location
    Where is the code wrong? Can you name the line it defaults on?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,710
    Location
    Quote Originally Posted by Tom Jones
    In this range B15:B150 can be name as I mention before:
    Cristal
    Jon
    Cris
    Ben
    Dave
    Beneton
    Rick



    If the name is that the above result snb's code is wrong.
    Even with those names in column B, snb's code does exactly what the OP asked for.

    If you want code that does something different, you need to say what you want the code to do.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  16. #16
    Hi,

    @SamT
    In the attached file I marked in red incorrect result SNB's code. I don't know if you tested and say is good with given example.

    @ Aussiebear
    No code is incorrect (and obviously not an error code) but the result code.

    Hope you will understand now.
    Attached Files Attached Files
    Last edited by Tom Jones; 05-30-2013 at 02:48 AM.

  17. #17
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,710
    Location
    You are a good man, Tom Jones; Stubborn. I like that.

    BTW, did I mention that you are correct and I was wrong.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  18. #18
    Quote Originally Posted by SamT
    You are a good man, Tom Jones; Stubborn. I like that.

    BTW, did I mention that you are correct and I was wrong.
    Hi,

    Perhaps my expression was not explicit enough but...
    I am glad that in the end you understood.

Posting Permissions

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