Hi,
I am having an issue with my code. I have a code that filters and copy to a different workbook. If the filtered table has no data, i do not want to copy and move to the next parameter in the range that needs to be filtered.
Here is my code below and I am getting this error message:
https://www.dropbox.com/s/912qfod8i8mxp23/Pulmonary%20Selection.zip?dl=0
I have attached the link to the workbook as well. The error is generated below in yellow.


I am having an issue with my code. I have a code that filters and copy to a different workbook. If the filtered table has no data, i do not want to copy and move to the next parameter in the range that needs to be filtered.
Here is my code below and I am getting this error message:
Code:
'----------------- CODE TO COPY TO DRG TAB
Sheets("DRG Sender to Receiver").Activate
Set DestCell = TargetSh.Range("DRG_SENDER_RECEIVER_START_CELL")
Set DestCell = DestCell.Offset(1, 0)
TargetSh.Activate
Rows("6:" & Rows.Count).ClearContents
For Each MyCell In MyRange
LastRow = ActiveSheet.Range("B50000").End(xlUp).Row
If MyCell.Value = "" Then Exit For ' this exits when you have a blank cell
wkbkGen.Activate
Worksheets("DRG by Sender and Receiver").Activate
' select the range and autofilter based on hospital name
Range("a6").Select
With ActiveSheet
.AutoFilterMode = False
.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").AutoFilter
.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").AutoFilter field:=13, Criteria1:=statename
.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").AutoFilter field:=6, Criteria1:=MyCell
' ...................................................................
On Error Resume Next
Set tbl = ws.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").SpecialCells(xlCellTypeVisible)
If tbl.Rows.Count > 1 Then .Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").AutoFilter field:=6, Criteria1:=MyCell
' ...................................................................
End With
' ...................................................................
Set tbl = ws.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If tbl.Rows.Count > 1 Then
' ...................................................................
ActiveCell.CurrentRegion.Select
'Set tbl = ActiveCell.CurrentRegion
tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Select
Selection.Copy
'activate template
wkbkTemp.Activate
TargetSh.Activate
TargetSh.Range(DestCell.Address).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set DestCell = TargetSh.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
End If
Next MyCell
https://www.dropbox.com/s/912qfod8i8mxp23/Pulmonary%20Selection.zip?dl=0
I have attached the link to the workbook as well. The error is generated below in yellow.

