+ Reply to Thread
Results 1 to 15 of 15

Loop through worksheet and insert row after a change in a cells value

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Loop through worksheet and insert row after a change in a cells value

    Hello everyone. I have a problem that I have not been able to resolve. I have a series of spreadsheets. They all have the same column headers. In particlar column 8 contains account numbers. I would like to loop though each worksheet and after each new account number insert a specific number of rows, say 5 of them.

    I have piece mealed some code that can be seen below. It sort of works but 1.) It fails to loop through each worksheet and 2.) Does not properly insert rows after each new account number.

    Note I do not want to run this macro o the first three worksheets of the work book. Only starting with worksheet 4 and onwards.

    Please Login or Register  to view this content.
    Last edited by AnthonyWB; 07-27-2011 at 02:32 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    Hi AnthonyWB, I have revised your code a bit. Let me know how it goes.
    Please Login or Register  to view this content.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    Thanks,


    I slightly modified the code. Please see the error I am recieving:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    Hi AnthonyWb, it seems to be throwing an error because when your code inserts formulas, the values are removed. Also, I can get this to work with one iteration but not a second because of deleting columns. Plus, I don't know what's supposed to be going on with the data you are trying to manipulate.

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    Perhaps what I was trying to do through code is simply not working.

    How would you go abut the task?

    For example in Column F I have account numbers

    a
    a
    b
    b
    b
    c
    c
    c

    I would like to insert a row whenever the acount number changes

    a
    a

    b
    b
    b

    c
    c
    c

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    Hi AnthonyWB, try the following code and let me know if it meets your requirements:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    Again thank you. Please see the sample output below. It alsmost works but not quite:

    72363I
    72363I

    115230I

    74267I
    74267I

    111548I

    111550I

    115540I

    130960I

    64598I

    111430I
    111430I

    111866I

    112294I

    112296I

    113092I

    113094I

    113666I

    113736I

    113750I

    113928I

    121716I

    114354I

    114356I

    115392I

    115508I

    115644I
    115644I

    116216I

    130068I

    130960I

    131200I
    131200I

    113570I
    113570I

    120524I
    120524I

    120526I
    120526I

    121012I
    121012I

    128730I <------Problems start
    128896I
    128896I
    130014I
    130014I
    130874I
    130876I
    139836I
    139836I
    120148I
    120148I
    74235I
    74577I
    74577I
    74345I
    111866I
    120246I
    120246I
    74637I

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    I just ran the code and didn't have a problem, it all turned out like:

    72363I
    72363I

    115230I

    74267I
    74267I

    111548I

    111550I

    130960I

    64598I

    111430I
    111430I

    111866I

    112294I

    112296I

    113092I

    113094I

    113666I

    113736I

    113750I

    113928I

    121716I

    114354I

    114356I

    115392I

    115508I

    115644I
    115644I

    116216I

    130068I

    130960I

    131200I
    131200I

    113570I
    113570I

    120524I
    120524I

    120526I
    120526I

    121012I
    121012I

    128730I

    128896I
    128896I

    130014I
    130014I

    130874I

    130876I

    139836I
    139836I

    120148I
    120148I

    74235I

    74577I
    74577I

    74345I

    111866I

    120246I
    120246I

    74637I

  9. #9
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    wow interesting

    And on one of the spreadsheets my data shows up as:

    111096I
    111096I
    111426I
    111426I
    113590I
    113590I
    117094I
    117094I
    120070I
    120070I


    133048I
    133048I

    141076I
    141076I

    141090I
    141090I

    120752I
    120752I

    120754I

  10. #10
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    I wonder if it is because I had the range wrong, try:
    Please Login or Register  to view this content.

  11. #11
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    I'm at home now on a different computer and the code worked like a charm. Eg:

    11
    11

    12

    13

    14
    14
    14

    15
    15

    16

    17

    18
    18
    18
    18
    18

    19
    19

    20
    20

    21

    22
    22

    23

    24
    24
    24

    25
    25
    25

    26

    27
    27
    27

    28
    28

    29

    30
    30
    30
    30
    30
    30

    31
    31
    31
    31

    32

    33
    33
    33
    33
    33
    33
    33

    333

    34

    35

    36

    37

    38

    39

    40
    40
    40

  12. #12
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through worksheet and insert row after a change in a cells value

    Give it one more try and if it still doesn't work for you then it may be best if you upload a mock file.

  13. #13
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    Perhaps the problem is that in some instances there MAY exist a blank row in between account numbers. For example

    a
    a
    a
    b
    b

    c
    c

    d
    d
    d
    d
    e
    e
    e

    It's close but slightly off.

  14. #14
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    yeah I "F8" through the code. the problem is when the code encounters a black space.

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Loop through worksheet and insert row after a change in a cells value

    here is the corrected version

    Please Login or Register  to view this content.
    Now i need to do this by looping through the spreadsheets. Any suggestions

+ 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