+ Reply to Thread
Results 1 to 21 of 21

Copy certain range when a cell is not blank

Hybrid View

ell_ Copy certain range when a... 01-10-2018, 03:49 AM
Sintek Re: Copy certain range when a... 01-10-2018, 04:05 AM
ell_ Re: Copy certain range when a... 01-10-2018, 04:13 AM
jindon Re: Copy certain range when a... 01-10-2018, 04:22 AM
ell_ Re: Copy certain range when a... 01-10-2018, 05:16 AM
jindon Re: Copy certain range when a... 01-10-2018, 05:27 AM
Sintek Re: Copy certain range when a... 01-10-2018, 05:29 AM
ell_ Re: Copy certain range when a... 01-10-2018, 08:57 PM
Sintek Re: Copy certain range when a... 01-11-2018, 01:36 AM
ell_ Re: Copy certain range when a... 01-11-2018, 01:39 AM
Sintek Re: Copy certain range when a... 01-11-2018, 01:41 AM
ell_ Re: Copy certain range when a... 01-11-2018, 01:50 AM
Sintek Re: Copy certain range when a... 01-11-2018, 02:02 AM
ell_ Re: Copy certain range when a... 01-11-2018, 02:10 AM
Sintek Re: Copy certain range when a... 01-11-2018, 02:15 AM
ell_ Re: Copy certain range when a... 01-11-2018, 02:22 AM
Sintek Re: Copy certain range when a... 01-11-2018, 02:32 AM
ell_ Re: Copy certain range when a... 01-11-2018, 02:49 AM
Sintek Re: Copy certain range when a... 01-11-2018, 02:59 AM
ell_ Re: Copy certain range when a... 01-11-2018, 03:03 AM
Sintek Re: Copy certain range when a... 01-11-2018, 03:04 AM
  1. #1
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Question Copy certain range when a cell is not blank

    Hi, all

    I have this data where it shows the parameters with unit as well as the monthly data and average. My issue is I would like to copy paste column "parameters", "unit" and "average", only if "average" has some values. The copied data will be pasted in the same sheet. May I know how to start? I have tried to search for a solution but it's still a bit blurry to me.

    Hereby I attached an Excel file that almost matches to my actual file. Your help will be really appreciated. Thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Give this a go...
    Option Explicit
    
    Sub Copy()
    Dim r As Long, lr As Long, rng As Range
    With Sheet1.Cells(1).CurrentRegion
        .AutoFilter 16, "<>"
        r = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(12).Row
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = Union(.Range("B" & r & ":C" & lr), .Range("P" & r & ":P" & lr))
        rng.Copy
        Sheet1.AutoFilterMode = False
        .Range("R4").PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Hi, Sintek

    Thank you so much for this! I'm currently altering this code to suit my actual file hence, can I know what is meant by SpecialCells (12)? What is meant by 12?

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy certain range when a cell is not blank

    ell_
    Your sheet layout is just made for AdvancedFilter.
    Sub test()
        With Sheets("sheet1")
            .[v2].Formula = "=p2<>"""""
            .Cells(1).CurrentRegion.AdvancedFilter 2, .[v1:v2], .[extract]
            .[v2].Clear
        End With
    End Sub
    P.S.
    You need to delete extra space at the end in P1 "Avg " to match with T3 "Avg".
    Last edited by jindon; 01-10-2018 at 04:35 AM.

  5. #5
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Hi, Jindon

    I got an error - Application-defined or object-defined error, may I know why?

    P/s: I have deleted the extra space

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy certain range when a cell is not blank

    Named range plays funny sometimes.
    Try this one then
    Sub test()
        With Sheets("sheet1")
            .[v2].Formula = "=p2<>"""""
            .Cells(1).CurrentRegion.AdvancedFilter 2, .[v1:v2], .[r3:t3]
            .[v2].Clear
        End With
    End Sub

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    what is meant by SpecialCells (12)
    visible cells after filter

  8. #8
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Hi, Sintek

    Currently trying your code but it doesn't work as what I expected when I played with it. Here's my code:

    Option Explicit
    
    Sub Copy()
    Dim r As Long, lr As Long, rng As Range
    With ActiveWorkbook.Worksheets("Summary").Cells(1).CurrentRegion
        .AutoFilter 16, "<>"
        r = .Range("B9", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(12).Row
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rng = Union(.Range("B" & r & ":C" & lr), .Range("AI" & r & ":AI" & lr))
        rng.Copy
        Worksheets("Summary").AutoFilterMode = False
        .Range("AL33").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    End Sub
    The blank data in column AI are still copied to AL33. May I know why?

    Capture.PNG

    To Jindon, thank you so much for this! I might be able to use it to my other sheets.
    Last edited by ell_; 01-10-2018 at 09:01 PM.

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    .AutoFilter 16, "<>"
    The 16 above is for column P, You are now using Column AI which is Col # 35

    Upload a sample of your new setup...
    Last edited by Sintek; 01-11-2018 at 01:40 AM.

  10. #10
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Hi, Sintek

    I have changed 16 to 35 but the same thing happens. Is it probably due to the reason that my data starts at row 9?

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Upload a sample...Can't say until I see...

  12. #12
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Sintek,

    Here is the edited Excel file which is similar to the actual file. The top rows (1-8) have some other data which will be ignored in this case. Thank you so much!

    P/s: I'm also hiding the columns in between because I only want to see column B, C and AI in the actual file
    Attached Files Attached Files

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    The top rows (1-8) have some other data
    Quite important to know which columns this data occupies...Otherwise this snippet of code won't work
    .Cells(1).CurrentRegion
    Edit....
    Not to worry...This workaround...
    Option Explicit
    
    Sub Copy()
    Dim r As Long, lr As Long, rng As Range
    With Sheet1
        With .Range("B9:AI" & Cells(Rows.Count, "B").End(xlUp).Row)
            .AutoFilter 34, "<>"
        End With
        r = .Range("B10", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(12).Row
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rng = Union(.Range("B" & r & ":C" & lr), .Range("AI" & r & ":AI" & lr))
        rng.Copy
        .AutoFilterMode = False
        .Range("AL33").PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    End Sub
    Last edited by Sintek; 01-11-2018 at 02:09 AM.

  14. #14
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Hi Sintek,

    Here's the updated Excel. In actual file, the parameters will increase or decrease, number of rows occupied will change from time to time. But the summarized table in AL33 will not change its position. Hope that clarifies. Thanks!
    Attached Files Attached Files

  15. #15
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Do you now see how important actual worksheet setup is in order for us to assist...All 3 your uploads are different and hence the code has to be adapted to suite...
    Option Explicit
    
    Sub Copy()
    Dim r As Long, lr As Long, rng As Range
    Application.ScreenUpdating = False
    With Sheet1
        With .Range("B8:AI" & Cells(Rows.Count, "B").End(xlUp).Row)
            .AutoFilter 34, "<>"
        End With
        r = .Range("B9", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(12).Row
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        Set rng = Union(.Range("B" & r & ":C" & lr), .Range("AI" & r & ":AI" & lr))
        rng.Copy
        .AutoFilterMode = False
        .Range("AL33").PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 01-11-2018 at 02:32 AM.

  16. #16
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Thank you, Sintek! And my apology for the trouble.

    May I know if this code be capable of filtering up to 50++ rows? Just in case if the data increases in the future.

  17. #17
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Yep, filtering is always better than looping...See change in code Post #15 - red part - to speed up.

  18. #18
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Thank you so much, Sintek! And sorry again for the trouble :/

    Works like a charm.

    Another curiosity is, what if I want to apply this code to do the same thing, but pasting it to different sheet? And in this case, I want to copy all the columns from B to AI?

    I know I should change:

    With Worksheets("Summary") 'change Summary to the sheet's name
    Set rng = Union(.Range("B" & r & ":C" & lr), .Range("AI" & r & ":AI" & lr)) 'this has to change too but not really sure how since I want to copy the whole B to AI
    .Range("AL33").PasteSpecial xlPasteValues 'this also has to change by defining sheet's name and position of my table
    Right?

  19. #19
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Set rng = .Range("B" & r & ":AI" & lr)
    Copy code remains the same...

  20. #20
    Forum Contributor
    Join Date
    09-19-2017
    Location
    London
    MS-Off Ver
    2015
    Posts
    169

    Re: Copy certain range when a cell is not blank

    Thank you so much, Sintek!

    Definitely a life saver.

  21. #21
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy certain range when a cell is not blank

    Glad I could help. Thanks for rep +

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Vba code to copy into first blank cell within a range
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-26-2017, 03:56 PM
  2. [SOLVED] VBA to copy a1 to the next blank cell in a range U18:U23
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-15-2016, 02:59 PM
  3. VBA macro to copy last non blank cell on a certain range that has formula
    By danwoltrs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2016, 03:58 PM
  4. [SOLVED] Copy Cell Adjacent to Blank Cell in Range.
    By NewExcelUser in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-20-2015, 07:45 PM
  5. [SOLVED] Copy / Paste range from one sheet to another, if cell in column A is not blank
    By Tapyr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2014, 09:18 AM
  6. macro to copy changing range to next blank cell in another sheet
    By d2whales in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-09-2013, 12:55 PM
  7. [SOLVED] Copy data from a range of cells into a blank range based on common cell
    By vanmeterkj in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-03-2012, 10:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1