In my Workbook 'Schedule Checks', the values in sheet 'Daily' range E4:S4 all have the…
- User
In my Workbook 'Schedule Checks', the values in sheet 'Daily' range E4:S4 all have the value 'n'. When I run the code below, it appends the value 'x' to the correct column in my workbook 'TasksTodayPA' sheet 'Today', but row 2 to row 8 are empty. The value 'x' is appended at row 9 and stops at row 16. The code should append the value x starting at row 2 and end at row 16. Can you detect and correct the issue in the code below.
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 cell in the range For Each cell In rng ' Check the value in the corresponding cell in the ‘Daily' sheet Select Case cell.Value Case "y" ' Change the corresponding cell in the ‘Today' sheet to "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Value = "x" Case "d" ' Change the corresponding cell in the ‘Today' sheet to "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol + 1).Value = "x" Case "n" ' Change the corresponding cell in the ‘Today' sheet to "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol + 2).Value = "x" End Select ' Check if the last column is reached or the end row is reached If cell.Column = 19 Or todayStartRow + (cell.Column - 5) > todayEndRow Then Exit For End If Next cell ' Close the ‘Schedule Checks' workbook scheduleWb.Close SaveChanges:=FalseEnd Sub
- Assistant
The issue in the code is that the loop is using the wrong column number for the 'Today' sheet. Instead of using the column number of 3 for 'TasksTodayPA' sheet 'Today', it should be using the column number of 4.
To correct the issue, change the line:
todayCol = 3to:
todayCol = 4This will ensure that the values are appended starting from row 2 in the correct column of the 'TasksTodayPA' sheet 'Today'.
- User
Using your suggestion, the values of x were written tot he wrong column, column F. Before the change the values of x were written to the correct column, column E. The issue is that the x values should be written starting from ROW 2.
- Assistant
Apologies for the confusion. To correct the issue and have the values of "x" written starting from row 2, you need to adjust the calculation of the destination row in the 'Today' sheet.
Replace the lines:
ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol + 1).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol + 2).Value = "x"With:
ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, -1).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, 0).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, 1).Value = "x"This change will ensure that the values of "x" are appended starting from row 2 in the correct columns of the 'TasksTodayPA' sheet 'Today'.
- User
Using your suggestion, the values of x were written tot he wrong column, column D. Before the change the values of x were written to the correct column, column E. The issue is that the x values should be written starting from ROW 2.
- Assistant
Apologies for the confusion. To correct the issue and have the values of "x" written starting from row 2 in the correct column, you need to adjust the calculation of the destination column in the 'Today' sheet.
Replace the lines:
ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, -1).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, 0).Value = "x" ws.Cells(todayStartRow + (cell.Column - 5), todayCol).Offset(0, 1).Value = "x"With:
ws.Cells(todayStartRow, todayCol + (cell.Column - 5)).Value = "x" ws.Cells(todayStartRow, todayCol + (cell.Column - 5) + 1).Value = "x" ws.Cells(todayStartRow, todayCol + (cell.Column - 5) + 2).Value = "x"This change will ensure that the values of "x" are appended starting from row 2 in the correct columns of the 'TasksTodayPA' sheet 'Today'.