Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: don't copy filtered data if no active cells

  1. #1

    don't copy filtered data if no active cells

    Hello All,

    I am experiencing a problem with some code I am using to filter a spread sheet by Date range and by another criteria. The code works great for the main part but I am hitting a serious problem when there are no active rows i.e. there are no cells in the date range. When this happens instead of not copy any rows over to my other sheet within the same workbook, it pastes ALL the rows from the Raw Data. I tried using an if statement to say if rng <> "" then carry on with code else msg box "No data to copy" but I cant get this to work. Any ideas?

    Code:
    Sub Jan()
    ' ******************* January KPI ***************************************
    
        Dim Jan As Date
        Dim Feb As Date
        Dim rng As Range
        
        Set rng = ActiveSheet.AutoFilter.Range
        
        Jan = #1/1/2012#
        Feb = #1/2/2012#
        
        ' Filter rows to January sailings only
        Range("K5").Select
        Selection.AutoFilter
        Selection.AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
        Criteria2:="<" & Feb, Operator:=xlAnd
        
        ' Filter rows to HKG to Kotka only
        Selection.AutoFilter field:=14, Criteria1:="Hong Kong"
        Selection.AutoFilter field:=15, Criteria1:="Kotka"
        
        'Select sheet "HKG to Kotka and clear old data
        Sheets("HKG to Kotka").Select
        Range("A11:W40").ClearContents
        ActiveWindow.ScrollColumn = 1
        ActiveWindow.ScrollRow = 1
        
        'Go back to Raw Data sheet
        Sheets("Raw Data").Select
        
        'Copy the active rows to HK to Kotka sheet
         rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Copy _
         Destination:=Worksheets("HKG to Kotka").Range("A11")
     
         'Filter to Show All
         ActiveSheet.ShowAllData
    gratefully appreciate any help. Thank you.

  2. #2
    I am having a little sucess with Specialcells(xlVisible), but im note sure who to use this within my code to achieve my goal.

  3. #3
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Try

    Code:
    Sub Jan()
    ' ******************* January KPI ***************************************
        
        Dim wksRawData  As Worksheet
        Dim wksDest     As Worksheet
        Dim rngRawData  As Range
        Dim lngCount    As Long
        
        Dim Jan As Date
        Dim Feb As Date
        Dim rng As Range
        
        
        Set wksRawData = Worksheets("Raw Data")
        Set wksDest = Worksheets("HKG to Kotka")
        
        With wksRawData
            If .AutoFilterMode Then .AutoFilterMode = False 'remove autofilter
            Set rngRawData = .Range("a5:w" & .Range("k" & .Rows.Count).End(xlUp).Row) 'set the range
        End With
        
        Jan = #1/1/2012#
        Feb = #1/2/2012#
        
        With Application.WorksheetFunction
            'count whether jan dates are exist
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:="Hong Kong"
                .AutoFilter field:=15, Criteria1:="Kotka"
                Set rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksDest.Range("A11:W40").ClearContents
            rng.Copy wksDest.Range("A11")
            wksRawData.ShowAllData
        End If
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  4. #4
    Thanks Admin, the code looks great but I am still in the same position. When there are no cells to copy after filter i get the error "No cells were found" but in the case where no active cells are filtered I need to move on to the next bit of code instead if stopping the sub. Thanks for your help
    Code:
    Set rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)

  5. #5
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    I guess there there would no "Hong Kong" or "Kotka" in the range.

    Declare two variables and do a countif for those 2 criterias like I did for date.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  6. #6
    Thanks Admin, I have amended my code as follows but third part of sub doesn't work and I can't figure out why. The full code of the sub is:
    Code:
    Sub Jan()
    '*********************** January KPI Report **************************
    
        Dim wksRawData As Worksheet
        Dim wksHKKot As Worksheet
        Dim wksHKHam As Worksheet
        Dim wksXMNSHAHam As Worksheet
        Dim rngRawData As Range
        Dim lngCount As Long
        
        Dim Jan As Date
        Dim Feb As Date
        Dim HK As String
        Dim KT As String
        Dim HAM As String
        Dim XMN As String
        Dim SHA As String
        
        Set wksRawData = Worksheets("Raw Data")
        Set wksHKKot = Worksheets("HKG to Kotka")
        Set wksHKHam = Worksheets("HKG to Hamburg")
        Set wksXMNSHAHam = Worksheets("XMN | SHA to Hamburg")
        
        Jan = #1/1/2012#
        Feb = #1/2/2012#
        HK = "Hong Kong"
        KT = "Kotka"
        HAM = "Hamburg"
        XMN = "Xiamen"
        SHA = "Shanghai"
        
        With wksRawData
            If .AutoFilterMode Then .AutoFilterMode = False 'remove autofilter
            Set rngRawData = .Range("a5:w" & .Range("k" & .Rows.Count).End(xlUp).Row) 'set the range
        End With
        
        ' **************** Hong Kong to Kotka ***********************************
        With Application.WorksheetFunction
            'count whether any shipment exist in January from Hong Kong to Kotka
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), "<" & Feb) _
            & .CountIf(wksRawData.Columns(14), "=" & HK) _
            & .CountIf(wksRawData.Columns(15), "=" & KT)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=KT
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKKot.Range("A11:W40").ClearContents
            Rng.Copy wksHKKot.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Kotka in January"
        End If
      ' *************************************************************************
         
     ' **************** Hong Kong to Hamburg ***********************************
        With Application.WorksheetFunction
            'count whether any shipment exist in May from Hong Kong to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), "<" & Feb) _
            & .CountIf(wksRawData.Columns(14), "=" & HK) _
            & .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=HAM
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKHam.Range("A11:W40").ClearContents
            Rng.Copy wksHKHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
      ' *************************************************************************
      
      ' **************** Xiamen | Shanghai to Hamburg ***********************************
      
        With Application.WorksheetFunction
            'count whether any shipment exist in May from Xiamen and Shanghai to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), "<" & Feb) _
            & .CountIf(wksRawData.Columns(14), "=" & XMN) _
            & .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=XMN
                .AutoFilter field:=15, Criteria1:=HAM
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksXMNSHAHam.Range("A11:W40").ClearContents
            Rng.Copy wksXMNSHAHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
        
      ' *************************************************************************
         
        Set wksRawData = Nothing
        Set wksHKKot = Nothing
        Set wksHKHam = Nothing
        Set wksXMNSHAHam = Nothing
        
    End Sub
    the code that's not working is after '***** Xiamen | Shanghai to Hamburg *******

    I'm almost certainly doing something stupid but I am stumped cheers.

  7. #7
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    try

    Code:
    Sub Jan()
    '*********************** January KPI Report **************************
    
        Dim wksRawData As Worksheet
        Dim wksHKKot As Worksheet
        Dim wksHKHam As Worksheet
        Dim wksXMNSHAHam As Worksheet
        Dim rngRawData As Range
        Dim lngCount As Long
        
        Dim Jan As Date
        Dim Feb As Date
        Dim HK As String
        Dim KT As String
        Dim HAM As String
        Dim XMN As String
        Dim SHA As String
        
        Set wksRawData = Worksheets("Raw Data")
        Set wksHKKot = Worksheets("HKG to Kotka")
        Set wksHKHam = Worksheets("HKG to Hamburg")
        Set wksXMNSHAHam = Worksheets("XMN | SHA to Hamburg")
        
        Jan = #1/1/2012#
        Feb = #1/2/2012#
        HK = "Hong Kong"
        KT = "Kotka"
        HAM = "Hamburg"
        XMN = "Xiamen"
        SHA = "Shanghai"
        
        With wksRawData
            If .AutoFilterMode Then .AutoFilterMode = False 'remove autofilter
            Set rngRawData = .Range("a5:w" & .Range("k" & .Rows.Count).End(xlUp).Row) 'set the range
        End With
        
        ' **************** Hong Kong to Kotka ***********************************
        With Application.WorksheetFunction
            'count whether any shipment exist in January from Hong Kong to Kotka
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & KT)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=KT
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKKot.Range("A11:W40").ClearContents
            Rng.Copy wksHKKot.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Kotka in January"
        End If
      ' *************************************************************************
         
     ' **************** Hong Kong to Hamburg ***********************************
        'reset lngCount
        lngCount = 0
        With Application.WorksheetFunction
            'count whether any shipment exist in May from Hong Kong to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=HAM
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKHam.Range("A11:W40").ClearContents
            Rng.Copy wksHKHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
      ' *************************************************************************
      
      ' **************** Xiamen | Shanghai to Hamburg ***********************************
        'reset lngCount
        lngCount = 0
        With Application.WorksheetFunction
            'count whether any shipment exist in May from Xiamen and Shanghai to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & XMN)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=XMN
                .AutoFilter field:=15, Criteria1:=HAM
                Set Rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksXMNSHAHam.Range("A11:W40").ClearContents
            Rng.Copy wksXMNSHAHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
        
      ' *************************************************************************
         
        Set wksRawData = Nothing
        Set wksHKKot = Nothing
        Set wksHKHam = Nothing
        Set wksXMNSHAHam = Nothing
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  8. #8
    Thanks Admin but still get the same error "No cells were found" so its not saying the lngcount exists though it does. Also its not picking up on the else msgbox for some reason?

  9. #9
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Could you attach a sample workbook? Remove the sensitive data and put some dummy values.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  10. #10
    Thanks Admin, I tweeked a couple of things and now works brilliant. second filter criteria was > Feb and should have been < Feb Thanks for All you help. Final code that worked:=
    Code:
    Sub Jan()
    '*********************** January KPI Report **************************
    
        Dim wksRawData As Worksheet
        Dim wksHKKot As Worksheet
        Dim wksHKHam As Worksheet
        Dim wksXMNSHAHam As Worksheet
        Dim rngRawData As Range
        Dim lngCount As Long
        
        Dim Jan As Date
        Dim Feb As Date
        Dim HK As String
        Dim KT As String
        Dim HAM As String
        Dim XMN As String
        Dim SHA As String
        
        Set wksRawData = Worksheets("Raw Data")
        Set wksHKKot = Worksheets("HKG to Kotka")
        Set wksHKHam = Worksheets("HKG to Hamburg")
        Set wksXMNSHAHam = Worksheets("XMN | SHA to Hamburg")
        
        Jan = #1/1/2012#
        Feb = #1/2/2012#
        HK = "Hong Kong"
        KT = "Kotka"
        HAM = "Hamburg"
        XMN = "Xiamen"
        SHA = "Shanghai"
        
        With wksRawData
            If .AutoFilterMode Then .AutoFilterMode = False 'remove autofilter
            Set rngRawData = .Range("a5:w" & .Range("k" & .Rows.Count).End(xlUp).Row) 'set the range
        End With
        
        ' **************** Hong Kong to Kotka ***********************************
        With Application.WorksheetFunction
            'count whether any shipment exist in January from Hong Kong to Kotka
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & KT)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=KT
                Set rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKKot.Range("A11:W40").ClearContents
            rng.Copy wksHKKot.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Kotka in January"
        End If
      ' *************************************************************************
         
     ' **************** Hong Kong to Hamburg ***********************************
        'reset lngCount
        lngCount = 0
        With Application.WorksheetFunction
            'count whether any shipment exist in January from Hong Kong to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), ">" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=HK
                .AutoFilter field:=15, Criteria1:=HAM
                Set rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksHKHam.Range("A11:W40").ClearContents
            rng.Copy wksHKHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
      ' *************************************************************************
      
      ' **************** Xiamen | Shanghai to Hamburg ***********************************
        'reset lngCount
        lngCount = 0
        With Application.WorksheetFunction
            'count whether any shipment exist in January from Xiamen and Shanghai to Hamburg
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & Jan) - .CountIf(wksRawData.Columns(11), "<" & Feb)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & SHA)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & XMN)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With
        
        'if exist
        If lngCount Then
            With rngRawData
                .AutoFilter field:=11, Criteria1:=">=" & Jan, Operator:=xlAnd, _
                        Criteria2:="<" & Feb, Operator:=xlAnd
                .AutoFilter field:=14, Criteria1:=SHA, Operator:=xlOr, _
                Criteria2:=XMN
                .AutoFilter field:=15, Criteria1:=HAM
                Set rng = .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).SpecialCells(12)
            End With
            wksXMNSHAHam.Range("A11:W40").ClearContents
            rng.Copy wksXMNSHAHam.Range("A11")
            wksRawData.ShowAllData
            Else
            MsgBox "No Shipments from Hong Kong to Hamburg in January"
        End If
        
      ' *************************************************************************
         
        Set wksRawData = Nothing
        Set wksHKKot = Nothing
        Set wksHKHam = Nothing
        Set wksXMNSHAHam = Nothing
        
    End Sub

Similar Threads

  1. Highlighting All the Cells of Active sheet which contains a particular String:
    By littleiitin in forum Excel and VBA Tips and Tricks
    Replies: 6
    Last Post: 10-18-2013, 04:19 PM
  2. Macro to copy data in specific Columns
    By Howardc in forum Excel Help
    Replies: 0
    Last Post: 04-19-2013, 10:42 AM
  3. Replies: 2
    Last Post: 02-11-2013, 08:13 PM
  4. Copy selected data to other excel sheet
    By dhiraj.ch185 in forum Excel Help
    Replies: 2
    Last Post: 02-02-2012, 06:23 AM
  5. Unique Count on a Filtered Range
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 08-14-2011, 04:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •