+ Reply to Thread
Results 1 to 4 of 4

How do you reference a variable file in VBA?

Hybrid View

Sgligori How do you reference a... 12-10-2015, 12:11 PM
sktneer Re: How do you reference a... 12-10-2015, 12:22 PM
Sgligori Re: How do you reference a... 12-10-2015, 02:51 PM
sktneer Re: How do you reference a... 12-10-2015, 03:18 PM
  1. #1
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    How do you reference a variable file in VBA?

    How can I reference Sheet2 of my opened file?

    Currently I have a macro that finds the most recent file in a folder and opens it. If I wanted to do a calculation based on a column in Sheet 2 of the newly opened file, how would I reference this file in VBA? I cannot use the file name to call it since the file name will change (each time I run the macro, the most recent file is opened). For example, if I run the macro this week, the file name "12-7 Reports" is opened, but if I run it next week, the file "12-14 Reports is opened"
    Last edited by Sgligori; 12-10-2015 at 12:13 PM.

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: How do you reference a variable file in VBA?

    You may try like this...
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets(2)
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Forum Contributor
    Join Date
    11-05-2014
    Location
    Charlotte
    MS-Off Ver
    2013
    Posts
    181

    Re: How do you reference a variable file in VBA?

    Thanks, but that does not work because the file is not my activeworkbook. I have an active workbook and I need to then switch over to the recently opened file.

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: How do you reference a variable file in VBA?

    Try this...

    Dim wb As Workbook, swb As Workbook
    Dim ws As Worksheet, sws As Worksheet
    
    Set wb = ThisWorkbook   'wb refers to the macro workbook
    Set ws = wb.Sheets(1)   'ws refers to the first sheet of macro workbook
    
    'Once the code opens a workbook
    Set swb = ActiveWorkbook 'swb refers to the opened workbook
    Set sws = swb.Sheets(2)  'sws refers to the Sheet2 of opened workbook
    
    wb.Activate    'Activating macroworkbook again

+ 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. File reference containing variable - is it possible?
    By AwelStaw in forum Excel General
    Replies: 1
    Last Post: 04-09-2013, 06:46 PM
  2. Variable File Reference and Variable Table Array in VBA VLOOKUP
    By Gingeiko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2011, 11:08 PM
  3. variable file reference
    By johndough185 in forum Excel General
    Replies: 5
    Last Post: 11-01-2007, 09:48 AM
  4. [SOLVED] Making a file and worksheet reference into a variable....
    By Bernard Liengme in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM
  5. [SOLVED] Making a file and worksheet reference into a variable....
    By Mr Mike in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-06-2005, 04:05 PM

Tags for this Thread

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