+ Reply to Thread
Results 1 to 25 of 25

delete blank rows in new Workbook with VBA

  1. #1
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    delete blank rows in new Workbook with VBA

    Hi all, this is driving me completely crazy, i'm far from a vba expert, hope you can help.

    So I have a workbook open with the following code behind a form control button.
    It's supposed to create,name & save a new workbook and copy a range to it from a sheet (with a frozen pane) called MOM no named wsI in the code (stands for method of manufacture), then remove any blank rows.
    I cant get it to remove the blank rows, i get a variation of error messages based around subscript out of range. I mention the frozen panes because It did briefly in a flaky way when i played around with freezing/unfreezing panes, but not robust enough to rely on, then it stopped.
    It seems that when i create & save the new workbook (wbo) it is the active workbook as it is ontop of the original & is in focus but if i add a message box to return name of active workbook it shows (wbi). But whatever i do to try to make the right sheet active at the right time it doesn't work.

    I don't mind going about it an entirely different way if anyone can suggest, all i want is a new workbook with the blank rows removed.

    Many thanks in advance

    VBA Code:
    Please Login or Register  to view this content.
    Last edited by QuantumSquirrel; 04-24-2020 at 10:27 AM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: delete blank rows in new Workbook with VBA

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: delete blank rows in new Workbook with VBA

    First things first:

    Welcome to the forum.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Note how is :C represented in the code not in the tags.


    As for merit:

    Have you tried fully qualified references like wbo.wso.range … etc?
    And doesn't
    Please Login or Register  to view this content.
    help?
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    oops! sorry about the newby tags error - now rectified


    nope, the blow does not work, have tried the below and other permutations. Really stuck now!
    wbo.activate
    Sheets("Sheet1").activate

    If the code is good, Could there be a setting in excel i need to edit?

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: delete blank rows in new Workbook with VBA

    Please attach a copy of your file as suggested in Post #2.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: delete blank rows in new Workbook with VBA

    See Mumps1 suggestion.

    As for better ways than delete row by row see such mod:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    sample file attached
    Attached Files Attached Files

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: delete blank rows in new Workbook with VBA

    Replace this code:
    Please Login or Register  to view this content.
    with this:
    Please Login or Register  to view this content.

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

    Re: delete blank rows in new Workbook with VBA

    Please Login or Register  to view this content.
    Last edited by Sintek; 04-24-2020 at 10:56 AM.
    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!!!

  10. #10
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    Ok now i'm confused.
    I created this sample file i knocked up to post here it did not work on my pc. I posted it downloaded it and it works fine ?????
    I put the same code in my original workbook (range references edited) and it still does not ?????

    anyone? pretty please with sugar on?

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

    Re: delete blank rows in new Workbook with VBA

    Why not just copy the range excl blanks rows to start with...Perhaps a more suitable sample file upload of actual setup and then sample file of result required?

  12. #12
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: delete blank rows in new Workbook with VBA

    As for posts #8 and #9 - I think both the codes will delete rows with not empty rows, but just column A empty and some values in columns B:M.

    Am I wrong?

    As for #10 - healing properties of Excelforum.com server

    May be: create new file, copy content of old one sheet by sheet, and module by module to a new one, save and reopen.

  13. #13
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    sintek that works - thankyou

    however it would be nice to remove the filter afterwards
    i've tried either:-

    Sheet1.Range("A:M").AutoFilter Field:=4
    or
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False
    or
    On Error Resume Next
    Sheet1.ShowAllData
    On Error GoTo 0

    but none work

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

    Re: delete blank rows in new Workbook with VBA

    Hey QuantumSquirrel

    Please take a moment to read our forum rules...here
    Your post does not comply with Rule # 2
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
    Please Login or Register  to view this content.
    So...Edit your post...Highlight the code and press the # button



    Post 8 also works...
    Please Login or Register  to view this content.
    Clears filter

  15. #15
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    sintek
    i've already put tags round it?

  16. #16
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    ah! if i remove .autofilter from the code that works

    thanks all

  17. #17
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: delete blank rows in new Workbook with VBA

    the code tags are missing in post #13

  18. #18
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    it appears not
    either way the filter code removes the first row. I've tried tinkering but to no avail.
    Any ideas please?

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

    Re: delete blank rows in new Workbook with VBA

    Quote Originally Posted by QuantumSquirrel View Post
    sintek
    i've already put tags round it?
    Refer...Post 13

    either way the filter code removes the first row
    we have no idea what you are doing...We do not have your file...
    Please Login or Register  to view this content.
    Last edited by Sintek; 04-24-2020 at 12:17 PM.

  20. #20
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    Hi Kaper,

    I see, i didnt think i needed to put tags around single snippets of code in the middle of explanatory text. I will do that from now if thats the convention.

    BTW
    I didnt get the below to work:
    Please Login or Register  to view this content.
    and the autofilter method suggested works but deletes the top row.
    Any ideas please

  21. #21
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    Hi All,

    I do apologise for making you think i was expecting you to read my mind.
    I am working off the file i attached earlier using the code:-
    Please Login or Register  to view this content.
    to filter out the blank rows.
    I have added a row into the copy range now so that it can just be removed by the vba and all is good. except that i cant turn the filter off for some reason?

  22. #22
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    so just to be clear,
    im using the code:-
    Please Login or Register  to view this content.
    with the attached file test.xlsm

    can anyone please tell me how to turn the filter off programatically in the worksheet created?

    thanks in advance
    QS
    Attached Files Attached Files

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

    Re: delete blank rows in new Workbook with VBA

    Please Login or Register  to view this content.
    Last edited by Sintek; 04-24-2020 at 01:27 PM.

  24. #24
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    thank you but that gives me a runtime error 1004

    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    04-24-2020
    Location
    Stourbridge, England
    MS-Off Ver
    365
    Posts
    29

    Re: delete blank rows in new Workbook with VBA

    sintek that works thank you
    Please Login or Register  to view this content.
    thought i'd tried it but maybe in the wrong place ?
    anyway many thanks

+ 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. Replies: 1
    Last Post: 07-31-2019, 01:32 PM
  2. [SOLVED] Delete blank rows between blocks of data except two blank rows
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-26-2018, 11:18 AM
  3. How do i delete blank rows from every worksheet in a workbook?
    By naomip in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2017, 06:47 AM
  4. Using code to delete rows with blank cells in them upon opening the workbook
    By marielouise1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2014, 02:00 PM
  5. Replies: 3
    Last Post: 07-17-2013, 01:25 AM
  6. [SOLVED] How to Delete Multiple Consecutive Blank Rows - Delete all Blank Rows
    By raw_geek in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-16-2012, 03:17 PM
  7. [SOLVED] Delete blank rows after filter - if no blank rows exit sub
    By cmb80 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-27-2012, 10:00 AM

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