+ Reply to Thread
Results 1 to 6 of 6

Need to activate an open workbook

  1. #1
    Registered User
    Join Date
    07-25-2008
    Location
    Bend, Oregon
    Posts
    39

    Need to activate an open workbook

    I have some code that copies the data on one spreadsheet and pastes it into another. The code knows the names of both workbooks and it currently uses the names in the code to activate the workbooks and copy and paste the data.

    I want to have only two workbooks open and have the code copy the data from whatever other workbook is open regardless of the workbook name. Any ideas?

    The workbook being copied from always has only one spreadsheet in it and the data range is always the same but the workbook name and spreadsheet name are always different.

    Thanks

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Assign a workbook variable (say, "wkb") to the workbook of interest when it's opened. Then you can reference that workbook.

    ThisWorkbook refers to the workbook where the code is running.

  3. #3
    Registered User
    Join Date
    07-25-2008
    Location
    Bend, Oregon
    Posts
    39
    Thanks for the help but your going to have to be a little more explicit for me to understand what you are telling me. I have only a very basic understanding of VB. But thanks again.

    I am trying some code I got from another post but am having trouble with a reference in it, keeps erroring. Here is the code:

    Option Explicit
    Sub grabData()
    Dim SourceWb As Workbook

    Set SourceWb = ActiveWorkbook

    SourceWb.UsedRange.Copy ThisWorkbook.Sheets.Cells(1, 1)
    SourceWb.Close False
    End Sub

    Keeps erroring on the line: SourceWb.UsedRange.Copy ThisWorkbook.Sheets.Cells(1, 1)

    I read a lot in VB help but as I said I'm a beginner so?

    Thanks again for the help

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Please read the Forum Rules and then wrap your code with Code Tags.

  5. #5
    Registered User
    Join Date
    07-25-2008
    Location
    Bend, Oregon
    Posts
    39
    Sorry for the code identification issue. I'm new ti this forum, hope thid is correct.

    I am trying some code I got from another post but am having trouble with a reference in it, keeps erroring. Here is the code:


    Please Login or Register  to view this content.
    Keeps erroring on the line: SourceWb.UsedRange.Copy ThisWorkbook.Sheets.Cells(1, 1)

    I read a lot in VB help but as I said I'm a beginner so?

    Thanks again for the help

  6. #6
    Registered User
    Join Date
    07-25-2008
    Location
    Bend, Oregon
    Posts
    39
    OK, so here is what I’ve come up with on my own. One sub named "WorkbookCollection" uses a for/each next statement to work through any and all open workbooks and another sub named "SliceAndDice" manipulates the spreadsheet data. It all seems to work fine.

    I need tow things, suggestions on anything I might be missing to clean this up since I'm an armature, and a suggestion on how I can keep the copy and paste procedure from copying a text box I have on my SliceAndDice spreadsheet.

    Thanks again for the help.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

+ 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