+ Reply to Thread
Results 1 to 3 of 3

Dim open workbooks with hardcoding their names

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-26-2014
    Location
    Kentucky
    MS-Off Ver
    Excel 2013
    Posts
    128

    Dim open workbooks with hardcoding their names

    Here is my entire code to open a file from a ComboBox in a userform. How can i alter it to get set the file that it opens as WB1 because this would allow me to reference it in the macro. This code was given to me by someone on this site and i am still pretty new to vba so i am limited. I actually want to incorporate another combobox with it to open another file from the same folder. How would I do that? Thank you in advance for the help.
    Option Explicit
    
    Const userDir As String = "C:\Users\Public"   
    
    
    Private Sub CommandButton1_Click()
       ' This is for Excel files
       Workbooks.Open Filename:=userDir & Me.ComboBox1
    End Sub
    
    
    Private Sub UserForm_Activate()
       
       Dim fname As String
       Dim target As Range
       
       Me.ComboBox1.Clear
       
       fname = Dir(userDir & "\*", vbNormal)
       
       Do While fname <> ""
          
          Me.ComboBox1.AddItem fname
          fname = Dir
       
       Loop
    
    
    End Sub
    Last edited by RaydenUK; 03-02-2015 at 12:13 AM.

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Dim open workbooks with hardcoding their names

    Set WB1 = Workbooks(Me.Combobox1)

  3. #3
    Forum Contributor
    Join Date
    03-26-2014
    Location
    Kentucky
    MS-Off Ver
    Excel 2013
    Posts
    128

    Re: Dim open workbooks with hardcoding their names

    Thanks that works perfectly!

+ 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] Need VBA to retreive worksheet names based on open workbooks
    By The Exceller in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-18-2013, 12:40 PM
  2. [SOLVED] Open Multiple Workbooks, Record names, Copy paste to Active Workbook, Close the Workbooks
    By vba_madness in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 06:09 AM
  3. Multiple open workbooks pulling their names!?
    By The Phil in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-28-2010, 01:44 PM
  4. Switch between 2 open workbooks in a macro without hardcoding the filename
    By russ8502 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2007, 02:38 AM
  5. how to open workbooks with different names
    By Kanan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-06-2005, 07:06 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