+ Reply to Thread
Results 1 to 9 of 9

multiple values in an array

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2014
    Location
    Bishops Stortford, England
    MS-Off Ver
    Excel 2007
    Posts
    10

    multiple values in an array

    I'm having a thick moment!!
    Basically, i have two columns of numbers. Column a is unique and column J has multiples of column a (though not all column a values match). I need to match and colour each corresponding value. The attached spreadsheet and macro works but i'm not sure how to increase the array so it looks for each value. Some of the spreadsheets i have to check are in excess of 100K rows. Ouch!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: multiple values in an array

    This will search in column A

    MySearch = Range("a2", Range("A" & Rows.Count).End(xlUp))

               Set Rng = .Find(What:=MySearch(I, 1), _
    Option Explicit
    
    Sub Color_cells_In_Range_Or_Sheet()
           
        
        Dim FirstAddress As String
        Dim MySearch As Variant
        Dim myColor As Variant
        Dim Rng As Range
        Dim I As Long
       Application.ScreenUpdating = True
       
        'Fill in the search Value and color Index
        MySearch = Range("a2", Range("A" & Rows.Count).End(xlUp))
        myColor = Array("6")
    
        'You can also use more values in the Array
        'MySearch = Array("ron", "jelle", "judith")
        'myColor = Array("3", "6", "10")
    
    
        'Fill in the Search range, for the whole sheet use
        'you can use Sheets("Sheet1").Cells
        With Sheets("Sheet1").Range("a2:j100")
    
            'Change the fill color to "no fill" in all cells
            .Interior.ColorIndex = xlColorIndexNone
    
            For I = LBound(MySearch) To UBound(MySearch)
    
                'If you want to find a part of the rng.value then use xlPart
                'if you use LookIn:=xlValues it will also work with a
                'formula cell that evaluates to MySearch(I)
                Set Rng = .Find(What:=MySearch(I, 1), _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlFormulas, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
    
                If Not Rng Is Nothing Then
                    FirstAddress = Rng.Address
                    Do
                        Rng.Interior.ColorIndex = myColor(I)
                        Set Rng = .FindNext(Rng)
                    Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
                End If
            Next I
        End With
    End Sub
    Last edited by AB33; 06-27-2014 at 09:45 AM.

  3. #3
    Registered User
    Join Date
    02-24-2014
    Location
    Bishops Stortford, England
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: multiple values in an array

    AB33,
    I have 2 problems with this. For some reason the 'Rng.Interior.ColorIndex = myColor(I)' throws up an error of 'Runtime 9 'out of range'
    the second part is my own fault. Can it be modified so that that only values of 100 in Col D are coloured, i had forgotten about that when I send in the post.

    many thanks in anticipation.

    PeterJ

  4. #4
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: multiple values in an array

    hi,

    I have solved your problem with a different code..
    a subroutine named test()

    before running the code, just Uncolor All the cells...
    just run the code a enjoy the magic...

    Vikas Gautam

    Sub Test()
    Set ColA = Range("A1:A100")
    Set ColJ = Range("J1:J100")
    For Each Cell1 In ColA
        For Each Cell2 In ColJ
            If Cell1.Value = Cell2.Value Then
              Cell1.Interior.ColorIndex = 6
              Cell2.Interior.ColorIndex = 6
            End If
        Next
    Next
    
    End Sub
    Attached Files Attached Files
    Last edited by Vikas_Gautam; 06-27-2014 at 10:08 AM.

  5. #5
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: multiple values in an array

    Does it help.... Peterjohns..

    Just let me.. know...

    Vikas gautam

    Sub Test()
    Set ColA = Range("A1:A100")
    Set ColJ = Range("J1:J100")
    For Each Cell1 In ColA
        For Each Cell2 In ColJ
            If Cell1.Value = Cell2.Value Then
              Cell1.Interior.ColorIndex = 6
              Cell2.Interior.ColorIndex = 6
            End If
        Next
    Next
    
    End Sub

  6. #6
    Registered User
    Join Date
    02-24-2014
    Location
    Bishops Stortford, England
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: multiple values in an array

    Vikas,
    yes it does work. many thanks.

    however, in Col D this is a hierarchal value, can be 10, 20, 30, 40, 50 or 100.
    Would it be easy to modify your solution so that it only works on those with a value of a 100?

    My fault I forgot that rather relevant point.

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: multiple values in an array

    A simple countif() with Conditional Formatting (CF) method will do that

    =COUNTIF($A$2:$A$113,B2)

    B2 Should be the active cell while applying the CF


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: multiple values in an array

    Okay! I can correct that error. As it is now, your code colours columns A and J.
    "Can it be modified so that that only values of 100 in Col D are coloured?"
    Do you mean if the rows in column J and A match and if the value if column D is 100, colour that cell?
    There is no row where the match is found and column D value is 100. Or am I missing something?

  9. #9
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: multiple values in an array

    Quote Originally Posted by PeterJohns View Post
    Column a is unique and column J has multiples of column a (though not all column a values match). I need to match and colour each corresponding value.
    ...
    Some of the spreadsheets i have to check are in excess of 100K rows. Ouch
    Peter,

    If this quote does indeed accurately describe your dataset, and you do have 100K+ rows, and want to bypass rows with 100 in col D, then you may like to try the following
    Sub color_matches_x()
    
    Dim rsa&, rsj&, u&
    Dim a, j, d
    Dim adra As String, adrj As String
    Dim b As Object, c As Object
    Set b = CreateObject("scripting.dictionary")
    Set c = CreateObject("scripting.dictionary")
    rsa = Cells(Rows.Count, 1).End(3).Row
    rsj = Cells(Rows.Count, "j").End(3).Row
    
    a = Range("A1:A" & rsa)
    j = Range("J1:J" & rsj)
    d = Range("D:D").Value
    
    For u = 2 To rsa
        If (d(u, 1) = 100) * (Len(a(u, 1)) > 0) Then b(a(u, 1)) = True
    Next u
    
    For u = 2 To rsj
        If (d(u, 1) = 100) * (Len(j(u, 1)) > 0) Then
            c(j(u, 1)) = True
            If b(j(u, 1)) Then
                If Len(adrj & ",J" & u) > 254 Then
                    Range(Mid(adrj, 2)).Interior.Color = vbYellow
                    adrj = ",J" & u
                Else
                    adrj = adrj & ",J" & u
                End If
            End If
        End If
    Next u
    
    For u = 2 To rsa
        If (d(u, 1) = 100) * (Len(a(u, 1)) > 0) Then
            If c(a(u, 1)) Then
                If Len(adra & ",A" & u) > 254 Then
                    Range(Mid(adra, 2)).Interior.Color = vbYellow
                    adra = ",A" & u
                Else
                    adra = adra & ",A" & u
                End If
            End If
        End If
    Next u
    
    Range(Mid(adra, 2)).Interior.Color = vbYellow
    Range(Mid(adrj, 2)).Interior.Color = vbYellow
    
    End Sub

+ 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. Is there a shorter way to use an array to look up multiple values?
    By sbrnard in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-06-2014, 04:14 PM
  2. [SOLVED] Return multiple values from an array
    By tig1142 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-27-2013, 08:31 PM
  3. macro to get an array of values from multiple sheets
    By tmiller1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2013, 12:30 PM
  4. Find a value in an array and return multiple values in an adjacent array
    By tonbra in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2012, 08:35 PM
  5. Array Lookup, Return/Sum Multiple Values
    By mre2000 in forum Excel General
    Replies: 3
    Last Post: 01-18-2010, 10:45 PM

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