+ Reply to Thread
Results 1 to 23 of 23

Optimized macro to run more efficiently

  1. #1
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Optimized macro to run more efficiently

    Any help is appreciated!!!!
    I would like to make this run faster and more efficiently. It goes on to 16 more sheets with different cells and columns.

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 03-12-2020 at 09:06 AM. Reason: Please use code tags!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Optimized macro to run more efficiently

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). Please see the yellow banner at the top of this page.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    I would like to simplify the macro. I takes to long to run. I thought there might be a different way to do this. I want to enter data on the entry sheet, then run the macro to copy the date to the other sheets.
    Attached Files Attached Files

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    Hi Justin,

    Did you mean to have the Entry Sheet words in column A match exactly the worksheet names? You have a space between "Location" and "1" in column A but not on the worksheet names. What does your read data look like?
    Last edited by MarvinP; 03-12-2020 at 11:06 AM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    That may just be a typo, the most important thing is the macro. The sheet i have uploaded is a very small sample. the full length code takes almost a two min to run.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    The words in column A don't match the worksheet tab names in your example. The macro I'm writing is using the words in Col A to match the sheet names. Do they match in the real data?

  7. #7
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    No, All sheet names, ranges, and offsets will change to fit my current workbook.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    Quote Originally Posted by Justin2010 View Post
    No, All sheet names, ranges, and offsets will change to fit my current workbook.
    That wasn't the question. Will the words in Column A be the exact spelling of the other worksheet names?

    Also is there only 1 row of data below a location? Could you have many numbers following a location before the next location in column A?

  9. #9
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    Sorry, I didn't quite understand your question. Yes they will be the exact spelling. There are one to five rows below the location.(I can move all the data to one row). There can be up to 20 numbers after column A if I move it to 1 row.( currently the data is in 4 or 5 columns wide and 1 to 5 rows below. The sheet this is going on is being designed for a tablet to ease the navigation of the workbook. So i want to minimize horizontal scrolling.

  10. #10
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Optimized macro to run more efficiently

    There are a few ways to do this, mine looks like this:
    Please Login or Register  to view this content.
    Cheers
    Erwin
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Optimized macro to run more efficiently

    Besides the sheet names not matching, the columns in Location 1 don't match the columns in Location 2. Have a look at the attached file. I have changed the sheet names so they match the values in column A of ENTRY SHEET and I have changed the columns in Location 2 to match Location 1. The macro is in Module1.
    Attached Files Attached Files

  12. #12
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    Ok Justin,

    We are getting closer to a better question and what you have/want. The spelling of the Locations will EXACTLY match the SHEET names. If you have multiple rows of numbers under a location (In Col A) do you expect each to show up on the Location sheet. This would mean you have many rows with the same date. The question is do we OVERWRITE the data that is already on the location sheet or add a new row with that same date?

    You should improve your sample file with multiple rows of data under a location row and post it again, to give us a better chance at solving your problem. I'll stop writing code until you give a better example file.

    I believe with your above (new) arrangement of data the code from Eastw00d may not work.

  13. #13
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    The columns wont be the same on every sheet!

  14. #14
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    Hi Justin and welcome to the forum (I forgot that in my first post)

    We are supposed to help you solve a problem. I'd start over with your data entry sheet. I'd make it only 4 or 5 columns wide. See the attached example. If you made your data entry in a better format it would be much more effective to deal with and get answers from. The way you have it now doesn't let Excel use it's power of tables of data.

    I've rearranged your data into a 5 column table and done a Pivot Table from my suggestion. It would be more productive to start over with my suggestion and tell us what your major objectives are.

    Hope this helps.
    We really do want to help but your current data entry sheet won't get you where I think you want to go.

    Marv

  15. #15
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    I have updated only the entry sheet to resemble my current sheet. I have added a second location2 row just to show how I could move my data to one row. I have not made the changes on the location1 and location2 sheet
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    I have 17 sheets that have different columns. I like your code, but is there any way to do it without the columns matching?

  17. #17
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    Marv, I can rearrange the entry sheet to whatever works, what seems to be the problem is that each one of my Sheets has a different column arrangement. And I am not seeing an attachment in your last post!?

  18. #18
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Optimized macro to run more efficiently

    Hi Justin,

    This is a better example of what your data might look like. BUT - once again I think you should input your data like I suggested in my last post. Excel works in rows and columns of similar type data. I'd put a date in A1 and in row 3 starting at "A3" I'd have :
    Sheet Name , Column Name, Value/Text,

    What you have on your entry sheet is a "Crosstab Table" with multiple tables and data for each.

  19. #19
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11

    Re: Optimized macro to run more efficiently

    Thank you Erwin!! Your code seems to to the trick. I don't understand most of the code is it possible for you to break this part down for me so i know what it does?
    For i = 1 To 6
    Select Case i
    Case 3, 4, 5
    ws1.Cells(r, i + 1).Offset(0, 1) = .Cells(3, i + 1)
    Case 6
    ws1.Cells(r, i + 1).Offset(0, 2) = .Cells(3, i + 1)
    Case Else
    ws1.Cells(r, i + 1) = .Cells(3, i + 1)

  20. #20
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Optimized macro to run more efficiently

    Hi Justin,
    well of course you can learn the best if you "walk" in VBE with "F8" through every line, while executing your macro.
    Basically I say in this Case statement:
    I have 6 cells to copy, A Cell, is a composite of a row and a column. hence the for statement.
    So when I have searched for the rownumber with the find-function, I use this row r to fill the cells in the Location1 sheet, here ws1,
    with each item out of the ws worksheet, by saying ... = .cells(3, i+1) , i.e. cell from row 3 and column i+1 in the Entry Sheet.
    For the first two items it's no problem, they will be put in the case else part, that is all i-numbers that are not mentioned in the other cases.
    Since there is a gap between- B and D - I have to offset the subsequent output with 1 , hence the line "case 3, 4, 5" where the columnumber will be accordingly 4, 5 and 6, i.e. D E and F .
    And the same for the last i, again an offset extra.

    I hope this is making a bit sense for you.
    Cheers
    Erwin
    PS maybe this is nicer to read:
    Please Login or Register  to view this content.
    Last edited by Eastw00d; 03-12-2020 at 03:51 PM. Reason: typo

  21. #21
    Registered User
    Join Date
    03-12-2020
    Location
    USA
    MS-Off Ver
    365
    Posts
    11
    Thank you Erwin!!. I have one more question before i start incorporating your code into my workbook. Comparing your code to my code in my first post, Do you think the code will execute faster a without so much processor usage?

  22. #22
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Optimized macro to run more efficiently

    I have never done this before, as I was always convinced that structured coding is faster, but I liked the challenge
    I did some timing and my code works about 3 times faster than the original!
    On top of that, if you omit the ScreenUpdating part it will go 5 times faster. In this particular case it's slowing down.
    But you can see for yourself if you add this code in the beginning and at the end of the macro:
    Please Login or Register  to view this content.
    Cheers
    Erwin

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Optimized macro to run more efficiently

    If you want to allocate the data according to the instructions given on Entry Sheet then try
    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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] Excel Macro needs to be optimized (speed up the execution)
    By kevivu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-13-2015, 06:20 AM
  2. Macro to run formulas more efficiently
    By Ale84 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-16-2015, 06:55 PM
  3. Curious about a more optimized method
    By cman0 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-24-2014, 07:40 PM
  4. [SOLVED] Macro to more efficiently copy/paste every 21 rows
    By Phil_Packer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-23-2013, 03:08 AM
  5. [SOLVED] Can this macro be written more efficiently?
    By alpinesd in forum Excel General
    Replies: 4
    Last Post: 06-16-2012, 02:46 AM
  6. Working Code: Can it be optimized?
    By qcity in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 06-17-2011, 01:00 AM
  7. Help to get Macro operating efficiently
    By mr_teacher in forum Excel General
    Replies: 4
    Last Post: 09-01-2006, 04:13 PM

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