Can I have a vba code that will do the following:
On cell change, check if the Target Value of the cell exists in sheet 'PENDING QUOTES' Range B2:B42
For each match that does exit,
pop up a message listing the corresponding values in the range A2:A42 one after the other
Assistant
Here is a VBA code that accomplishes the task you have described:
vb
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
If found Then
Dim message As String
message = "Corresponding values:" & vbCrLf
For Each cell In rngPending
If cell.Value = Target.Value Then
message = message & "- " & cell.Offset(0, -1).Value & vbCrLf
End If
Next cell
MsgBox message
End If
End If
End Sub
User
GREAT IT WORKED. How do I make the Message Box use the Target Value as the title. I tried this but it did not work: MsgBox message, vbInformation, Target.Value
Assistant
To set the Target Value as the title of the message box, you can modify the code by creating a variable for the message box style and using the MsgBox function separately to set the title.
Here is the updated VBA code:
vb
User
Can you please change the message to a Yes or No message
"The following Quotes for this Provider" line break
"have not been processed to Finance" line break line break
"Click YES to continue your PREQUEST" line break line break
"Click NO to cancel"
' CHECK IF PROVIDER ENTERED HAS A PENDING QUOTE
Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
code
If found Then
Dim message As String
message = "Quotes Still Pending:" & vbCrLf & vbCrLf
For Each cell In rngPending
If cell.Value = Target.Value Then
message = message & "- " & cell.Offset(0, -1).Value & vbCrLf
End If
Next cell
MsgBox message, vbInformation, Target.Value
End If
End If
Assistant
Here is the updated VBA code with the message formatted as a Yes or No message box:
vb
User
Did I write it correctly: Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
code
Assistant
It looks like the code you provided has a few issues. I have corrected it for you below:
vb
Place this code in the code module for the worksheet where you want this functionality to be active.
Now, whenever a cell in that worksheet is changed, the code will check if the new value matches any value in the range B2:B42 in the "PENDING QUOTES" sheet. If there is a match, a message box will pop up listing the corresponding values from A2:A42 in the "PENDING QUOTES" sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
If found Then
Dim message As String
message = "Corresponding values:" & vbCrLf
For Each cell In rngPending
If cell.Value = Target.Value Then
message = message & "- " & cell.Offset(0, -1).Value & vbCrLf
End If
Next cell
MsgBox message, vbInformation, Target.Value
End If
End If
End Sub
With this code, the title of the message box will be set to the value of the cell that triggered the event. Now, when you change a cell in the worksheet, the message box will pop up with the corresponding values as the message and the target value as the title.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
If found Then
Dim message As String
message = "The following Quotes for this Provider" & vbCrLf & _
"have not been processed to Finance" & vbCrLf & vbCrLf & _
"Click YES to continue your PRE-REQUEST" & vbCrLf & vbCrLf & _
"Click NO to cancel"
Dim result As VbMsgBoxResult
result = MsgBox(message, vbYesNo + vbInformation, Target.Value)
If result = vbYes Then
' Add your logic to continue the PREQUEST process here
MsgBox "PREQUEST continued"
Else
' Add your logic to cancel the PREQUEST process here
MsgBox "PREQUEST cancelled"
End If
End If
End If
End Sub
With this code, when a cell is changed in the worksheet, a message box will pop up with the formatted message. The message will prompt the user to click Yes or No. Depending on the user's choice, you can add specific logic to continue or cancel the PREQUEST process.
If found Then
Dim message As String
message = "The following Quotes for this Provider" & vbCrLf & _
"have not been processed to Finance" & vbCrLf & vbCrLf & _
"Click YES to continue your PRE-REQUEST" & vbCrLf & vbCrLf & _
"Click NO to Cancel"
Dim result As VbMsgBoxResult
result = MsgBox(message, vbYesNo + vbInformation, Target.Value)
For Each cell In rngPending
If cell.Value = Target.Value Then
message = message & "- " & cell.Offset(0, -1).Value & vbCrLf
End If
Next cell
MsgBox message, vbInformation, Target.Value
If result = vbYes Then
MsgBox "PREQUEST continue"
Else
Target.Value = ""
Range("C" & calcRow).Calculate
Range("K" & calcRow).Calculate
ThisWorkbook.Worksheets("QUOTES").Activate
End If
End If
End If
Dim rngPending As Range
Dim cell As Range
Dim found As Boolean
Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42")
If Not Intersect(Target, Me.UsedRange) Is Nothing Then
For Each cell In rngPending
If cell.Value = Target.Value Then
found = True
Exit For
End If
Next cell
If found Then
Dim message As String
message = "The following Quotes for this Provider" & vbCrLf & _
"have not been processed to Finance" & vbCrLf & vbCrLf & _
"Click YES to continue your PRE-REQUEST" & vbCrLf & vbCrLf & _
"Click NO to Cancel" & vbCrLf
For Each cell In rngPending
If cell.Value = Target.Value Then
message = message & "- " & cell.Offset(0, -1).Value & vbCrLf
End If
Next cell
Dim result As VbMsgBoxResult
result = MsgBox(message, vbYesNo + vbInformation, Target.Value)
If result = vbYes Then
MsgBox "PREQUEST continued"
Else
Target.Value = ""
Range("C" & calcRow).Calculate
Range("K" & calcRow).Calculate
ThisWorkbook.Worksheets("QUOTES").Activate
End If
End If
End If
In this corrected code, I have moved the loop that builds the message to display the corresponding quotes for the provider inside the initial condition check. This way, the message box will properly display the list of quotes before asking the user to click Yes or No. I have also removed the redundant MessageBox statement after the loop.