escaping from coding

MAMIBUSSOL

Board Regular
Joined
Jun 2, 2011
Messages
95
i am running a piece of code which filters some data based on an account
I am then copying this data to another worksheet, this code does this if it has matching criteria, if the filter contains no results, it still works but it copies every piece of data.

Code:
    With ActiveWorkbook.Worksheets("CRED")
        .Range("$A$1:$IE$388").AutoFilter Field:=3, Criteria1:="=ADVERT"
        .Range("$A$3:$G$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End With

is it possible to adjust the above code to drop out of the with statement if the filter produces NO results and not copy any data, so in essence bypass this line of code
Code:
        .Range("$A$3:$G$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this line before the copy:
Code:
If .AutoFilter.Range.Rows.SpecialCells(xlCellTypeVisible).Count > 0 Then
    .Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
End If
 
Upvote 0
Code:
With ActiveWorkbook.Worksheets("CRED")
    If Not .Range("C1:C388").Find("ADVERT", , xlValues, xlWhole, , , False) Is Nothing Then
        .Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=ADVERT"
        .Range("A3:G$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End If
End With
 
Upvote 0
Tried your adjustment, got a debug error

PHP:
copy method of worksheet class failed

i am assuming you was think of the following adjustment to the code

Code:
    With ActiveWorkbook.Worksheets("CRED")
        .Range("$A$1:$IE$388").AutoFilter Field:=3, Criteria1:="=ADVERT"
        If .AutoFilter.Range.Rows.SpecialCells(xlCellTypeVisible).Count > 0 Then
            .Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
        End If
    End With

if I adjust your code to this I get rid of the error, however the same happens

Code:
.Range("$A$3:$G$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
AlphaFrog same problem but in reverse, I get no data transferred, when I step through the code it exits the if statement
 
Upvote 0
I spoke too soon AlphaFrog

I've adjusted my coding to accommodate your suggestions, however, its being picky
Code:
    ActiveWorkbook.Worksheets("CRED").Select
    With ActiveWorkbook.Worksheets("CRED")
        If Not .Range("C1:C388").Find("ADVERT", , xlValues, xlWhole, , , False) Is Nothing Then
            .Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=ADVERT"
            .Range("A3:G$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
        End If
    End With
    With ActiveWorkbook.Worksheets("CRED")
        If Not .Range("C1:C388").Find("B DEPREC", , xlValues, xlWhole, , , False) Is Nothing Then
            .Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=B DEPREC"
            .Range("A3:G$388").Copy Sheets("TRIAL").Range("J" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
        End If
    End With
    With ActiveWorkbook.Worksheets("CRED")
        If Not .Range("C1:C388").Find("BAR EQUIP", , xlValues, xlWhole, , , False) Is Nothing Then
            .Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=BAR EQUIP"
            .Range("A3:G$388").Copy Sheets("TRIAL").Range("S" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
        End If
    End With
    With ActiveWorkbook.Worksheets("CRED")
        If Not .Range("C1:C388").Find("BAR SUNDRIES", , xlValues, xlWhole, , , False) Is Nothing Then
            .Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=BAR SUNDRIES"
            .Range("A3:G$388").Copy Sheets("TRIAL").Range("S" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
        End If
    End With

there are no records for ADVERT & B DEPREC however BAR EQUIP & BAR SUNDRIES have records.

the coding seems to work for BAR EQUIP, but the BAR SUNDRIES doesn't
 
Upvote 0
Code:
    Application.ScreenUpdating = False
    ActiveWorkbook.Worksheets("CRED").Select
    Range("A1:IE388").AutoFilter 'Reset autofilter
    If Not Range("C1:C388").Find("ADVERT", , xlValues, xlWhole, , , False) Is Nothing Then
        Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=ADVERT"
        Range("A3:G388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End If
    Range("A1:IE388").AutoFilter 'Reset autofilter
    If Not Range("C1:C388").Find("B DEPREC", , xlValues, xlWhole, , , False) Is Nothing Then
        Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=B DEPREC"
        Range("A3:G388").Copy Sheets("TRIAL").Range("J" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End If
    Range("A1:IE388").AutoFilter 'Reset autofilter
    If Not Range("C1:C388").Find("BAR EQUIP", , xlValues, xlWhole, , , False) Is Nothing Then
        Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=BAR EQUIP"
        Range("A3:G388").Copy Sheets("TRIAL").Range("S" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End If
    Range("A1:IE388").AutoFilter 'Reset autofilter
    If Not Range("C1:C388").Find("BAR SUNDRIES", , xlValues, xlWhole, , , False) Is Nothing Then
        Range("A1:IE388").AutoFilter Field:=3, Criteria1:="=BAR SUNDRIES"
        Range("A3:G388").Copy Sheets("TRIAL").Range("S" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
    End If
    Worksheets("CRED").AutoFilterMode = False 'Turnoff autofilter
    Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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