+ Reply to Thread
Results 1 to 18 of 18

Trying to write a Loop to perform calculations within blank rows between populated rows

  1. #1
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Trying to write a Loop to perform calculations within blank rows between populated rows

    Good morning/afternoon/evening all. I hope someone is up for (what I think is) a challenge as I've reached the end of my meager expertise!

    I'm using Excel 2010 with the workbook in question saved as an Excel 97-2003 Workbook in Compatibilty Mode.

    I've been able to piece together a macro that searches Sheet1 (please see the attached workbook) for specific elements in column B then transfers that individual row to Sheet2. I've then sorted both Sheets 1 & 2 by column A, added labels and placed Sheet2's cursor in cell C5 (as per the attached workbook). Looking at Sheet 2 row 5 in the example, I now need to write a Loop statement that will sum columns C, F & G for each unique Rep Name in column A placing those resulting values in C5, F5 & G5, calculate D5 as (F5 * C5) and calculate E5 as (G5 * C5). I then need to copy the name "Emp1" into cell A5, add a label ("Combined Average" or something TBD) to cell B5, move down to the next blank row beneath the next unique Rep Name & repeat the process. Once all calculations are complete, I need to copy each resulting row back into Sheet1 beneath the corresponding unique Rep Name.

    The information in and length of Sheet1 will vary from day to day, so the elements being transferred to Sheet2 will never be identical (the Supervisors will be pasting new information into Sheet1 cell A1 every day). Due to that I'm not able to complete Sheet2 with static cell references. The number of blank rows between unique Rep Names as you see in the example can be altered if necessary; I thought having a blank row above & beneath each unique Rep Name might make it easier to perform the calculations.

    I don't believe the macro as it exists needs to be added here as it performs flawlessly (so far!). My sincere hope is that someone can assist with the loop statement as I've outlined. If you would like to see the code as it exists now, I'll be happy to add it at your request.

    I look forward to any assistance! Thank you for reading my post.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    let us take a look at the macro, your needs can be in bedded in it

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,707

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    See if this works for the first part of your rquirement
    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Another:

    Please Login or Register  to view this content.
    I was working with Sheet2 in your example?

  5. #5
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Thanks for looking at this, rcm. Here's the macro as it exists. I've heavily noted it for myself as I don't understand vbs code for Excel as well as I should:
    Please Login or Register  to view this content.
    Last edited by ObliviousAmI; 01-03-2014 at 12:56 PM.

  6. #6
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Good morning, John H Davis. I tried your code today, and the first loop (for column C) worked like a gem, then I found I needed to modify your second and third loops as the cells in columns F & G contain formulas instead of numeric values. Once I had done that, those loops worked beautifully, too. I found I also had to slightly modify your code in the final line of the last loop ("rcell.Value = rcell.Offset(-1).Value & " Combined Average") as my request hadn't been altogether clear. I needed to have the unique Rep Name from column A brought down to the first blank row (which your code does), but then I needed to have the next label placed in column B rather than concatenating the name & "Combined Average" both in column A, so I added "rcell.Offset(, 1).Value = "Combined Average"" which did exactly what I needed.

    My final question is this: How do I move columns A:E of each resultant row back to Sheet1 into the blank space that I've already placed beneath each unique Rep Name? Some of the cells (column C) contain formulas rather than numeric values, so I need to take that into consideration when the rows are transferred.

    Thanks again for the help, John. It was outstanding! I'm studying your code today to learn more from it so I don't have to ask so many questions! 'Preciate it. If you or anyone can help with this last bit, I'll be grateful.
    Last edited by ObliviousAmI; 01-03-2014 at 02:14 PM.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Not sure about the changes you made for Columns F & G so you'll have to reedit this for those changes.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Thank you for your prompt reply, John! Unfortunately, I find that when I add that additional code it throws run-time error '91': Object variable or With block variable not set. The error is on the line "ws.Activate". Here's the entire code with the edits I had added. One other edit I hadn't mentioned earlier. The values in columns D & E in your loop beneath the line "NoData:" were supposed to have been the result in column F divided by the result in column C rather than the product of those two columns, so I also had to edit your code in the final loop for both lines that start "rcell.Offset(, N).Value..." Here's my code:
    Please Login or Register  to view this content.
    I hope I'm being clear for you. Thank you again for your attention!

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    I modified your code Try:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    That works like a charm!! Thank you SO very much John, I appreciate your help and expertise! I'll be marking this "Solved" in just a moment!

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    You're welcome. Glad to help out and thanks for the feedback.

  12. #12
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Good afternoon/evening/morning, all. After John Davis' invaluable help last week with my macro, I find one additional situation has cropped up: In the final steps of the macro, each unique employee's newly calculated row is transferred from sheet2 to the blank row beneath that unique employee's name on sheet1. The situation I'm seeing is that the first unique employee's name at the top of sheet2 is added to the bottom of sheet1 to row 500. Here's the code in question:

    Please Login or Register  to view this content.
    Is it possible to change the range where I've commented "Here's where the calculated row is transferred from Sheet2 to Sheet1" so that it only searches to the last occupied cell in columnA & doesn't go farther?

    I'm sorry if I haven't explained this sufficiently. Hope y'all can understand this.

    I appreciate any response.

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Maybe:

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    That works absolutely PERFECTLY! Once again, my thanks to you, John! You've proven yourself invaluable again! I was confident that's what needed to be done, but I just didn't know how to write the code for that line. My sincere gratitude to you for all your efforts!

  15. #15
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Again welcome. And Glad to hear it works for you.

  16. #16
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    Good Morning/Afternoon/Evening, all. Unfortunately for me, I'm back with another brows furrowed, scratching my head question. It seems that the code that John H Davis so kindly helped me with is displaying a hiccup that I didn't notice until after the spreadsheet had been in use for a few days. In the final steps of John's solution for me, the row that was calculated on Sheet2 for each unique rep name is copied and placed back on Sheet1 into the blank row that was created beneath that unique rep's name. What one of the Supervisors here discovered is that the very last unique rep's calculated row isn't copied back to Sheet1; everyone's is except for that very last rep. The following is the code block where the calculation occurs on Sheet2 for each unique rep, labels are added and the row is copied back to Sheet1:

    Please Login or Register  to view this content.
    As in the past on this thread, I hope I've sufficiently explained the situation. If not, please share any questions you might have. If anyone would care to help, I'd sincerely appreciate it. Make it a great day!

  17. #17
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    This was set for a range to 500. I'm not certain that I understand your problem but try making this change.

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    12-31-2013
    Location
    Sioux Falls, SD
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trying to write a Loop to perform calculations within blank rows between populated row

    John, again, thanks for your rapid reply. Unfortunately, changing that line of code didn't make a difference. So I thought outside the box and decided that, rather than changing the For loop, since the affected row is always the final row on Sheet2, I would copy that row and paste it to the first empty row in Sheet1. Here's the code snippet that I used (it might be somewhat clumsy as I assembled it from various sources I found while researching this-you would likely write something far more elegant):

    Please Login or Register  to view this content.
    I just added this after the end of the For loop you wrote for me (above) and found my problem resolved! Thanks for your efforts; they're much appreciated.
    Last edited by ObliviousAmI; 01-15-2014 at 07:16 PM.

+ 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. How to perform 'insert rows' instead of 'copy rows' using .copyrecordset (excel vba)
    By praful k in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2013, 06:06 AM
  2. [SOLVED] Perform calculations for multiple rows based on specific column text
    By justinmirsky in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 03-31-2013, 08:20 PM
  3. loop through rows for match insert blank rows with title
    By reeyu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 07:34 AM
  4. Deleting blank rows using loop and SpecialCells
    By mcraewhite in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-03-2008, 05:42 PM
  5. Deleting blank rows using loop and SpecialCells
    By SOS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2008, 09:40 AM

Tags for this Thread

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