Sub ThisWeek(rngChange As Range) Dim rngValues As Range Dim rngOffset As Range Set…
- User
Sub ThisWeek(rngChange As Range)
codeDim rngValues As Range Dim rngOffset As Range Set rngValues = Me.Range("AB3:AB18") Set rngOffset = rngValues.Offset(, -1) ' Offset by 1 column to the left (AB -> AA) Dim cell As Range For Each cell In rngChange If cell.Value = "b" Then Dim rowNum As Long rowNum = cell.Row - rngChange.Rows(1).Row + 1 ' Calculate the relative row number Dim lookupValue As Variant lookupValue = rngValues.Cells(rowNum, 1).Value ' Get the corresponding value from AB3:AB18 Dim resultRng As Range Set resultRng = Union(Me.Range("D3:D18"), Me.Range("L3:L18"), Me.Range("T3:T18")) Dim resultCell As Range Set resultCell = resultRng.Find(lookupValue, LookIn:=xlValues) If Not resultCell Is Nothing Then Dim offsetVal As Long offsetVal = rngChange.Columns(1).Column - rngOffset.Columns(1).Column - 1 ' Calculate the offset Dim resultOffset As Range Set resultOffset = resultCell.Offset(, offsetVal) If resultOffset.Value = "z" Then resultOffset.Value = "y" Else MsgBox "Current Bay is not Free on this day. Only Parking Spaces marked 'z' are free on the day." cell.Value = "" ' Reset the rejected "b" value to empty Exit Sub End If End If End If Next cellEnd Sub
In this VBA code, I want to change this part of the code, "For Each cell In rngChange If cell.Value = "b" Then" . . . What I want the code to do is to run after each "b" has been entered and ignore other values of 'b' that may have been entered previously.
- Assistant
To achieve the desired behavior of running the code only after each newly entered "b" value and ignoring any previously entered "b" values, you can modify the code as follows:
vba