VBA Search row for instances of pattern in two adjacent cells

franklin_m

New Member
Joined
Jun 16, 2013
Messages
47
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
This one is kicking my bu**.

The data source comes from someone using a worksheet like a piece of paper, and I'm trying to turn it into some useable data. It's a schedule for a year set up like a Gantt chart. Row A, columns 5 through 370 are the day of the year. Column A, rows 1-200 are the individual's name. Columns B, C, & D contain admin data for each name. Columns 5 (E) through 370 contain either an empty cell ( "" ) or an "X" depending on whether that person is scheduled to work that day. There's no regularity in the duration of the work shift (in days).

What I want to do is for each row (name) on the schedule worksheet, loop through the cells looking for the pattern of empty cell followed by "X", then read that day of the year from row "A" and the column where the "X" appears - and write that date to another worksheet (say "Table_Data")where the name of individual is in column "A", the date read from the loop in column "B." Then back on the original data sheet, continue from that date looking now for the pattern of "X" followed by an empty cell ( "" ), where the "X" marks the last day of the shift. Then like above, read that column date and write on that other worksheet in column "C". It would continue looping through the rest of the days in the year and create a new line on the "Table_Data" worksheet for each start end date throughout the year. Once it's searched all 365 days, then move to the next line on the schedule sheet and do the same.

Thanks so much ...
Frank
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
@franklin_m Try something like below.'
Assumes the code to be pasted in the code module of the original data sheet and that the 'Table_Data' sheet exists and has headers in row 1.

VBA Code:
Sub Get_Schedule()
Dim r, c, n  As Integer
Dim lastrow As Integer
Dim sdate As Long

n = 2  'start row for entry in Table_Data  assumes headers in row 1
lastrow = Range("A" & Rows.Count).End(xlUp).Row  'last row of original data
For r = 2 To lastrow  'assumesheader row A
    For c = 5 To 20
        If LCase(Cells(r, c)) = "x" And LCase(Cells(r, c - 1)) <> "x" Then sdate = Cells(1, c)
        If LCase(Cells(r, c)) = "x" And Cells(r, c + 1) = "" Then
        fdate = Cells(1, c)
        With Sheets("Table_Data")
            .Cells(n, 1) = Cells(r, 1)
            .Cells(n, 2) = sdate
            .Cells(n, 3) = fdate
        End With
    n = n + 1
    End If
    Next c
Next r
End Sub

Hope that helps?
 
Upvote 0
Run time may benefit by disabling screen updating.

VBA Code:
Sub Get_Schedule()
Dim r, c, n  As Integer
Dim lastrow As Integer
Dim sdate As Long

Application.ScreenUpdating = False
n = 2  'start row for entry in Table_Data  assumes headers in row 1
lastrow = Range("A" & Rows.Count).End(xlUp).Row  'last row of original data
For r = 2 To lastrow  'assumesheader row A
    For c = 5 To 20
        If LCase(Cells(r, c)) = "x" And LCase(Cells(r, c - 1)) <> "x" Then sdate = Cells(1, c)
        If LCase(Cells(r, c)) = "x" And Cells(r, c + 1) = "" Then
        fdate = Cells(1, c)
        With Sheets("Table_Data")
            .Cells(n, 1) = Cells(r, 1)
            .Cells(n, 2) = sdate
            .Cells(n, 3) = fdate
        End With
    n = n + 1
    End If
    Next c
Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top