+ Reply to Thread
Results 1 to 14 of 14

Variables in macros?

  1. #1
    Registered User
    Join Date
    11-01-2006
    Posts
    8

    Variables in macros?

    Is it possible to use variables inside a macro?

    I have created different macros:
    1. copy and rename worksheets within the workbook.
    2. change multiple cells to new dynamic link paths (pulling data from certain cells in a different excel project altogether).

    what I am in dire need of is the ability to write the macro so it does this:
    - copys current sheet to end of list of sheets.
    - renames new sheet.
    - applies macro2 (from above) to the new sheet.
    - rinse and repeat.

    i will have a total of 128 sheets (when done) and i will have to update at least 7 columns with 12 rows in each column (roughly 10,752 changes... way to many to do by hand).

    as reference, the path to the resources i am pulling from is as such:
    "='c:\Folder\[Jan Report]Manger1'!R8C2" (or B8 on the Manager1 sheet in the Jan Report.xls file)
    i am needing the 'Jan Report' and 'Manager1' to be variables. the R8C2 would be the same cell for each month. and it would change to R9C2 for the next sheet in the workbook (the one that macro1 created).

    have i totally lost everyone? proabably. just let me know if you need more info and i will try to clear it up. again, i am mainly needing to know if (and how) to get variables inside a macro.

    -Nym

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Yes VBA will accept variables ...
    My recommendation is to go step by step ...

    Before looping through 128 worksheets,
    Turn on the macro recorder
    proceed with your usual steps with one worksheet
    stop macro recorder
    analyse how VBA has translated your actions in code ...

    Then will come the time of introducing variables, looping, etc ...

    HTH
    Carim

  3. #3
    Registered User
    Join Date
    11-01-2006
    Posts
    8
    Thanks Carim, been there and done that.
    I happen to not be just any office nerd, I am a geek at heart and trade (office is just not my stong point), I know how to code in varias languages (VBA is NOT one of them, unfortunately).

    Warning Lots and Lots of ugly code:
    Please Login or Register  to view this content.
    Thats for just one column. i have copies and pasted that 6 more times (making one ugly batch of code) and chaning the R8C2 to R8C3,C4,C5,C6,C7,C9.
    The only things that i have to change is the Name of the Manager (David Keys) and the Row/Column reference. the name of the excel doc changes as well (01,02,03,04....12).

    Hope this helps a little bit more.
    -Nym

  4. #4
    Forum Contributor
    Join Date
    07-13-2006
    Posts
    400
    you can just use concatenate

    so for your example
    "='c:\Folder\[Jan Report]Manger1'!"&B8.value
    should give you what you need
    Last edited by MDubbelboer; 11-29-2006 at 02:39 PM.
    --Mark

    Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?

  5. #5
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi again,

    Below is a sample to simplify your code ...
    Please Login or Register  to view this content.
    HTH
    Carim
    Last edited by Carim; 11-30-2006 at 04:16 AM.

  6. #6
    Registered User
    Join Date
    11-01-2006
    Posts
    8
    Thanks Carim
    um, wouldne there be a proplem when 'i' hit 10?
    would it not concatenate as:
    ...thly\[PM-M 010-07.xls]Dav...
    since Str2 = "[PM-M 0" & i (or 10,11,12) & Str3 = "-07.xls]"

    just double checking.. havent tried it yet, just picking it apart so that I can understand what it is doing.

    -Nym

    EDIT:
    just tested it and yep, Rows 10,11 and 12 are broken. I am trying to noodle on a way to fix this, in any event thanks a ton Carim.
    Last edited by Nymphadora; 11-29-2006 at 03:29 PM.

  7. #7
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Nym,

    You are absolutely right ... !!!

    That is what happens when code is not tested ... I am bad ...!!!

    Carim

  8. #8
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Nym,

    Code adjusted ...

    Please Login or Register  to view this content.
    As you have noticed, you are now in a position to simplify all your code even further by building loops with all your strings ...if need be ...

    HTH
    Carim

  9. #9
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Nym,

    Do you need absolute references addresses ...?
    Had not noticed you also needed the row number as a variable ..

    Please Login or Register  to view this content.
    HTH
    Carim

  10. #10
    Registered User
    Join Date
    11-01-2006
    Posts
    8
    building loops he says.... lol
    I was venturing out and building these said loops when lo and behold, I had no clue what I was doing. I thought I had set it up right, when WHAM! infinate loop.
    HOLY CRAP!!!
    so, um, just how do i set that loop up so that i dont have to kill the EXCEL process in task manager again. (i did it 3 times then gave up on looping).

  11. #11
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Nym,

    There are many different ways to create loops ...
    Just give me an example of what you trying to accomplish ...
    If I can I will give you a hand ...or a hint ....

    HTH
    Carim

  12. #12
    Registered User
    Join Date
    11-01-2006
    Posts
    8
    Ok, i will try to explain, as i am sure it is all a mess. :D

    as i am visual person, my quasy-excel doc:
    http://smilkduds.googlepages.com/SAN...stingMacro.xls
    and the remote doc i am pulling data from:
    http://smilkduds.googlepages.com/PM-M01-07.xls

    The column varabile will change a number of times per work sheet. the row varable will change once per sheet.

    In this doc, Column B(and C) pull from Column B from the remote document.
    D pulls from C,
    E from D,
    F from E,
    G from F,
    H from G,
    I is used for a percentage formula,
    J from I on the remote doc.

    Every sheet will pull from the same columns, only the row reference with change (go up by 1 [8-9-10-11...])

    Hope that helps.
    ultimatly i want the macro to:
    1. Copy the current worksheet to the end of the list of worksheets.
    2. Change name of the worksheet to that of a certain property.
    3. Change cell A2 to the same name as the worksheet.
    4. Select cell B6.
    5. Run the macro (or automatically edit) the one I am currently trying to get working.
    6. Start over, but give this new worksheet a different name.
    7. Continue doing this over and over for all 128 properties that I have.

    -Nym

  13. #13
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Nym,

    Thanks for the links ... it does help to understand your business requirement ...

    Attached is a slightly revised template with an amended version of Autofill macro ...

    A couple of questions remain unclear for me :
    1. With 9 Managers and 128 properties, don't you need an "assignment table" linking them ... a "who-does-what" kind of table ... ?
    2. The logic behind the row increment 8, 9, etc ... (which by the way is not yet coded ) does not appear logical ... since template summarizes months whereas Managers sheets deal with properties ...

    HTH
    Carim
    Attached Files Attached Files

  14. #14
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Have you already sorted out everything ...?

    Carim
    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)

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