Ask Experts Questions for FREE Help !
Ask
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #1

    Mar 11, 2009, 12:46 PM
    Retrive data from another spreadsheet
    Hello

    I have a macro that retrives data into a template. But I need to have the macro retrieve from the other file no matter what the name is. Ok lets say I have opened spreadsheet named 10971. The macro opens the blank template file and populates correctly. This works good. But I want to have the macro open the template and populate the template from the opened file no matter what the name is! I see on my macro, I see a line like so:
    Windows("10971.XLS").Activate
    course the macro doesn't work properly unless the one spreadsheet is named 10971! So can I have a variable that retrieves the data from the file no matter the spreadsheet name?
    Thanks
    Robert
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

    Mar 11, 2009, 12:49 PM

    You can use a cell reference instead of the hardcoded filename, then populate it with sheet you want to access.
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #3

    Mar 11, 2009, 12:59 PM

    Hi Scott,
    Are you saying I can put a cell reference in the macro? The original file always is new , as it is an import from another system, but the location of the data I want is always the same. For example, I always need cells :
    B1, B4, C4, D4 C8 from the original file
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #4

    Mar 11, 2009, 01:36 PM

    I believe you can use:

    Windows(Sheet1:A1).Activate

    Or something similar.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #5

    Mar 12, 2009, 12:19 AM

    There are many attributes you can utilize in your macro to have it determine what the current File Name is.

    ThisWorkbook.Name
    Result - 10971.xls

    ThisWorkbook.FullName
    Result - C:\ExcelDocs\TempFiles\10971.xls

    You can experiment with this in the part of your code where you want the particular book to be activated.

    So maybe:

    Code:
    Windows(ThisWorkbook.Name).Activate
    Keep in mind that when working with templates, attributes like this can only be referenced when your new doc based on the template has been saved at least once, thus creating all these attributes.
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #6

    Mar 12, 2009, 07:26 AM

    Hi JB,
    The way I interpreted this is he is opening a template and wants to pull data into the template from an external file that may change each time. In that case he needs to use a variable where he can enter the filename.

    Can you think of another way (maybe an InputBox Like Access has) where he can be prompted for the filename when the macro runs?
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #7

    Mar 12, 2009, 07:33 AM

    Hi ScottGem, that is exactly the case. I download the spreadsheet , which always has a unique name. I open it run the macro, it opens a template and populates the template.Actaully thinking to tell the user, to save the template as the unique file name. But first I want my macro o run no matter what the file name is. I have thought about having my macro , just save all the data from the unique file, close the file, paste the data into the template and them cut, copy and paste it from there.What do you think?
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #8

    Mar 12, 2009, 07:46 AM

    If the macro works when you hard code the filename, I see no reason why you can't type the actual filename in and reference it that way. At the least, you could just rename the downloaded file.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #9

    Mar 12, 2009, 12:17 PM

    I download the spreadsheet , which always has a unique name...
    I open it...
    ... run the macro...
    ... it opens a template and populates the template.
    Based on that order of work process above, I think the variables as I've suggested should do the trick. Used that way, regardless of what the current randomly downloaded filename is, the macro would look it up for itself and use the ThisWorkbook.Name in the appropriate place.

    This approach should make even knowing the exact filename irrelevant and an input box also unnecessary.
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #10

    Mar 12, 2009, 01:23 PM

    Hi Jb
    Look right?

    Sub MyMacro()

    Dim Thisworkbook.name As Worksheet
    Set Thisworkbook.name = ActiveSheet

    Windows(ThisWorkbook.Name).Activate
    Range("C7").Select
    Selection.Copy
    Workbooks.Add Template:= _
    "C:\Documents and Settings\rsernowski\Application Data\Microsoft\Templates\NBCN Trade Advice.xltx"
    Windows("NBCN Trade Advice1").Activate
    Range("E4").Select
    Windows(ThisWorkbook.Name).Activate
    Range("D7").Select
    Selection.Copy
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #11

    Mar 12, 2009, 02:03 PM

    No, ThisWorkbook.Name is a built in function, you don't have to declare it. You could declare a substitute to make your formula look nice, but it's not necessary. This is untested edits to your code:
    Code:
    Sub MyMacro()
    Windows(ThisWorkbook.Name).Activate
    
    Range("C7").Copy
        Workbooks.Add Template:= _
            "C:\Documents and Settings\rsernowski\Application Data\Microsoft\Templates\NBCN Trade Advice.xltx"
    Windows("NBCN Trade Advice1").Activate
    Range("E4").Select
    Windows(ThisWorkbook.Name).Activate
    Range("D7").Copy
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #12

    Mar 12, 2009, 02:23 PM
    Hmm, I'm having trouble with that. Perhaps declaring a variable will simplify it:
    Code:
    Sub MyMacro()
    Dim MyBook As Worksheet
    Set MyBook = ActiveSheet
    Windows(MyBook).Activate
    
        Range("C7").Copy
        Workbooks.Add Template:= _
            "C:\Documents and Settings\rsernowski\Application Data\Microsoft\Templates\NBCN Trade Advice.xltx"
    
    Windows("NBCN Trade Advice1").Activate
    Range("E4").Select
    
    Windows(MyBook).Activate
    Range("D7").Copy
    End Sub
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #13

    Mar 13, 2009, 05:39 AM

    Hey Jb

    Thanks for your help. The macro is stopping on the line:
    Windows(MyBook).Activate
    There is only the 3 lines above it! I open up the spreadsheet, in this case it is called 10972 and run the macro - and the macro stops with a dialogue box
    Run time error #13
    Type mismatch
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #14

    Mar 13, 2009, 08:35 AM

    Well, let's see if that variable is even being registered properly. Put this line of code above that one and you should get a popup message with the workbook name right before the macro fails in that spot, so at least we know.
    Code:
    MsgBox "The Workbook name is " & MyBook.Name
    BTW, I've never used the Windows(MyBook).Activate syntax, but it does not error out on my system, and Intellisense seems to approve it, too.
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #15

    Mar 13, 2009, 08:47 AM

    Done
    It returns:
    The workbook name is sheet1
    rsernowski's Avatar
    rsernowski Posts: 13, Reputation: 1
    New Member
     
    #16

    Mar 13, 2009, 08:53 AM
    Hi JB,

    Maybe I am doing this all wrong. I have 10 cells I require to copy from the "MyBook" spreadsheet. Perhaps I should declare the variable and set it. Close the worksheet , open the template and paste each variable.? If you think it is better just give one sample and I can duplicate it for the other cells. One cell from the open book will be c7 to paste into e4 on the template

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Collect data into 2 combo boxes from data in an access table [ 1 Answers ]

I have two combo boxes. When the form loads the db connection opens a recordset that selects all the data from the retaillocations table. When you choose the region (0-4), that number loads into a text box and the Contacts load into the forst combo box. Now I want to select only the Name based on...

How to retrive data from database in phased manner using jsp [ 1 Answers ]

I want to show data(that are retrived from database) in phased maner in the web browser.I want to say that I have supoose 12 records present in database.first only 4 records are to be shown,then when pressing a "next" button next 4 records to be shown in web browser and so on.

Can we retrive WAC number? [ 3 Answers ]

My H1B application was approved 7 years before , I have forgot my WAC number. Is there any chance of reteriving WAC number.

How to retrive the deleted Emails. [ 1 Answers ]

Hi I have deleted some Emails from my inbox (Gmail). I deleted from trash box also. I want to retrieve the deleted mails from gmail server. Please any body explain me about this query. Thanks & Regards Dhanunjaya D

Can you retrive buddy's from an AOL buddylist? [ 1 Answers ]

Hello I need help if atall possible.. I've managed to delete my whole AOL buddylist profile with some important contacts.. (which I shouldn't of done) Is there anywhere atall on my PC maybe the regedit I don't know... where these will be in a cache still.. Thanks for any help atall J


View more questions Search