+ Reply to Thread
Results 1 to 12 of 12

Recording macro using cell references in worksheet

  1. #1
    Registered User
    Join Date
    01-18-2007
    Posts
    16

    Recording macro using cell references in worksheet

    Hi, I'm hoping someone brilliant can help. I did a search and didn't see anything along these lines that had already been discussed.

    I am working on this project that involves opening several Excel spreadsheets and copying the data into a new workbook. The problem is that the names of the various worksheets I want to copy change every day - it always reflects the date. I open 7 workbooks, go to the sheet named (for example)01.17.07, copy a static range and then paste it to a new workbook.

    Is it possible to record a macro and use a cell reference in the destination workbook (the one I want to copy to) to tell it which worksheet to copy? So, for example, I would type 01.17.07 in cell A1 of the new workbook and the macro would look for the sheet named 01.17.07 when running? Then tomorrow, I could type 01.18.07 and it would know to look for a different sheet?

    I'd appreciate any guidance. Sorry if this is confusing.

    -sara

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

    If I understand correctly you are after a simple variable ...
    Say cell A1 = Today()
    In your macro a variable called mydate can be set as follows
    mydate = Range("A1").Value

    As a consequence, not only you do not have to type the date, but the value can be used in your code ...
    By the way, if you have access to the code which saves and generates the workbooks you are working on, you will find the very same principle is used to create the workbook name ...
    HTH
    Carim


    Top Excel Links

  3. #3
    Registered User
    Join Date
    01-18-2007
    Posts
    16
    That makes sense, but can you show me an example of how the code will look when selecting the sheet? Right now, it is a static reference to a sheet called "Previous":

    Sheets("Previous").Select

    Would I replace "Previous" with "mydate"?

    There are times when the date I want to select is not the previous day, therefore typing in the date I am working on is necessary. I assume it will still work if I type the date in A1 instead of using the Today function, correct?

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

    To make your sheetname a variable ...
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-18-2007
    Posts
    16
    Thanks! That's exactly what I needed.

    Seems so simple now...


  6. #6
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Glad it fixed your problem ...

    Thanks for the feedback

  7. #7
    Registered User
    Join Date
    01-18-2007
    Posts
    16
    Can I also use this to insert a variable into a string?

    I have this as part of my macro:

    ActiveCell.FormulaR1C1 = _
    "='C:\Documents and Settings\My Documents\[Workbook.xls]Worksheet'!R125C3"

    I want the worksheet name to change based on a particular cell, but when I define the variable and insert it, it returns the variable name in place of the worksheet name. I figured it had something to do with the fact that I'm editing a formula in the macro (replacing a text "=" with a regular "=" so that the cell will return the result of the formula instead of text), but I have no idea how to fix it.

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

    Yes you can ... do not know how you typed the instruction ...
    use concatenation...i.e "adding strings" ... e.g.
    =val1 & val2

  9. #9
    Registered User
    Join Date
    01-18-2007
    Posts
    16
    If I use a concatenation, it doesn't return the result of the formula, only the formula itself.

    Basically, I did a concatenation in Excel to build the reference I want. I want cells to go to a particular worksheet (named based on a date, so it changes depending on what day I'm working on) to lookup a value. So if I'm working on January 12, I want it to go to sheet 01.12.07 and return a specific cell.

    I then took that concatenation and pasted values in the cells where I want the values to display. Obviously, it's pasted as text and the only way to display the formula result is to delete the starting equals sign, replace it and hit enter.

    I thought I might be able to write a macro that would do the deleting and replacing for me, but when I do, it builds this code into the macro:

    Please Login or Register  to view this content.
    I tried creating a variable called MyDate and inserting it to replace the "01.12.07"

    Please Login or Register  to view this content.
    But when I do that, it doesn't actually insert the value in D1, it pastes it as text and I do not get a formula result - just a formula text.

    So anyway, maybe this is just a really complicated way to do something simple, but it's been a long day...

    You've been such a huge help already.

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

    Give a try to following ...
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-18-2007
    Posts
    16
    Nevermind. Your concatenation did work. I must have made a mistake somewhere when I tested it.

    Thanks again for all your help.

  12. #12
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Glad you fixed your problem

    Thanks for the feedback

+ 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