Help with error handling inside a for loop

Firesword

New Member
Joined
Oct 10, 2018
Messages
46
Office Version
  1. 2016
Platform
  1. Windows
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
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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Please consider this


VBA Code:
    On Error Resume Next
    If IsError(.Cells(ch, "R") * .Cells(ch, "I")) Then
      .Cells(ch, "S").Value = "Unknown Price"
    Else
      .Cells(ch, "S").Value = (.Cells(ch, "R") * .Cells(ch, "I"))
    End If
    On Error GoTo 0

There didn't seem to be a reason to sum the multiplication of two numbers
 
Upvote 1
Solution
Hi
untested but see if this update resolves your issue
Rich (BB code):
  Dim Price As Double
    With Sheet5
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        For ch = 6 To lLastRow
            
            'your code
            
            If strBuy = "Yes" Then
                Price = Val(.Cells(ch, "R")) * Val(.Cells(ch, "I"))
                .Cells(ch, "S") = IIf(Price > 0, Price, "Unknown Price")
            End If
        Next ch
        
    End With

Dave
 
Upvote 0
Please consider this


VBA Code:
    On Error Resume Next
    If IsError(.Cells(ch, "R") * .Cells(ch, "I")) Then
      .Cells(ch, "S").Value = "Unknown Price"
    Else
      .Cells(ch, "S").Value = (.Cells(ch, "R") * .Cells(ch, "I"))
    End If
    On Error GoTo 0

There didn't seem to be a reason to sum the multiplication of two numbers
Pefect, thank you very much.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top