+ Reply to Thread
Results 1 to 6 of 6

Add Multiple Rows to Differing Variables

  1. #1
    Registered User
    Join Date
    05-14-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Add Multiple Rows to Differing Variables

    Good Morning,

    I am trying get a VBA code that will add multiple rows given the amount of weeks between a date. I have attached an example spreadsheet with the original data that I have, and also what I require.

    Basically if a row has six weeks between a loan of an item, I need 5 rows inserted with the start date and end date of each week between the loan period, with the rest of the cells remaining the same. The next loan is then say 5 weeks, so I need 4 lines inserted with the start week and and week changed. I guess the support ID in the example is the constat that will differentiate each loan period.

    This is required so I can graph the qty of the loans over a period i.e monthly, 3 monthly or my choice of period. I am hoping the example is self explanatory, if not, I can try and clarify more if you have questions.

    Kind Regards

    Matthew
    Attached Files Attached Files

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Add Multiple Rows to Differing Variables

    Go to the VBA IDE (alt-F11), insert a new module, and paste in this code:

    Please Login or Register  to view this content.
    You can call it with a command button or assign it to a ctrl-key combo. It starts at the bottom of the sheet, looking for entries with start and end dates. When it finds them, it calculates how many new rows are needed, inserts them, then copy/paste the source row to them. I'm not quite sure what you want to do with the entries at the bottom that don't have start and end dates, and you won't be able to run the macro more than once over the same data set.

    The undated entries could be moved to a location lower in the sheet, if you want.

  3. #3
    Registered User
    Join Date
    05-14-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Add Multiple Rows to Differing Variables

    Wallyeye,

    Thank you for your code. It was my mistake in the attached sheet, those entries with no dates should have been deleted. This is almost what I need, I am now trying to play with the code to insert the number of weeks, instead of months. I assume this will be just changing the code to look at colums L and M, instead of N and O??? I think.

    I think once I get the right number of lines inserted, I will manually put in the start and end dates for the week, instead of trying to code it in.

    Eg: date range is 07/05/12 till 21//05/12, this is two weeks, so I need two lines inserted. with the 1st inserted line date to be 14/05/12 and second line being 21/05/12. If you get my drift

    Cheers though for the code, great start to what I require.

  4. #4
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Add Multiple Rows to Differing Variables

    Actually, all you need to do is change the intMonths calculation to an intWeeks calc:


    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-14-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Add Multiple Rows to Differing Variables

    Thanks,

    I am getting a runtime error on this lin

    intWeeks = Int((arrValues(lngCurrRow, 13) - arrValues(lngCurrRow, 12)) / 7) - 1

    Cant seem to get passed it

  6. #6
    Registered User
    Join Date
    05-14-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Add Multiple Rows to Differing Variables

    Quote Originally Posted by mattyh77 View Post
    Thanks,

    I am getting a runtime error on this lin

    intWeeks = Int((arrValues(lngCurrRow, 13) - arrValues(lngCurrRow, 12)) / 7) - 1

    Cant seem to get passed it

    I've finally managed to figure it out.

    Cheers for your help

+ 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