+ Reply to Thread
Results 1 to 6 of 6

Accessing multiple workbooks through a macro

  1. #1
    Registered User
    Join Date
    06-29-2005
    Posts
    46

    Accessing multiple workbooks through a macro

    Hi guys,

    Just so that you know in advance, I am a beginner at writing macros. In fact, this is the first macro I've tried to write. Basically I have two workbooks and I would like to access both of them through a macro that I plan on running on one of the workbooks. I'm not even sure where to look, and I know the first workbook can be accessed through ThisWorkbook, but I'm not sure how to get to the other one. Can anyone help? Thanks in advance.

    PS - Both workbooks are open in Excel and if possible I would like to be able to access a cell selection I have made in the second workbook, but it is not imperative.

    Raman
    Last edited by Raman325; 06-29-2005 at 11:02 AM.

  2. #2
    Registered User
    Join Date
    06-29-2005
    Location
    England
    Posts
    50
    The easiest way to see how to do things is to use the macro recorder. This will generate a macro for you with all the relevant properties and methods.

    You can then use this as a base to write fully functional code that accepts parameters and all the other bells and whistles you can put in yourself (A good bookm on VBA would help).

    To access the recorder, use Tools->Macro->Record New Macro. To access the Vb editor to see the monster you've created use Tools->Macro->VB Editor.

    Regards

    Rich

  3. #3
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    suppose what a.xls is the workbook which has the macro, and b.xls is the workbook you want to access, there is two ways to code depending on b.xls file is opened or it is not opened

    if b.xls is opened

    then you can access by using:
    workbooks("b.xls").activate
    then you can directly go into the worksheets
    worksheets("Sheet1").select
    T=RANGE("A2").VALUE

    if b.xls is not opened

    then you have to open it to access the workbook, you can open it using macro code, let say that b.xls is in c:\

    then
    Workbooks.Open Filename:="c:\b.xls" 'opens the file
    and then follow the previous code
    workbooks("b.xls").activate
    then you can directly go into the worksheets
    worksheets("Sheet1").select
    T=RANGE("A2").VALUE

    you can save and close the workbook by using
    workbooks("b.xls").save
    workbooks("b.xls").close

  4. #4
    Registered User
    Join Date
    06-29-2005
    Posts
    46
    Thanks, that fixed it. One more question, sorry. In one of the workbooks, I have multiple sheets with the format String-1, String-2, String-3, etc. I want to access them each in turn through a for loop. Can I simply have a counter and access the worksheet by doing Worksheets("String-" + counter).Select?

  5. #5
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    yes you can use

    for counter=1 to 3
    Worksheets("String-" & counter).Select
    'more code
    next

  6. #6
    Registered User
    Join Date
    06-29-2005
    Posts
    46
    Great, thanks. I may have a couple of other questions, and I'll post them here if I can't find the answer myself

+ 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