Home > OS >  How to bypass a code(not use it) if criteria don't match (error)?
How to bypass a code(not use it) if criteria don't match (error)?

Time:02-25

The code is working perfectly when the criteria exist. But, I am getting an error when the criteria doesn't exist. So how can I use a boolean or another function to by pass the below code if the criteria doesn't exist which happens sometimes. So it doesn't break my program.

    ' Define constants.
        Const srcName As String = "wfm_rawdata"
        Const srcFirst As String = "D2" ' Location for Group
        Const dstName As String = "bond_insurance"
        Const dstFirst As String = "A2"  ' do not change the 'A' (entire row).
          
          
      'This function will transfer rows from one worksheet to another worksheet if the value = specified critiera
        ' Define workbook.
        Dim wb As Workbook: Set wb = ActiveWorkbook ' Workbook containing this code.
        
        ' Define Source Range
        Dim LastRow As Long
        Dim srg As Range
        
        ' Define worksheet and column am working on and getting the range of last used cell using(LastRow)
        With wb.Worksheets(srcName).Range(srcFirst)
            LastRow = .Offset(.Worksheet.Rows.Count - .Row).End(xlUp).Row
            Set srg = .Resize(LastRow - .Row   1, 10)
        End With
          
        'Combine' critical cells into a range.
        Dim brg As Range ' Built Range --> Range in the new sheet
        Dim cel As Range ' Current Cell Range --> Range in the current sheet(rawdata)
        
        'for every cell in group within wfm_rawdata sheet if the value = GO
        For Each cel In srg.Cells
            If cel.Value = "BOND INSURANCE" Then
                ' If the range in the new sheet have nothing then add specific criteria from the group in wfm_rawdata
                If brg Is Nothing Then
                    Set brg = cel
                 ' if there is range in there combine the new and old range together  using -> Union function
                Else
                    Set brg = Union(brg, cel)
                End If
            End If
        Next cel
        
        Application.ScreenUpdating = False
        
        ' Copy and delete critical rows of Source Range.
        With wb.Worksheets(dstName).Range(dstFirst)
            .Resize(.Worksheet.Rows.Count - .Row   1, _
                .Worksheet.Columns.Count).clear
            Set brg = brg.EntireRow ' 'Convert' cells into rows.
            brg.Copy .Offset ' Copy. 'Offset' because range is in 'With'.
            brg.Delete ' Delete.
        End With

I want the above code to run only if the criteria exist for example if criteria "dog" exist then run the code and if it doesn't exist bypass the code. Thank you.

Sub master()
Call report1
Call report2
Call report3
End Sub

I use the above code to run all the 3 modules with the codes similar to the top code.

CodePudding user response:

One you've assigned srg you can use Match() to check whether it contains any instances of the term you're interested in:

'...
'...
' Define worksheet and column am working on and getting the range of last used cell using(LastRow)
With wb.Worksheets(srcName).Range(srcFirst)
    LastRow = .Offset(.Worksheet.Rows.Count - .Row).End(xlUp).Row
    Set srg = .Resize(LastRow - .Row   1, 10)
End With

'Exit if "BOND INSURANCE" is not found in `srg`
If IsError(Application.Match("BOND INSURANCE", srg, 0)) Then Exit Sub
'...
'...
  • Related