+ Reply to Thread
Results 1 to 10 of 10

Macro to insert lines based on cell content

  1. #1
    Registered User
    Join Date
    07-06-2012
    Location
    Bedford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Question Macro to insert lines based on cell content

    Hello,

    I am new here and having real difficulty with something. I am doing my masters dissertation and have huge amounts of data. I'm finding myself repeating the same action again and again, and if I continue like this I will never complete my project on time! My project is looking at hostage-taking and kidnapping incidents, and my unit of analysis is the individual. However, for many incidents, more than 1 hostage is taken, and so I need to have a seperate line for each hostage.

    What I need to do is insert extra rows depending on how many hostages were taken. So far my lines of data look like this, with the long number being the incident number, and the small number being the number of hostages:

    200807160018 2
    200807270038 2
    200808210016 12
    200809220014 128
    200810250007 17
    200811260015 2

    I'm not very good at programming I'm afraid... is there a simple way around this?! I really appreciate any help that you can give me on this.
    Thanks in advance...

    MirTraynor

  2. #2
    Forum Contributor
    Join Date
    04-12-2012
    Location
    MD, England
    MS-Off Ver
    Excel 2003
    Posts
    142

    Re: Macro to insert lines based on cell content

    @MirTraynor

    Welcome to the forum. This should work. Insert into a standard module.

    Please Login or Register  to view this content.
    hth

    gmk



    Click the * below the post to say thanks and remember to mark the thread as solved if answered satisfactorily.

  3. #3
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Add an option for you

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-06-2012
    Location
    Bedford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to insert lines based on cell content

    Thanks for that guys. I haven't used these before... I'm used to just recording macros. Will these work for any row/column/cell number?

  5. #5
    Registered User
    Join Date
    07-06-2012
    Location
    Bedford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to insert lines based on cell content

    Neither have worked, it is saying 'run time error' and the fault seems to be with the lines:

    NumFrom = CLng(Mid(.Value, InStr(.Value, " ") + 1, 9))

    and

    nNumOfRows = Right(.Cells(nDataRow, "A"), Len(.Cells(nDataRow, "A")) - InStr(.Cells(nDataRow, "A"), " ")) ''' get rows to insert

    Any ideas? To clarify, the large number and the small number are in seperate columns.

    Thanks again,

    Mirtraynor

  6. #6
    Forum Contributor
    Join Date
    04-12-2012
    Location
    MD, England
    MS-Off Ver
    Excel 2003
    Posts
    142

    Re: Macro to insert lines based on cell content

    @MirTraynor

    To clarify, the large number and the small number are in seperate columns.
    That would have been helpful to know from the start.

    I have amended the code to suit 2 columns. To see what happens change your data references starting at C17 and the row numbers to insert starting at D17. Just make sure sure the 2 lots of data are aligned by row then experiment by changing the row and column numbers where indicated.

    Please Login or Register  to view this content.
    hth

    gmk



    Click the * below the post to say thanks and remember to mark the thread as solved if answered satisfactorily.
    Last edited by gmk; 07-07-2012 at 01:35 PM.

  7. #7
    Registered User
    Join Date
    07-06-2012
    Location
    Bedford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to insert lines based on cell content

    Ah, apologies gmk. I should have been clearer.
    I will try this one and see how I go.
    Thanks for your help.

    MirTraynor

  8. #8
    Registered User
    Join Date
    07-06-2012
    Location
    Bedford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Macro to insert lines based on cell content

    gmk... This worked a charm, and has made my life a whole lot easier!

    Thanks very much!

  9. #9
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Macro to insert lines based on cell content

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  10. #10
    Forum Contributor
    Join Date
    04-12-2012
    Location
    MD, England
    MS-Off Ver
    Excel 2003
    Posts
    142

    Re: Macro to insert lines based on cell content

    @MirTraynor

    Good to hear.

    made my life a whole lot easier
    That's what forums are for.

+ 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