The following VBA is supposed to check the values in sheet 'Daily' E4:S4. Depending on…
- User
The following VBA is supposed to check the values in sheet 'Daily' E4:S4. Depending on the values in the range, it should write the value 'x' into sheet 'Today' in either column 'C' or 'D' or 'E', starting from ROW 2 all the way to down to ROW 16. The VBA appears to be writing the values 'x' to the correct column, but it is not writing anything into Row 2 to Row 8. It is only writing the values 'x' into ROW 9 to ROW 16.
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:=False
End Sub
- Assistant
The issue seems to be with the condition in the loop that determines when to exit the loop. Currently, the loop uses the condition
todayStartRow + (cell.Column - 5) > todayEndRowto determine when to exit the loop. However, this condition is incorrect as it is based on the column value of the cell, rather than the row value.To fix the issue, you can modify the condition in the loop to
todayStartRow + (cell.Row - 3) > todayEndRowwhich will check if the row value of the cell is greater than thetodayEndRow. The modified code snippet is shown below:vba - User
The x values are to be written from ROW 2 all the way down to ROW 16. Will this ammendment do that?
- Assistant
Apologies for the confusion. To write the 'x' values from Row 2 to Row 16, you need to adjust the condition in the loop. Instead of checking if
todayStartRow + (cell.Row - 3) > todayEndRow, you should check iftodayStartRow + (cell.Row - 3) <= todayEndRow.Here's the modified code snippet with the corrected condition:
vba - User
When I run the modified code, it is only writing to one cell in ROW 3 only and not to all the 16 cells from ROW 2 to ROW 16
- Assistant
I apologize for the confusion. It seems that there was a misunderstanding in the way the loop was structured.
To write the 'x' values from Row 2 to Row 16 for each column, you need to adjust the loop to iterate through the rows from 2 to 16 and then check the corresponding cell in the 'Daily' sheet for each row and column.
Here's the modified code snippet with the corrected loop structure:
vba - User
- Assistant
Apologies for the confusion. Thank you for providing clear instructions on how the values should be updated based on the specific cells in the 'Daily' sheet.
To update the values in the 'Today' sheet based on the specific cells in the 'Daily' sheet, you need to modify the code as follows:
vba - User
The VBA code is still not accurately writing all required values of 'x' into sheet 'Today' that correspond to the values in range E4:S4 of sheet 'Daily'
- Assistant
I apologize for the confusion. To accurately write the corresponding 'x' values in the 'Today' sheet based on the values in the range E4:S4 of the 'Daily' sheet, you can use the
Findmethod to search for the specific values in the range and update the corresponding cells in the 'Today' sheet.Here's the modified code:
vba