+ Reply to Thread
Results 1 to 4 of 4

switching between workbooks

  1. #1
    Registered User
    Join Date
    06-13-2004
    Posts
    8

    Unhappy switching between workbooks

    Hey All,

    Can someone please help with my problem as outlined below?

    A macro "1" is opened from workbook "A"
    macro "1" formats workbook "A" and then it opens workbook "B"
    macro "1" then calls macro "2" which formats workbook "B" (the active workbook) and copies a selected range.

    How can I then paste this selected range back into workbook "A"?

    The problem is that when I save and close workbook "B" the copied range isnt available for the subsequently active workbook "A" to paste into. (I tried the PutInClipboard method but I cannot get it to work)

    Is there a way to achieve this without resorting to passing variables (ie filenames) between the two macros or merging the two macros together?

    Thanks

    macro "2"
    'highlight cells to be copied
    Cells.Select
    Selection.Find(What:="As of Date", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    ActiveCell.Offset(1, 0).Select
    LastRow = Range([C1], ActiveSheet.UsedRange).Rows.Count
    Range(Cells(7, 3), Cells(LastRow, 3).End(xlToRight)).Select
    Selection.Copy

    'sub ends and then returns to the calling sub ie macro "1"

    macro "1"
    'Close workbook "2"
    ActiveWorkbook.Save
    ActiveWorkbook.Close False

    Range("A2").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Hi i have added the line at the bottom of your code
    Please Login or Register  to view this content.
    I was able to just ACtivate the workbook because your Macro1 "close workbook" doesn't close the workbook it just saves it, however if you did close it you would have to use the Workbooks("Workbook1").Open but you would have to set the path if its the same path as workbook2 set it like With ThisWorkBook.Path......

    Regards,
    Simon

  3. #3
    Registered User
    Join Date
    06-13-2004
    Posts
    8
    Hi Simon,

    Thanks for the response.

    As I mentioned I was trying to avoid this approach because the workbook name changes. Is there another way of achieving this?

    Thanks

  4. #4
    Registered User
    Join Date
    07-05-2006
    Posts
    16
    Quote Originally Posted by dabith
    Hi Simon,

    Thanks for the response.

    As I mentioned I was trying to avoid this approach because the workbook name changes. Is there another way of achieving this?

    Thanks
    Well depending on how many workbooks you have you could always use the workbooks(#) thingy to get the desired result.

    For instance if you have only the 2 work books open you could do the flowing

    i = 1
    strCurrentBook = activeworkbook.name
    do until i > workbooks.count

    if workbooks(i).name <> strCurentBook then

    exit do

    end if

    i = i +1
    loop

    workbooks(i).activate

    and then whatever the code is for paste

+ 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