+ Reply to Thread
Results 1 to 6 of 6

Referencing an Opened Workbook (with variable in file name)

Hybrid View

  1. #1
    Registered User
    Join Date
    12-15-2015
    Location
    Seattle
    MS-Off Ver
    Windows 10
    Posts
    3

    Referencing an Opened Workbook (with variable in file name)

    What I am trying to do:
    1) Have user enter date, make the date a part of the file name and open that file.
    2) Once the file is opened, look at each tab, find due dates within 7 days, and copy the data to the corresponding "5 days lookahead" workbook.

    ' Open Data Report. unhide columns and unfilter all tabs
    
        Dim strTodayDate As String
        Dim sPathTodayReport As String
        
            strTodayDate = txtTodayDate
    
    ' coding for unhide and unfilter (it works)
    
    sPathTodayReport = ".....\Airframe Daily_ " & strTodayDate & ".xlsx"
    
         
        Workbooks.Open (sPathTodayReport), UpdateLinks:=0
    
    Dim strTodayFileName As String
        
    strTodayFileName = """Airframe Daily_" & strTodayDate & ".xlsx"""
    
     Workbooks(strTodayFileName).Sheets("sheet 1").Select  '<< this code does not work
    
    'I have also tried Windows(strTodayFileName)

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Referencing an Opened Workbook (with variable in file name)

    You could add a variable that references the workbook being opened.

    For example, I've added the variable wbToday to this code and set it to refer to the workbook being opened.

    You can now use wbToday in any subsequent code when you want to refer to that workbook.

    ' Open Data Report. unhide columns and unfilter all tabs
        Dim wbToday As Workbook
        Dim strTodayDate As String
        Dim sPathTodayReport As String
        
            strTodayDate = txtTodayDate
    
    ' coding for unhide and unfilter (it works)
    
            sPathTodayReport = ".....\Airframe Daily_ " & strTodayDate & ".xlsx"
    
         
            Set wbToday = Workbooks.Open (sPathTodayReport, UpdateLinks:=0)
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-15-2015
    Location
    Seattle
    MS-Off Ver
    Windows 10
    Posts
    3

    Re: Referencing an Opened Workbook (with variable in file name)

    Norie, thank you for replying.

    The Workbook with the date has been opened. What I want to do is to go to this workbook, copy some data.
    Then go back to the workbook (5 days lookahead) and paste it. Then go back and forth.

    My codings are:

    Dim strTodayFileName As String
        
    strTodayFileName = """Airframe Daily_" & strTodayDate & ".xlsx"""
    
     Workbooks(strTodayFileName).Sheets("sheet 1").Select  '<< this code does not work
    
    'I have also tried Windows(strTodayFileName) still does not work

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Referencing an Opened Workbook (with variable in file name)

    You can't select a workbook and a sheet in that workbook at the same time.

    Mind you, there's no need to select/activate anything to copy/paste.

    What range do you want to copy from Sheet1 of the workbook that's been opened?

  5. #5
    Registered User
    Join Date
    12-15-2015
    Location
    Seattle
    MS-Off Ver
    Windows 10
    Posts
    3

    Re: Referencing an Opened Workbook (with variable in file name)

    It's actually more complicated:

    1) So now my working workbook (5days lookahead) and my data workbook (Airframe Daily with date variable) are opened.
    2) I want to go to Daily, tab Tier 2 to find out how many rows of data I have.
    3) Then for anything with 3rd column with a date less than now() =7, I want to copy that row
    4) Then go to the 5 days workbook, Tier 2 tab, and paste that row to the row after the last record.

            Dim i As Integer
            Dim iLastRowT2 As Integer
            Dim i5DLastRowT2 As Integer
            
            
            ' Find Last Row of Data in Tab Tier 2 90 Day Look Aheads
                
                Windows(strTodayFileName).Activate
                Sheets("Tier 2 90 Day Look Aheads").Select
                With ActiveSheet
                    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
                        iLastRowT2 = Cells.Find(What:="*", _
                        After:=.Cells(1, 1), _
                        LookAt:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
                        
                    Else
                        iLastRowT2 = 1
                        Cells(iLastRowT2, 1).Select
                        Exit Sub
                    End If
                End With
                
                Cells(iLastRowT2, 1).Select
                ActiveCell.Offset(1, 0).Activate
                
        'Find any row with Due date within 7 days
        
     
        For i = 2 To iLastRowT2
            If Cells(i, 3) < Now() + 7 Then
            Rows(i).Copy
            
                Windows("5Days Lookahead Template.xlsm").Sheets("Tier 2").Select
                
                With ActiveSheet
                    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
                        i5DLastRowT2 = Cells.Find(What:="*", _
                        After:=.Cells(1, 1), _
                        LookAt:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
                        
                    Else
                        i5DLastRowT2 = 1
                        Cells(i5DLastRowT2, 1).Select
                        Exit Sub
                    End If
                End With
                
                Cells(i5DLastRowT2, 1).Select
                ActiveCell.Offset(1, 0).PasteSpecial
            End If
        
        Next i

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Referencing an Opened Workbook (with variable in file name)

    Doesn't sound too complicated.

    Mind you all that, unneeded, selecting/activating does kind of make the code look complicated.

+ 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. [SOLVED] Put name of every newly opened file into variable?
    By davepoth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2014, 08:18 AM
  2. Replies: 4
    Last Post: 04-21-2013, 10:53 PM
  3. Replies: 2
    Last Post: 04-12-2013, 07:56 PM
  4. Replies: 5
    Last Post: 03-21-2013, 07:34 AM
  5. [SOLVED] How to set variable to be opened word file
    By Alexander_Golinsky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2012, 01:29 AM
  6. [SOLVED] Referencing an opened Workbook
    By BerkshireGuy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2005, 06:05 AM
  7. Referencing an Opened Workbook
    By DaveUK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-16-2005, 04:38 PM

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