+ Reply to Thread
Results 1 to 14 of 14

Looping macro

  1. #1
    Registered User
    Join Date
    01-14-2010
    Location
    Corpus Christi, Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Looping macro

    How do I write a macro that will delete every other line on a spreadsheet? I can create a macro that delete one line using the Record a macro command but what do I ad at the end of the macro to keep it going until stopped with Ctrl Break?

    Help.. Thank you.

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Looping macro

    hello,

    are you saying that you want to delete every 2nd line eg line 1,3,5,7. and manually stop the macro using ctrl+pause/break keybord keys.

    1, the macro will run very fast, even programming a little pause between each row deletion, you will eventually run into not hitting the keys fast enough and deleting a row you wanted to keep. and deleting the rows via VBA doesnot realy allow you to undo the changes like you may need.

    2, you could use a simple userform to input the start row(1st row you want to delete) and the end row(last row you want to delete, aswell as how many rows you want to skip between each deletion.

    3. another way would be to record a macro assiging a shot-cut key. go into the vba editor and delete the code you just recorded and replace it with
    Please Login or Register  to view this content.
    and now all you have to do is select a cell within the row that you want to delete and hit the shot-cut key.

    cheers


  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Looping macro

    Hi mbenazeraf
    a quick search and you'll find an answer
    http://www.excelforum.com/excel-gene...-65-lines.html
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Registered User
    Join Date
    01-14-2010
    Location
    Corpus Christi, Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping macro

    D_Rennie, I'm not sure I understand. What I have is row 1 to 850. I want to delete row 2, 4, 6, etc all the way to end. There is nothing after line 850, so even if the macro continue running after, I can still break it. I also understand that after deleting row 2, what was row 4 is now row 3, so using the command "down" instead of the row number should do the trick. Can this macro be writen using Create a macro and if so, please tell how.

    Thanks.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Looping macro

    Hi mbenazeraf
    if you click on the link you'll find this code which you can modify to suit your needs
    Please Login or Register  to view this content.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Looping macro

    This little macro will check column A and determine on its own how far down the data belongs, then it will create a range of every "even" row in the range and delete them all at once.
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  7. #7
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Looping macro

    JB Thats a clean way to do things, i like it.
    i would have done like pike
    Last edited by D_Rennie; 01-17-2010 at 04:25 AM.

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Looping macro

    I'll have to have a platya round wth that syntax for future use

  9. #9
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Looping macro

    WARNING THREAD HIJACK

    i been picking a bit up of JB latley i used one of your approaches the other day.

    PHP Code: 
    With .Sheets("TestVBA")
                .
    Range(.Cells(21), .Cells(LR1)).FormulaR1C1 _
                    
    "=IF(Sheet1!RC="""","""",IF(OR(Sheet1!RC[1]=1,Sheet1!RC[2]=1),Sheet1!RC,""""))"
                
    .Columns(1).SpecialCells(xlCellTypeFormulas1).EntireRow.Copy ThisWorkbook.Sheets(2).Range("A1")
                .
    Columns(1).ClearContents
            End With 
    seamed to work well. then mikerickson and another decided to school me all over again.
    live and learn


  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Looping macro

    Please Login or Register  to view this content.
    Any help to you?

  11. #11
    Registered User
    Join Date
    01-14-2010
    Location
    Corpus Christi, Texas
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping macro

    Thanks all. I will try Pike's and JB's suggestions and will return to the post.
    Great board. This my first try on it.
    Question: Why is it when I print this page, the macros under the heading "code" are printed in light grey color? Very hard to read. Any suggestion how to improve that?
    Thanks.
    Last edited by mbenazeraf; 01-17-2010 at 12:22 PM. Reason: Typo

  12. #12
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Looping macro

    hi Mbenazeraf,

    Welcome to the Forum

    That's a good question. The Forum design has recently been over-hauled & further comments have been requested, so I have pasted your question as post number 84 in the below thread:
    http://www.excelforum.com/exceltip-c...e-board-6.html

    Edit: Nice one JB
    (btw, Somewhere (...?) I've seen a thread discussing that unions can become slow after the joining of 300-400 (I think?) areas & someone (perhaps DO or KeepItCool?) suggested flushing the range after x unions using an outer loop to help keep the speed up. However, with 850 being mentioned as the lastrow, flushing the range doesn't seem very necessary at all.)

    hth
    Rob
    Last edited by broro183; 01-17-2010 at 02:08 PM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Looping macro

    I ran it with 8000 values and it seemed to still be acceptably brief...the OP will have to decide if this is a win for him.

    ==========

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

  14. #14
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Looping macro

    JB, you're more dedicated than me!
    I've just made an unreferenced comment & haven't done any testing to support the comment.

    I agree, it's up to the OP & I imagine they'll be happy - hopefully they give some feedback...

    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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