The code below is not accurate. In sheet ‘Daily’ the range E4:S4 contains 15 cells. In…
- User
The code below is not accurate. In sheet ‘Daily’ the range E4:S4 contains 15 cells. In sheet ‘Today’ the ranges C2:C16 and D2:D16 and E2:E16 each contain 15 cells. If the value ‘y’ is found in the first 8 cells of the range E4:S4 of sheet Daily, then the code should only write the value x to the first 8 cells in column C of the sheet Today. If the value ‘d’ is only found in the 10th cell of the range E4:S4 of sheet Daily, then the code should only write the value x to the 10th cell in column D of the sheet Today. If the value ‘n’ is only found in the last 4 cells of the range E4:S4 of sheet Daily, then the code should only write the value x to the last 4 cells in of column E (E13:E16) in the sheet Today. Currently the code is writing the value x to all the cells in column C, D E. Sub CheckScheduleChecks() Dim wb As Workbook Dim ws As Worksheet Dim scheduleWb As Workbook Dim scheduleWs As Worksheet Dim lastColumn As Long Dim rng As Range Dim cell As Range Dim todayCol As Long Dim todayStartRow As Long Dim todayEndRow As Long
’ Set the workbook and worksheet objects Set wb = ThisWorkbook Set ws = wb.Sheets(“Today”) todayCol = 3 ’ Column C todayStartRow = 2 ’ Row 2 todayEndRow = 16 ’ Row 16
’ Check if the ‘Schedule Checks’ workbook is open On Error Resume Next Set scheduleWb = Workbooks(“Schedule Checks.xlsm”) On Error GoTo 0
’ If the ‘Schedule Checks’ workbook is not open, open it If scheduleWb Is Nothing Then Set scheduleWb = Workbooks.Open(wb.Path & “\Schedule Checks.xlsm”) End If
’ Set the worksheet object Set scheduleWs = scheduleWb.Sheets(“Daily”)
’ Find the last column in the ‘Daily’ sheet lastColumn = scheduleWs.Cells(4, scheduleWs.Columns.Count).End(xlToLeft).Column
’ Set the range to check values in the ‘Daily’ sheet Set rng = scheduleWs.Range(scheduleWs.Cells(4, 5), scheduleWs.Cells(4, lastColumn))
’ Loop through each row from 2 to 16 For RowIndex = todayStartRow To todayEndRow ’ Loop through each cell in the range For columnIndex = 5 To lastColumn ’ Check the value in the corresponding cell in the ‘Daily’ sheet Select Case scheduleWs.Cells(4, columnIndex).Value Case “y” Set cell = scheduleWs.Range(scheduleWs.Cells(4, columnIndex), scheduleWs.Cells(todayEndRow, columnIndex)).Find(“y”) If Not cell Is Nothing Then ’ Change the corresponding cell in the ‘Today’ sheet to “x” ws.Cells(RowIndex, todayCol).Value = “x” End If Case “d” Set cell = scheduleWs.Range(scheduleWs.Cells(4, columnIndex), scheduleWs.Cells(todayEndRow, columnIndex)).Find(“d”) If Not cell Is Nothing Then ’ Change the corresponding cell in the ‘Today’ sheet to “x” ws.Cells(RowIndex, todayCol + 1).Value = “x” End If Case “n” Set cell = scheduleWs.Range(scheduleWs.Cells(4, columnIndex), scheduleWs.Cells(todayEndRow, columnIndex)).Find(“n”) If Not cell Is Nothing Then ’ Change the corresponding cell in the ‘Today’ sheet to “x” ws.Cells(RowIndex, todayCol + 2).Value = “x” End If End Select Next columnIndex Next RowIndex
’ Close the ‘Schedule Checks’ workbook scheduleWb.Close SaveChanges:=False
- Assistant
The code provided does not accurately implement the required logic. Here is the amended version: