Hi
I'm having trouble with an error handling section of code that fails sometimes, but I can't figure out why it.
I have a spreadsheet with values, and there will be occasions when to include the (value x qty) and when not to, I have a for loop checking each line, it will either list "strBuy" with a "Yes" or "No" this part of the code works fine.
The issue is when using worksheetfunction ".Cells(ch, "S") = Application.WorksheetFunction.Sum(.Cells(ch, "R") * .Cells(ch, "I"))", there will be times when there is no value for the price but text instead. This will cause the formula to error. What I'm looking for is to put "Unknow Price" when there is an error, or do the sum when it's fine and continue to the next line.
Using windows 11 64 bit, with office 2016.
This is the code that I'm having issues with
This is the full code
Any help will be appreciated.
Thanks
Simon
I'm having trouble with an error handling section of code that fails sometimes, but I can't figure out why it.
I have a spreadsheet with values, and there will be occasions when to include the (value x qty) and when not to, I have a for loop checking each line, it will either list "strBuy" with a "Yes" or "No" this part of the code works fine.
The issue is when using worksheetfunction ".Cells(ch, "S") = Application.WorksheetFunction.Sum(.Cells(ch, "R") * .Cells(ch, "I"))", there will be times when there is no value for the price but text instead. This will cause the formula to error. What I'm looking for is to put "Unknow Price" when there is an error, or do the sum when it's fine and continue to the next line.
Using windows 11 64 bit, with office 2016.
This is the code that I'm having issues with
VBA Code:
If strBuy = "Yes" Then
On Error GoTo PriceUnknownB
.Cells(ch, "S") = Application.WorksheetFunction.Sum(.Cells(ch, "R") * .Cells(ch, "I"))
GoTo PriceContinueB
PriceUnknownB:
.Cells(ch, "S") = "Unknown Price"
PriceContinueB:
On Error GoTo 0
End If
This is the full code
VBA Code:
With Sheet5
lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
For ch = 6 To lLastRow
lLevel = .Cells(ch, "A")
If lLevel = 1 Then l1 = .Cells(ch, "H")
If lLevel = 2 Then l2 = .Cells(ch, "H")
If lLevel = 1 Then
If l1 = "B" Then
strBuy = "Yes"
Else
strBuy = "No"
End If
End If
If lLevel = 2 Then
If l1 = "B" Then
strBuy = "No"
ElseIf l2 = "B" Then
strBuy = "Yes"
Else
strBuy = "No"
End If
End If
If strBuy = "Yes" Then
On Error GoTo PriceUnknownB
.Cells(ch, "S") = Application.WorksheetFunction.Sum(.Cells(ch, "R") * .Cells(ch, "I"))
GoTo PriceContinueB
PriceUnknownB:
.Cells(ch, "S") = "Unknown Price"
PriceContinueB:
On Error GoTo 0
End If
Next
End With
Any help will be appreciated.
Thanks
Simon