• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

skip blank autofiltered table

ysherriff

Member
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:

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.

upload_2015-5-5_11-24-7.png

upload_2015-5-5_11-24-56.png
 
The line in yellow, change to:
Code:
If Not tbl Is Nothing Then
When you are setting the tbl Range, if there are no visible cells, it will become nothing, so this is how we check for it.
 
Thanks Luke. There is no error but now it just loops and doesn't select the table and then copy it to the other workbook. I will take a stab at it again to see why it is not selecting and then copy. Here is the modified code:

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 Not tbl Is Nothing 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 Not tbl Is Nothing 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
  
  Range("B4").Select
  Selection.End(xlDown).Select
  rownum = Selection.Row
  Range("b" & rownum + 1).Select
  Range(Selection, Selection.End(xlDown)).Select
  Selection.EntireRow.Delete
  
  Range("b6").Activate
  
  
  
  'On Error Resume Next
  'Kill folderPathStr & "\*.xls"
  'On Error GoTo 0
  
  ActiveWorkbook.SaveAs OutputFilePath & "\" & reportName
  
  ActiveWorkbook.Close , False
  
  
  
  Next I
 
My thoughts on your code, commented with "LUKE M: ..."
Code:
 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)
         
          'LUKE M: Why do we re-filter the table? It's already filtered
          If Not tbl Is Nothing Then .Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").AutoFilter field:=6, Criteria1:=MyCell
        '  ...................................................................
     End With
    '  ...................................................................
    'LUKE M: We already defined tbl, shouldn't need to do this again
     Set tbl = ws.Range("DRG_DISCH_BY_SENDER_RECEIVER_TBL").SpecialCells(xlCellTypeVisible)
      On Error GoTo 0
      If Not tbl Is Nothing Then
            '  ...................................................................
             'LUKE M: DANGER!! the last cell activated was A6. Is this really the cell you want, or should it be tbl?
             ActiveCell.CurrentRegion.Select
             
              'LUKE M: Again, tbl was already defined, not sure why we're changing it again
              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
 
Luke,

Thanks for the post. I have tried a couple of things and still not working. It is still selecting the empty table and trying to copy and paste and I get an error message. The error message pixs are attached. I know it is something simple but can't figure it out. Never happened to me before. I appreciate any insight for a novice.


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("DRG_DISCH_BY_SENDER_RECEIVER_TBL").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)
  
  End With
'  ...................................................................
  On Error GoTo 0
  If Not tbl Is Nothing Then
'  ...................................................................
  ActiveCell.CurrentRegion.Select

  '---my comments... (I tried not to define the table below but when I deleted the set tbl it did not select the table and therefore ignored the command and continued with the loop )

  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
  
  Range("B4").Select
  Selection.End(xlDown).Select
  rownum = Selection.Row
  Range("b" & rownum + 1).Select
  Range(Selection, Selection.End(xlDown)).Select
  Selection.EntireRow.Delete
  
  Range("b6").Activate
  
  
  
  'On Error Resume Next
  'Kill folderPathStr & "\*.xls"
  'On Error GoTo 0
  
  ActiveWorkbook.SaveAs OutputFilePath & "\" & reportName
  
  ActiveWorkbook.Close , False
  
  
  
  Next I

upload_2015-5-6_16-41-29.png


upload_2015-5-6_16-41-55.png
 
I believe the issue lies in this syntax:

If Not tbl Is Nothing Then

It is always displaying the table as empty so it skips and goes to the next MyCell

upload_2015-5-6_17-34-9.png
 
Would it be possible to see your workbook? I'm getting lost as to what's going on. After the filter is applied, it sounded like there were no records found. In which case, tbl is Nothing, and we should go on to the next criteria/MyCell, right?
 
Hi ysherriff,

Unfortunately, I can't access files from outside locations (not a problem with your file specifically, problem on my end). :(

@NARAYANK991 , can you take a look? We're trying to write the code to check if no records are found when table is filtered, but I wasn't able to visualize the layout.
 
Hi ,

I have made some changes in the code , but they are not related to the problem you have mentioned.

The code runs to completion and generates the relevant reports.

I am posting the link to the zipped file , which has been generated after selecting MO in the dropdown in C3 in the Control tab.

Please let me know what I should do to create the error situation , so that I can check it out.

https://www.dropbox.com/s/2efuxy65ci0tz39/Reports.zip?dl=0

Narayan
 
Back
Top