Home » excel » excel – How to write code to loop lookups over sheets

excel – How to write code to loop lookups over sheets

Posted by: admin May 14, 2020 Leave a comment

Questions:

I know what I want to do and the logic seems fairly easy. However, when it comes down to actually writing the code for it, it seems quite complicated for someone my level.

I have an Excel workbook with 2 Sheets.

Sheet 1 contains, 200 First and Last Names in Columns A & B and 13 Dates in Row 1. The rest of the spread Sheet is empty.
Sheet 2 contains the results of a doodle for eleven 1 hour blocks (09:00 – 20:00) on those dates for the same 200 people from Sheet 1. So Sheet 2 contains the same names in Columns A & B, Dates in Row 1 where the dates are repeated 11 times in consecutive cells to accommodate the eleven 1 hour blocks which are in Row 2, followed by the rest of the spreadsheet filled with one of the three values – “Yes”, “No” and “Maybe”, for each of the 1 hour blocks that were available per date for each name.

I want to fill each cell in Sheet 1 the times at which each person in Sheet 1 marked themselves as “Yes” in Sheet 2. So, If Person 1 marked themselves “Yes” on Hours 09:00 – 10:00, 11:00-12:00 and 13:00-14:00 on Sheet 2 for the 31/03/2020, I would like to concatenate these hour blocks exactly as follows
09:00-10:00
11:00-12:00
13:00-14:00
and then input this into the corresponding cell for that name and date on Sheet 1.

I must confess that I only just started looking at coding for VBA Excel about 5 hours ago. So I actually have no code I can present. I have been trying to use loops, arrays, tables, lookups and so on. And I have been running into issues with the fact that my sheets have different data types.

How to&Answers:

Principle would be to scan down and across sheet1 using some basic mathematics to
determine the range of cells on sheet 2 to be examined for “Yes”

Sub mysub()

    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Dim r As Integer, c As Integer, c2 As Integer, s As String, d As Integer

    Set wb = ThisWorkbook
    Set ws1 = wb.Sheets("Sheet1")
    Set ws2 = wb.Sheets("Sheet2")

    ' scan sheet 1
    For r = 3 To 10 ' or whatever the last row is
        For d = 0 To 10 ' 11 dates
            s = ""
            ' work out first columns on sheet2 for that d
            c2 = 3 + d * 11
            For c = c2 To (c2 + 10)
                If ws2.Cells(r, c) = "Yes" Then
                    s = s & ws2.Cells(2, c) & " " ' row 2 for time
                End If
            Next
            ws1.Cells(r, d + 3) = s
        Next
    Next
    MsgBox "Done"
End Sub

Answer:

Credit goes to @CDP1802 for doing the whole thing. I just edited a few small things! Thanks alot!

    Sub mysub()

        Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
        Dim r As Integer, c As Integer, c2 As Integer, s As String, d As Integer

        Set wb = ThisWorkbook
        Set ws1 = wb.Sheets("Sheet1")
        Set ws2 = wb.Sheets("Sheet2")

        ' scan sheet 2
        For r = 3 To 202 ' or whatever the last row is
            For d = 0 To 12 ' 13 dates
                s = ""
                ' work out first columns on sheet2 for that d
                c2 = 3 + d * 11
                For c = c2 To (c2 + 10)
                    If ws2.Cells(r, c) = "Yes" Then
'                   Pick up time from row 2 in Sheet 2
                        s = s & ws2.Cells(2, c) & vbNewLine 
                    End If
                    If ws2.Cells(r, c) = "Under reserve" Then
'                   Pick up time from row 2 in Sheet 2 and place it in parenthesis
                        s = s & "(" & ws2.Cells(2, c) & ")" & vbNewLine
                        End If
                Next
' Insert the resulting string of times in Sheet 1. Sheet 1 has one less rows, hence r-1
                ws1.Cells(r - 1, d + 3) = s
            Next
        Next
        MsgBox "Done"
    End Sub