+ Reply to Thread
Results 1 to 9 of 9

Building a macro with a wildcard

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    Tulsa, Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    Building a macro with a wildcard

    Every week I copy this table out of OneNote and paste it into five or six different excel books to get the totals from all of the pages in each book. How do I put this formula into a macro?

    Labor =sum('*'!m48)
    Equipment =sum('*'!m55)
    Materials =sum('*'!m62)
    Total =sum('*'!m65)
    Hours =sum('*'!k74)



    Labor, Equipment, Materials, Total, & Materials are text in column 1 and the formulas are text in column 2. The cells referenced stay the same each week.

    With my limited knowledge of macros I was only able to record the process of pasting the table. It worked great until I attempted to use it later. The macro only pasted what was currently on the clipboard, so when I had other data on the clipboard, the whole thing was a colossal failure!

    I have tried to learn about the syntax required to build the macro in VBA, but I really suck at it!

    Any help at all would be greatly appreciated!!!

    Thanks,
    Karen

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

    Re: Building a macro with a wildcard

    Interesting, I had never used the asterisk like this before. It looks like it sums all the sheets other than itself, when entered in the Excel interface, but it translates it to the sheet names when you enter it. You can simulate this with a bit of VBA, assuming you are putting the formula on the first worksheet:

    Please Login or Register  to view this content.
    I tried putting the asterisk in the formula, Excel didn't like that. This first finds the name of the second and last worksheets, then embeds them in the formula.
    Last edited by Cutter; 07-17-2012 at 08:48 PM. Reason: Added code tags

  3. #3
    Registered User
    Join Date
    08-01-2011
    Location
    Tulsa, Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Building a macro with a wildcard

    I thought it might help to attach a sample of one of the weekly books I make. This particular job only had four sheets, but some jobs have as many as 40 sheets per week. I insert a new worksheet at the end and paste my previously stated formula into cell B4 and yes it does translate it to the sheet names of the current book. This particular book is for job 12106, but I have many jobs, ie. 11034 and 11186.

    I made a genuine effort to paste the code into a new module and "let 'er run!" Colossal failure x 2! My VBA skills leave A LOT to be desired!!!

    Can you tell me how I messed up with using the code? Your help is GREATLY appreciated!!
    Attached Files Attached Files
    Last edited by Cutter; 07-23-2012 at 02:32 PM. Reason: Removed whole post quote

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

    Re: Building a macro with a wildcard

    So, tweaking it to add a Summary worksheet a the end of the workbook, and adding an apostrophe to enclose worksheet names with spaces:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-01-2011
    Location
    Tulsa, Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Building a macro with a wildcard

    SQUEALS with delight! It's ALIVE!

    The only problem I have now is that I have to go into VBA each time I want to use it and move the module from my personal.xlsb into the book I am working in, essentially duplicating the number of steps I previously used when copying and pasting wildcard formula from OneNote.

    What did I do wrong?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Building a macro with a wildcard

    Hello vanmeterkj ,

    The problem lies with the using the qualifier ThisWorkbook. When this encountered in your Personal.xlsb, VBA interprets this to mean you are referencing the Personal.xlsb workbook. Changing the qualifier from ThisWorkbook to ActiveWorkbook should fix the problem. The change has been made to the code below.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: Building a macro with a wildcard

    Hmmm... I thought I had changed this to worksheets(1). Incorporating Leith's comments:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-01-2011
    Location
    Tulsa, Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Building a macro with a wildcard

    I suspected it was because of the reference to 'ThisWorksheet', but I didn't know what to do about it. Where can I learn the rules for writing code? How do you know what terminology to use and the order that it is used?

    Thank you so much for all your help!!

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

    Re: Building a macro with a wildcard

    It is all about the Object Model. All of the MS applications, and many others, have a data model that represent their application and documents. After that it is just a bit of VBA and logic to tie it together. OK, a lot of VBA and logic. There are quite a few sites around with tutorials, I've learned a lot just participating in forums like this one, both reading and posting solutions.

+ 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