+ Reply to Thread
Results 1 to 3 of 3

Activate open workbook from range in another workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-09-2012
    Location
    Isle of Man
    MS-Off Ver
    Excel 2010
    Posts
    118

    Activate open workbook from range in another workbook

    Hi Guys,

    I am working in a workbook (let's call it "Master workbook"). When I run a macro other workbooks are opened and data is collated from them and pasted into the Master Workbook. One of the workbooks that the macro opens is a workbook that is replaced daily. The title of the workbook is a date e.g. OtherWorkbook 01-06-2016.xlsx Because the name of the file changes the macro uses a date in C10 and data in cell D10 of my master workbook to enable me to open the file.

    Cell C10 has the date 01/06/2016 and cell D10 has the formula ="OtherWorkBook "&TEXT(C10,"dd-mm-yyyy")&".xlsx" resulting in D10 containing OtherWorkbook 01-06-2016.xlsx

    When in my Master Workbook the following code opens OtherWorkbook 01-06-2016.xlsx
    
    Application.Workbooks.Open ("C:\My\File\Path\" & Range("D10")) 
    
        Sheets("Sheet 1").Select
        Cells.Select
        Selection.Copy
        Windows("Master Workbook.xlsm").Activate
        Sheets("Sheet 1").Select
        Cells.Select
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
    Once the information from OtherWorkbook 01-06-2016.xlsx has been copied into my Mater Workbook I want some code to activate OtherWorkbook 01-06-2016.xlsx and close the workbook. How do I write this as VBA code when the name of the file is a date that changes i.e. the name of the file will not always be the same.

    Any help you can give me is much appreciated.

    Thanks

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Activate open workbook from range in another workbook

    What you should do is use workbook objects in your code, along the lines of this (which also shows how to not use .Select )

        Dim WB As Workbook
        Set WB = Application.Workbooks.Open("C:\My\File\Path\" & Range("D10").Value) 
    
        WB.Sheets("Sheet 1").Cells.Copy
        Workbooks("Master Workbook.xlsm").Sheets("Sheet 1").Cells.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
       WB.Close False
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Activate open workbook from range in another workbook

    Hi there,

    See if the following code does what you need:

    
    
    Option Explicit
    
    
    Sub CollectDailyData()
    
        Const sSHEETNAME_MASTER As String = "My Master Worksheet Name"
        Const sSHEETNAME_DATA   As String = "Sheet 1"
        Const sFILENAME_CELL    As String = "D10"
        Const sFILEPATH         As String = "C:\My\File\Path\"
    
        Dim sFullName           As String
        Dim wksMaster           As Worksheet
        Dim wbkDaily            As Workbook
    
        Set wksMaster = ThisWorkbook.Worksheets(sSHEETNAME_MASTER)
    
    '   Get the FileName of the daily workbook
        sFullName = sFILEPATH & wksMaster.Range(sFILENAME_CELL)
    
    '   Open the daily workbook
        On Error Resume Next
            Set wbkDaily = Application.Workbooks.Open(Filename:=sFullName)
        On Error GoTo 0
    
    '   Proceed only if the daily workbook has been successfully opened
        If Not wbkDaily Is Nothing Then
    
    '         Copy the data from the daily workbook to the ClipBoard
              wbkDaily.Worksheets(sSHEETNAME_DATA).Cells.Copy
    
    '         Paste the data from the ClipBoard to the Master workbook
              With ThisWorkbook.Worksheets(sSHEETNAME_DATA)
                  .PasteSpecial xlPasteValuesAndNumberFormats
              End With
    
    '         Before closing the daily workbook, temporarily disable Alerts to avoid
    '         the "There is a lot of data on the ClipBoard . . . " message
              Application.DisplayAlerts = False
    
                  wbkDaily.Close SaveChanges:=False
    
    '         Re-enable Alerts now that the daily workbook has been closed
              Application.DisplayAlerts = True
    
        Else: MsgBox "The workbook """ & sFullName & """ could not be opened", vbExclamation
    
        End If
    
    End Sub
    The highlighted values may be altered to suit your own requirements.

    The My Master Worksheet Name worksheet is the worksheet which contains the FileName prefix and the Date cells.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trying to activate an open Workbook with a variable
    By msf013 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2014, 01:53 PM
  2. macro to copy cells(Range) from an open Workbook to another open Workbook.
    By jbester in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2013, 09:47 AM
  3. Activate another open workbook and sheet
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2013, 11:14 AM
  4. [SOLVED] Activate an open workbook
    By jimbolhp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2012, 03:00 PM
  5. Activate Open Workbook
    By Launchnet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2010, 11:42 PM
  6. Activate sheet after open workbook?
    By proepert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2010, 08:22 AM
  7. Need to activate an open workbook
    By TrafficGuy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-06-2008, 04:52 PM
  8. [SOLVED] how to Activate the previous Open, workbook..?
    By acegap in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2006, 11:20 AM

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