In the VBA event below I am getting an error onthe following: If Not wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And Not wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value Then
Sub SearchSubmittedQuotes(Target As Range)
code
Assistant
The issue in the code is with the If Not condition. The logic of the condition needs to be adjusted. Instead of using And Not, you should use If Not for each individual comparison and then combine them with And.
Here is the corrected code snippet:
vba
' Check if both conditions are false
If Not (wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value) Then
' Display message
MsgBox "NO QUOTE ENTRY FOUND" & vbCrLf & "New Entry Will Be Recorded", vbInformation, "NO QUOTE ENTRY"
' Find the next blank cell in column A
nextBlankRow = wsQuotes.Cells(wsQuotes.Rows.count, "A").End(xlUp).Row + 1
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
' Rest of the code for adding a new entry in quotes sheet
End If
User
Assistant
The error occurs because you are trying to access the outside the loop. The variable is declared within the loop and its scope is limited to that loop. To fix this issue, you should declare and set another range variable outside the loop to store the cell where the conditions are not met. Here's the updated code:
User
How does your ammendment know what the notFoundCell values are
Assistant
Dim wsRequest As Worksheet
Dim wsQuotes As Worksheet
Dim cell As Range
Dim searchValue As Variant
Dim nextBlankRow As Long
Application.EnableEvents = False
Set wsRequest = ThisWorkbook.Sheets("PREQUEST")
Set wsQuotes = ThisWorkbook.Sheets("QUOTES")
wsQuotes.Unprotect Password:="edit"
searchValue = Target.Offset(0, -7).Value
For Each cell In wsQuotes.Range("A5:A" & wsQuotes.Cells(wsQuotes.Rows.count, "A").End(xlUp).row)
If wsQuotes.Cells(cell.row, 1).Value = searchValue Then
If wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value Then
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
wsQuotes.Cells(cell.row, 8).Value = wsRequest.Cells(Target.row, "H").Value ' NOTES
wsQuotes.Cells(cell.row, 9).Value = wsRequest.Cells(Target.row, "I").Value ' P REQUEST#
wsQuotes.Cells(cell.row, 10).Value = wsRequest.Cells(Target.row, "J").Value ' DATE
wsQuotes.Cells(cell.row, 13).Value = "Finance"
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
End If
Next cell
' Check if both conditions are false
If Not wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And Not wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value Then
' Display message
MsgBox "NO QUOTE ENTRY FOUND" & vbCrLf & "New Entry Will Be Recorded", vbInformation, "NO QUOTE ENTRY"
' Find the next blank cell in column A
nextBlankRow = wsQuotes.Cells(wsQuotes.Rows.count, "A").End(xlUp).row + 1
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
wsQuotes.Cells(nextBlankRow, 1).Value = wsRequest.Cells(Target.row, "A").Value ' CONTRACTOR
wsQuotes.Cells(nextBlankRow, 2).Value = wsRequest.Cells(Target.row, "B").Value ' JOB
wsQuotes.Cells(nextBlankRow, 3).Value = wsRequest.Cells(Target.row, "C").Value ' SECTOR
wsQuotes.Cells(nextBlankRow, 4).Value = wsRequest.Cells(Target.row, "D").Value ' SUBSECTOR
wsQuotes.Cells(nextBlankRow, 5).Value = wsRequest.Cells(Target.row, "E").Value ' QUO/INV#
wsQuotes.Cells(nextBlankRow, 6).Value = wsRequest.Cells(Target.row, "F").Value ' VALUE
wsQuotes.Cells(nextBlankRow, 7).Value = wsRequest.Cells(Target.row, "G").Value ' LOCATION
wsQuotes.Cells(nextBlankRow, 8).Value = wsRequest.Cells(Target.row, "H").Value ' NOTES
wsQuotes.Cells(nextBlankRow, 9).Value = wsRequest.Cells(Target.row, "I").Value ' P REQUEST#
wsQuotes.Cells(nextBlankRow, 10).Value = wsRequest.Cells(Target.row, "J").Value ' DATE
wsQuotes.Cells(nextBlankRow, 13).Value = "Finance"
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
Target.AddComment "Quote sent to Finance " & Date
wsQuotes.Protect Password:="edit"
Application.Wait Now + TimeValue("00:00:01")
ProcessQuote Target
End Sub
By placing the conditions inside parentheses and using Not for each individual comparison, you can ensure that the conditions are applied correctly.
Error: Object variable or With block variable not set, on the lines 'If Not (wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value) Then
in the VBA event:
Sub SearchSubmittedQuotes(Target As Range)
code
Dim wsRequest As Worksheet
Dim wsQuotes As Worksheet
Dim cell As Range
Dim searchValue As Variant
Dim nextBlankRow As Long
Application.EnableEvents = False
Set wsRequest = ThisWorkbook.Sheets("PREQUEST")
Set wsQuotes = ThisWorkbook.Sheets("QUOTES")
wsQuotes.Unprotect Password:="edit"
searchValue = Target.Offset(0, -7).Value
For Each cell In wsQuotes.Range("A5:A" & wsQuotes.Cells(wsQuotes.Rows.count, "A").End(xlUp).row)
If wsQuotes.Cells(cell.row, 1).Value = searchValue Then
If wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value Then
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
wsQuotes.Cells(cell.row, 8).Value = wsRequest.Cells(Target.row, "H").Value ' NOTES
wsQuotes.Cells(cell.row, 9).Value = wsRequest.Cells(Target.row, "I").Value ' P REQUEST#
wsQuotes.Cells(cell.row, 10).Value = wsRequest.Cells(Target.row, "J").Value ' DATE
wsQuotes.Cells(cell.row, 13).Value = "Finance"
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
End If
Next cell
' Check if both conditions are false
If Not (wsQuotes.Cells(cell.row, 5).Value = wsRequest.Cells(Target.row, "E").Value _
And wsQuotes.Cells(cell.row, 6).Value = wsRequest.Cells(Target.row, "F").Value) Then
' Display message
MsgBox "NO QUOTE ENTRY FOUND" & vbCrLf & "New Entry Will Be Recorded", vbInformation, "NO QUOTE ENTRY"
code
' Find the next blank cell in column A
nextBlankRow = wsQuotes.Cells(wsQuotes.Rows.count, "A").End(xlUp).row + 1
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
wsQuotes.Cells(nextBlankRow, 1).Value = wsRequest.Cells(Target.row, "A").Value ' CONTRACTOR
wsQuotes.Cells(nextBlankRow, 2).Value = wsRequest.Cells(Target.row, "B").Value ' JOB
wsQuotes.Cells(nextBlankRow, 3).Value = wsRequest.Cells(Target.row, "C").Value ' SECTOR
wsQuotes.Cells(nextBlankRow, 4).Value = wsRequest.Cells(Target.row, "D").Value ' SUBSECTOR
wsQuotes.Cells(nextBlankRow, 5).Value = wsRequest.Cells(Target.row, "E").Value ' QUO/INV#
wsQuotes.Cells(nextBlankRow, 6).Value = wsRequest.Cells(Target.row, "F").Value ' VALUE
wsQuotes.Cells(nextBlankRow, 7).Value = wsRequest.Cells(Target.row, "G").Value ' LOCATION
wsQuotes.Cells(nextBlankRow, 8).Value = wsRequest.Cells(Target.row, "H").Value ' NOTES
wsQuotes.Cells(nextBlankRow, 9).Value = wsRequest.Cells(Target.row, "I").Value ' P REQUEST#
wsQuotes.Cells(nextBlankRow, 10).Value = wsRequest.Cells(Target.row, "J").Value ' DATE
wsQuotes.Cells(nextBlankRow, 13).Value = "Finance"
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
Target.AddComment "Quote sent to Finance " & Date
wsQuotes.Protect Password:="edit"
Application.Wait Now + TimeValue("00:00:01")
ProcessQuote Target
End Sub
cell
cell
vba
Sub SearchSubmittedQuotes(Target As Range)
Dim wsRequest As Worksheet
Dim wsQuotes As Worksheet
Dim cell As Range
Dim searchValue As Variant
Dim nextBlankRow As Long
Dim notFoundCell As Range ' Declare a variable to store the cell where conditions are not met
Application.EnableEvents = False
Set wsRequest = ThisWorkbook.Sheets("PREQUEST")
Set wsQuotes = ThisWorkbook.Sheets("QUOTES")
wsQuotes.Unprotect Password:="edit"
searchValue = Target.Offset(0, -7).Value
For Each cell In wsQuotes.Range("A5:A" & wsQuotes.Cells(wsQuotes.Rows.Count, "A").End(xlUp).Row)
If wsQuotes.Cells(cell.Row, 1).Value = searchValue Then
If wsQuotes.Cells(cell.Row, 5).Value = wsRequest.Cells(Target.Row, "E").Value _
And wsQuotes.Cells(cell.Row, 6).Value = wsRequest.Cells(Target.Row, "F").Value Then
' Update existing quote
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
' Code to update quote
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
End If
Next cell
' Check if both conditions are false
If notFoundCell Is Nothing Then ' Check if notFoundCell is still Nothing
' Display message
MsgBox "NO QUOTE ENTRY FOUND" & vbCrLf & "New Entry Will Be Recorded", vbInformation, "NO QUOTE ENTRY"
' Find the next blank cell in column A
nextBlankRow = wsQuotes.Cells(wsQuotes.Rows.Count, "A").End(xlUp).Row + 1
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
' Code to add new entry in quotes sheet
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
Target.AddComment "Quote sent to Finance " & Date
wsQuotes.Protect Password:="edit"
Application.Wait Now + TimeValue("00:00:01")
ProcessQuote Target
End Sub
In this updated code, I introduced a new notFoundCell variable to store the cell where the conditions are not met. This variable is then used to check if a new entry should be added when no existing quote is found. Adjust the update code as needed to fit your requirements.
Apologies for the confusion. I misunderstood your initial requirement. To determine if a quote entry is not found in the loop and then add a new entry based on that, you can introduce a boolean flag variable to track if any matching quote entry is found. Here's an updated version of the code:
vba
Sub SearchSubmittedQuotes(Target As Range)
Dim wsRequest As Worksheet
Dim wsQuotes As Worksheet
Dim cell As Range
Dim searchValue As Variant
Dim nextBlankRow As Long
Dim foundQuote As Boolean
Application.EnableEvents = False
Set wsRequest = ThisWorkbook.Sheets("PREQUEST")
Set wsQuotes = ThisWorkbook.Sheets("QUOTES")
wsQuotes.Unprotect Password:="edit"
searchValue = Target.Offset(0, -7).Value
foundQuote = False ' Initialize flag variable
For Each cell In wsQuotes.Range("A5:A" & wsQuotes.Cells(wsQuotes.Rows.Count, "A").End(xlUp).Row)
If wsQuotes.Cells(cell.Row, 1).Value = searchValue And _
wsQuotes.Cells(cell.Row, 5).Value = wsRequest.Cells(Target.Row, "E").Value And _
wsQuotes.Cells(cell.Row, 6).Value = wsRequest.Cells(Target.Row, "F").Value Then
' Update existing quote
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
' Code to update quote
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
foundQuote = True ' Set flag if a matching quote is found
Exit For ' Exit the loop once a matching quote is found
End If
Next cell
If Not foundQuote Then
MsgBox "NO QUOTE ENTRY FOUND" & vbCrLf & "New Entry Will Be Recorded", vbInformation, "NO QUOTE ENTRY"
' Find the next blank cell in column A
nextBlankRow = wsQuotes.Cells(wsQuotes.Rows.Count, "A").End(xlUp).Row + 1
ThisWorkbook.Sheets("QUOTES").Unprotect Password:="edit"
' Code to add new entry in quotes sheet
ThisWorkbook.Sheets("QUOTES").Protect Password:="edit"
End If
Target.AddComment "Quote sent to Finance " & Date
wsQuotes.Protect Password:="edit"
Application.Wait Now + TimeValue("00:00:01")
ProcessQuote Target
End Sub
In this revised version, the foundQuote boolean variable is used to track if a matching quote entry is found while looping through the quotes. If no matching quote is found, the code inside the If Not foundQuote Then block will be executed to add a new entry. The flag is set to True when a matching quote is found, and the loop is exited using Exit For.