gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
In the code below, I do not want the value of any cell to be cleared. I only need a…
- User
In the code below, I do not want the value of any cell to be cleared. I only need a popup message to notify me if the value in the cell does not match the next expected value.
Sub FindNextValue() Dim rng As Range Dim cell As Range Dim currentValue As String Dim nextValue As String Dim pattern As Variant Dim i As Integer Dim errorFound As Boolean
code’ Set the range where values are present Set rng = Range(“B3:B1000”) ’ Define the repeating pattern pattern = Array(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”) ’ Get the value in B3 currentValue = Range(“B3”).Value ’ Find the index of the current value in the pattern For i = LBound(pattern) To UBound(pattern) If pattern(i) = currentValue Then Exit For End If Next i ’ Determine the next expected value If i = UBound(pattern) Then nextValue = pattern(LBound(pattern)) Else nextValue = pattern(i + 1) End If ’ Display message with the next expected value MsgBox "Next expected value: " & nextValue ’ Reset the error flag errorFound = False ’ Find the next non-empty cell in the range For Each cell In rng If cell.Value <> “” Then ’ Check if the value matches the expected value If cell.Value <> nextValue Then ’ Set the error flag and exit the loop errorFound = True Exit For End If ’ Clear contents of the current cell cell.ClearContents ’ Determine the next expected value for the next iteration i = (i + 1) Mod 7 nextValue = pattern(i) ’ Display message with the next expected value MsgBox "Next expected value: " & nextValue Exit For End If Next cell ’ Display error message if value doesn’t match If errorFound Then MsgBox “Error found!” End If - Assistant
To achieve your goal of not clearing the value of any cell, you can modify the code as follows:
vba