+ Reply to Thread
Results 1 to 15 of 15

Combining individual macros into one module/macro

  1. #1
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Combining individual macros into one module/macro

    Hi all,

    I've tested on the individual macros and they can run well. However, is it possible to combine both recordings and vba codes together in one Module that runs in one macro?

    Let's say for example I want to run one macro that does all the following:

    1) Macro that renames and insert new tab (Macro Recording)

    Please Login or Register  to view this content.
    2) Macro that finds and highlights the String 'ERROR' and 'UNKNOWN' in the 'Log' tab/worksheet. (Macro Recording)

    Please Login or Register  to view this content.
    3) Macro that removes unwanted columns (Macro Recording)

    Please Login or Register  to view this content.
    4) Macro that does vlookup cell across another Worksheet (VBA Code)

    5) Macro that autoranges to the last row and remove formula (VBA Code)

    6) Macro that does advance sorting (Macro Recording)

    7) Macro that removes duplicates (VBA Code)

    8) Macro that replaces numbers with texts (VBA Code)



    Is it even logical if I combine all the codes together after every End Sub? Appreciate any advice. Thanks in advance!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Combining individual macros into one module/macro

    You can Call each macro in the order you want them to run
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    Thanks davesexcel for your reply.=)

    Could you give me an example on the part
    Please Login or Register  to view this content.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Combining individual macros into one module/macro

    What Part?

    Please Login or Register  to view this content.
    Calls all the macros if first runs Macro one, once that is finished, it then runs Macro two, and so on.

    Place your macro names in the code in the order that you want them to run.

    If you want everything to run as one code, you would have to supply more information, like a sample workbook or somebody would have to spend a lot of time on this trying to decipher what you are trying to do.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining individual macros into one module/macro

    The first 2 macros combined:
    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Combining individual macros into one module/macro

    snb: that is a pretty impressive abbreviation!

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining individual macros into one module/macro

    @StephenR

    Please feel free to improve...

  8. #8
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Combining individual macros into one module/macro

    Purely space-wise, I'm saying nothing about efficiency...
    Please Login or Register  to view this content.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining individual macros into one module/macro

    @StephenR

  10. #10
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    Wow im totally amazed at how much you can eliminate so much codes and at the same time make it work!

    I'll try my best to explain each steps and codes as clearly as I can.=)
    1st Step:When i open a new excel sheet, the macro will renames 'Sheet 1 to Log', 'Sheet 2 to SE', 'Sheet 3 to SR'. Inserts new sheet name as 'Data'. This macro is a standalone because it can't link to the second macro as there are no contents in the worksheets yet.
    (Im so sorry snb for the efforts you've put in)


    2nd step: I now have all the tabs that I need to bring in the contents to each worksheets. So the next step will be to search for the string 'error' and 'unknown' in the 'Log' worksheet and highlight them.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    3rd step Now I will move on to look at the 'SE' tab. This macro is a macro recording that delimits (tick check on text, check on semicolon and check on other|) ) , removes all unwanted columns except for Col K, N, W, AJ and AM, shifts the wanted columns all way to the left and add in 2 new columns (Col F- SEC ID and Col G- SEC NO)

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    4th step: Now I will do a vlookup on cell F2 across the worksheet 'Data' and cell G2 across the worksheet 'Data' also.

    Please Login or Register  to view this content.
    5th step: Next is the macro that does an auto fill from cell F2 to the last row and cell G2 to the last row and removes the vlookup formulas.

    Please Login or Register  to view this content.
    6th step: Next is a recorded macro that does an advance sorting. Data range has a header row. Primary sort by SEC ID (Column F- Descending), secondary sort by CODE_A (Column C- Ascending), Tertiary sort by SECURITY_A (Column A- Ascending).

    Please Login or Register  to view this content.

    6th step: Next is a macro that removes all duplicates by looking at Column A.

    Please Login or Register  to view this content.
    7th step: Finally the last macro is to find and replace numbers with text in Column C.

    Please Login or Register  to view this content.
    As for the SR file. The same steps applies in the ‘SR’ worksheet only that the new columns added in will be in columns G and column H instead of Column F and G.

    I will be most appreciative if anyone can show me how to combine step 3 to step 7 first.
    Once I’ve seen a sample I will try to slowly combine the ‘SR’ worksheet together so there will be 4 individual macros which is
    1) Macro that rename and create new tab
    2) Macro that highlights the Log worksheet
    3) Macro that runs the SE worksheet
    4) Macro that runs the SR worksheet

    After that I will try to narrow down to just creating just 2 macros.
    1) Macro that rename and create new tab
    2) Macro that highlights the Log worksheet runs the SE worksheet and runs the SR worksheet.

    Meanwhile, I'll try using the codes by davesexcel to combine these codes. I hope someone out there is helping me out at the same time too. Thanks in advance!

  13. #13
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    Oops sorry about the error in typing a double entry for the '6th step' So the steps that i wish to combine is from step 3 to step 8.

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Combining individual macros into one module/macro

    I've found that keeping routines that do different things in different Subs helps when updating or editing the workbook.

    Structured Programming suggests that it would be wise to keep separate subs separate.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  15. #15
    Registered User
    Join Date
    07-08-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Combining individual macros into one module/macro

    Thanks Mikerickson for your suggestion. These worksheets however do not need any updating or editing. It is more of reading the final information in the individual worksheets after all the macro steps has been completed.

    Therefore, having a macro will defitinely not only eliminate manual work but also prevent errors from occuring when doing all these manual work.

    Btw davesexcel, is the first part of the code suppose to be like this? Im not too sure how to move on to the second part though. Help upgently needed.


    Please Login or Register  to view this content.

+ 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