+ Reply to Thread
Results 1 to 11 of 11

Macro to autofill cells and delete some rows and columns

  1. #1
    Registered User
    Join Date
    04-18-2013
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    24

    Macro to autofill cells and delete some rows and columns

    I better start with saying that I'm an Excel rookie, so I know very little (next to nothing) about macro programming, and I only know basic formulas.

    I have a set of data with a large number of rows, and I only need the rows that has data in column C. Therefore I would like to delete all rows, where the cell in column C is empty, to make the data set smaller for the calculations in the other sheets.
    First, however, I would like to fill column L with numbers, so that it automatically fills a given cell i column L with the number in the cell above, unless that row has the value "1" in column A.

    I attached the file to make it easier to understand, as English is not my first language, so it possibly could be difficult to understand what I mean...

    This is a list of plays in american football games. The column A is the play number of every game, and in column L the first play of each game has the game #. So I want the game number to be filled into all rows of that game, but when the next game starts (column A has the value "1" again) the new game # should be filled to all the plays of that game and so on. All the text notes in column L can be overwritten, they are not used for anything in this document.
    Then I would like to delete all plays that has no penalties (column C), to make the data set smaller, as it shall be used for penalty statistics, so I only need the plays with penalties. Finally I would like it to delete columns B, I, J, K, M and N as they are not used for anything in this document.

    The macro should be able to put in a button that is placed in the "sheet1" sheet, as the whole "data" sheet is imported from a website, so if the button is placed there, it will be deleted when I get a new data set from the website, as more games are played.

    I hope that it is understandable...

    PlaylistData_W1-W17.xlsx

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    Hi K.M.,

    Be sure to keep a backup file as, because of the column deletions, you can only run it once

    (Maybe we should just hide those columns instead):

    Please Login or Register  to view this content.
    Directions for running the routine(s) just supplied

    If you haven't used macros before you'll need to go to:
    File- options - trust center -trust center settings - macro settings ,
    the second option down (disable all macros with notification)

    Then - Copy the code to the clipboard

    Open your Workbook

    Press ALT + F11 to open the Visual Basic Editor.

    Select "Module" from the Insert menu

    Type "Option Explicit" then paste the code under it

    With the cursor between Sub and End Sub press F5 (F8 to Single Step)

    OR

    Press ALT + Q to close the code window.

    Press ALT + F8 then double click on the macro name
    *Be sure to save the book with the code as Macro-Enabled

    To Hide instead:

    Please Login or Register  to view this content.
    Last edited by xladept; 09-19-2015 at 07:11 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    04-18-2013
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to autofill cells and delete some rows and columns

    Thanks a lot for your help! Can it be done so the cell filling in column L is done before the row deletion? As there is no penalties on the first play, then the line with the play number (which is what I want copied) will be deleted before we copy the game number to all the other rows. And can it be done so it refers to a sheet called "data" so I can run the macro from another sheet?

    And is there a faster way of doing the autofilling in column L? Even though it down to 544 lines after the row deletion, it takes quite some time, and if we have to do it before the row deletion to still have the game numbers, then it will take forever with around 8000 lines at the end of the season

    I tried to understand the code to see if I could move around the lines myself, but most of it is gibberish to me

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    Hi K.M.,

    This is about as quick as I can make it - here's the hide version:

    Please Login or Register  to view this content.
    * I'm surprised that you thought the other was slow - you say only 8000 rows?
    Last edited by xladept; 09-19-2015 at 07:44 PM.

  5. #5
    Registered User
    Join Date
    04-18-2013
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to autofill cells and delete some rows and columns

    Now it works more or less instantly, so much faster! I don't know why it was so slow before, but I could literally see each line being filled one by one

    It's still not the game numbers that it fills in column L though, I get a mix of the other notes (screenshot attached). Don't know why... It should be the 6-digit number in each of the play # "1" rows (103xxx)
    Playlistdata.jpg

    I'm going to bed now, so I won't reply to the thread until tomorrow or monday. I really appreciate your help, and maybe I even start to understand what the code is doing if I study it really hard

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    I just filled with whatever entry was above - I'll look into it tomorrow

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    Hi K.M.,

    Try this:

    Please Login or Register  to view this content.
    * do you want to preserve those game notes?

  8. #8
    Registered User
    Join Date
    04-18-2013
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to autofill cells and delete some rows and columns

    Perfect! Thank you so much!

    And no, the game notes are only used on the website where the data is imported from, so in the excel document I don't need them.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    You're welcome and thanks for the rep!

    I've tried to comment the code for you:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    04-18-2013
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to autofill cells and delete some rows and columns

    Awesome, then I can maybe figure out what it actually does, and learn something
    Thank you so much for your help!

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to autofill cells and delete some rows and columns

    You're welcome again!

    Part of the confusion, if any, may be because I like to reuse my variables like r and D here

+ 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] Macro to delete set columns and rows
    By tejboyd in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-29-2014, 02:09 PM
  2. Unmerge cells and delete empty rows and columns
    By Nickyh1984 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 07:08 PM
  3. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  4. Need to delete cells, not columns or rows, based on LEN=0
    By jenny_journalist in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-29-2009, 04:11 PM
  5. Delete rows with empty cells in columns B&C
    By Richard in forum Excel General
    Replies: 3
    Last Post: 03-17-2006, 08:20 PM
  6. Autofill Macro for 2 rows and 100 columns
    By donniemaclean@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2005, 10:05 AM
  7. Autofill Macro for 2 rows and 100 columns
    By donniemaclean@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-04-2005, 08:05 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