Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

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

  1. #11
    Oh Dear Just when I thought it was sorted, I come across a month that doesn't have any data to copy and so have found out the lngcount to see if data does exist is not working. Any idea?
    Code:
    ' **************** 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), ">=" & May) _
            - .CountIf(wksRawData.Columns(11), ">" & Jun)
            lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
            lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & HAM)
        End With

  2. #12
    panic over. I replaced '*' with '&' now picks up if blank. Thanks again.

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

    Ideally the * should be there. The logic is, it multiplies each countif results. So if any countif returns 0, then the code won't work.
    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. #14
    Very good point. I ran through the code line by line and found the lngCount was a - figure so changed code to the following. Do you think it would be ok? if lngCount > 0 copy data. That way only when there is at least 1 line of data will code copy the data else it will show message. TBH my brain is getting a little fried.

    Code:
    With Application.WorksheetFunction
            'count whether any shipment exist in May from Hong Kong to Kotka
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & May) _
            - .CountIf(wksRawData.Columns(11), ">" & Jun) _
            * .CountIf(wksRawData.Columns(14), "=" & HK) _
            * .CountIf(wksRawData.Columns(15), "=" & KT)
        End With
        
        'if exist
        If lngCount > 0 Then
    Thank you for being patient.

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

    Not sure how a -figure comes.

    split each count on different line of code rather than puting a single line.

    Code:
    With Application.WorksheetFunction
        'count whether any shipment exist in May from Hong Kong to Kotka
        lngCount = .CountIf(wksRawData.Columns(11), ">=" & May) - .CountIf(wksRawData.Columns(11), ">" & Jun)
        lngCount = lngCount * .CountIf(wksRawData.Columns(14), "=" & HK)
        lngCount = lngCount * .CountIf(wksRawData.Columns(15), "=" & KT)
    End With
    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. #16
    Thanks Admin. I have spent a few hours on it but can't get it to work. the above code won't work The problem is I have to run the same code for each month of the year to copy data for shipment in that month for shipments from port1 to port2. The code we have filters the code to the month which is great but the problem with the code is :
    Code:
    With Application.WorksheetFunction
            'count whether any shipment exist in May from Hong Kong to Kotka
            lngCount = .CountIf(wksRawData.Columns(11), ">=" & May) _
            - .CountIf(wksRawData.Columns(11), ">" & Jun) _
            * .CountIf(wksRawData.Columns(14), "=" & HK) _
            * .CountIf(wksRawData.Columns(15), "=" & KT)
        End With
    the main problem is that when using 'CountIf(wksRawData.Columns' this uses the whole sheet as range not just the month in question. So I need to filter month, then only look for shipments from port1 to port 2 within the month range.

  7. #17
    Perhaps we can check for shipments in the month (which we have) then set that as a range. We can then use that range to filter the ports and copy the data. Not sure how to code that though

  8. #18
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    If you could attach a sample workbook, I'll have a look.
    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)

  9. #19

    Red face

    Thanks admin. Attaching now. The attached uses the month on June as the example. To summerise, it checks the whole sheet to see if any shipments but I need it to only check the date range (June). Thanks again Attachment 424

  10. #20
    I'm very nearly there. I just need the following code corrected so it only checks column 14 of visible rows.
    Code:
    lngCount = 0
        With Application.WorksheetFunction
            'count whether any shipment exist in June from Hong Kong to Kotka
            lngCount = rngRawData.SpecialCells(xlCellTypeVisible).CountIf(Columns(14), HK)
            End With
    Thanks

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
  •