+ Reply to Thread
Results 1 to 4 of 4

How to pick out multiple values from a range that are in a separate table

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2011
    Location
    Macau
    MS-Off Ver
    Excel Mac 2011
    Posts
    5

    How to pick out multiple values from a range that are in a separate table

    Hi Forum,

    I have been trying to figure out this problem for a while, I am certain it is possible but I can’t figure it out. I think explaining the problem may be trickier than solving it.

    The document is a schedule, and annual leave tracker. In the example I have attached there is 8 weeks worth of the tracker and a table that contains the dates of all the holidays in the year. There are 2 types of holidays represented by yellow and green. The date range I included includes 3 holidays of both types.

    What I need to accomplish is a way to determine in each 4 week block (in the actual document there is a whole years worth) if one of the holidays in the table comes up and then be able to tell if the people worked the holiday, or not (represented by PHO) , and what type of holiday it was.

    Please let me know if there is a way I can explain this better.

    Also if you can suggest a better title for this thread please do, I am not sure exactly what function I am looking for.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: How to pick out multiple values from a range that are in a separate table

    Try the attached. I have applied Conditional Formatting to highlight the dates within the work schedules that correspond to the 2 types of holidays.

    As for the rest of your request you'll have to be more specific as to how you want it done. Amend your sample file to show how you want the final result to look.
    Attached Files Attached Files

  3. #3
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: How to pick out multiple values from a range that are in a separate table

    Hi PardySound

    Not sure If I have understood your problem totally, but the attached workbook contains 2 macros.

    The first will look at each of the dates, check whether they are holiday dates and colour them Yellow or Green, with a cell comment of Chinese or International.

    Sub MarkDates()
        Dim type1 As Long, type2 As Long
        Dim rn As Long, cn As Long, i As Long, j As Long
    
        rn = 12   ' starting row number
        For j = 1 To 2  ' this would need changing to 13 for full year
            For i = 3 To 33
                type1 = WorksheetFunction.CountIf(Range("M37:M46"), Cells(rn, i).Value)
                type2 = WorksheetFunction.CountIf(Range("M47:M50"), Cells(rn, i).Value)
                If type1 > 0 Or type2 > 0 Then
                    Cells(rn, i).Select
                    Selection.ClearComments
                    With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent4
                        .TintAndShade = 0.399975585192419
                        .PatternTintAndShade = 0
                    End With
                    Selection.AddComment
                    Selection.Comment.Visible = False
                    If type1 Then
                        Selection.Comment.Text Text:="Chinese" & Chr(10) & ""
                        With Selection.Interior
                            .Pattern = xlSolid
                            .PatternColorIndex = xlAutomatic
                            .Color = 65535
                            .TintAndShade = 0
                            .PatternTintAndShade = 0
                        End With
                    Else
                        Selection.Comment.Text Text:="InterNational" & Chr(10) & ""
                        With Selection.Interior
                            .Pattern = xlSolid
                            .PatternColorIndex = xlAutomatic
                            .Color = 65280
                            .TintAndShade = 0
                            .PatternTintAndShade = 0
                        End With
                    End If
    
                End If
            Next i
            rn = rn + 13    ' number of rows between Dates
        Next j
    
    End Sub

    The second Macro then finds each of the commented cells, and tests the range of entries below that date to look for the search item - "PHO".
    When found, it lists the persons name, the date and the type onto a list on Sheet2.


    
    Sub ListNames()
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim myRange As Range, rgComments As Range
        Dim found As String, mr As Long, mc As Long, lr As Long
        Dim SearchItem As String
        Set ws1 = Sheets("Sheet1")
        Set ws2 = Sheets("Sheet2")
        Set rgComments = ws1.Cells.SpecialCells(xlCellTypeComments)
        
        SearchItem = "PHO"
        For Each cell In rgComments
            mr = cell.Row
            mc = cell.Column
            lr = ws1.Cells(mr, mc).End(xlDown).Row
            Set myRange = Range(Cells(mr, mc), Cells(mr + lr - mr, mc))
            If WorksheetFunction.CountIf(myRange, SearchItem) Then
                found = WorksheetFunction.Match(SearchItem, myRange, 0)
                lr = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
                ws2.Cells(lr, 1) = ws1.Cells(mr + found - 1, 2).Value
                ws2.Cells(lr, 2) = ws1.Cells(mr, mc).Value
                ws2.Cells(lr, 3).Value = "'" & ws1.Cells(mr, mc).Comment.Text
            End If
        Next
    End Sub

    Hopefully, even if this is not the format you want, there will be sufficient for you to amend to suit your requirements.
    Attached Files Attached Files
    Last edited by NBVC; 05-17-2011 at 12:34 PM.
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  4. #4
    Registered User
    Join Date
    04-30-2011
    Location
    Macau
    MS-Off Ver
    Excel Mac 2011
    Posts
    5

    Re: How to pick out multiple values from a range that are in a separate table

    Hi and thanks for the response and the input.. I am sorry for not being specific enough with my first example. The file I have attached this time has the results I am looking for, but I was only able to achieve it by adding rows between the employee's names which wont work in practice because I need to protect and hide them.. But then I cant paste data into the other cells :-(
    But at least this works for the example

    Do you think this is possible to achieve without using VBA ? I am out of my depth when it comes to code and I need to be able to apply this to a big and complicated sheet and I fear I won’t be able to... I am hoping for a few scalable formulas I can apply on a large scale.

    What I am thinking is something I can put to the left of the 4 week block that looks at all the dates in that 4 weeks, compares them with the table of holidays, and if somebody has worked on the holiday count weather it was a DIL holiday or a PTO holiday.

    Something to keep in mind, please note how the dates work.. The first date is set and then the rest of the year is relative to it, the sheet needs to work this way as different user will start using it at different times, and in subsequent years the holidays will be on different dates.

    I am still finding this difficult to explain accurately, please let me know if there is more details I can fill in. And thanks again.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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