+ Reply to Thread
Results 1 to 6 of 6

Open file from location using VBA code - file name changes everytime.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    India, Pune
    MS-Off Ver
    Excel 2003
    Posts
    6

    Open file from location using VBA code - file name changes everytime.

    Hi,
    I need help to write a code to open a User name workbook from a location. This code should be in master file (Update_Report.xls) user will enter data in master file & press button to perform below actions.
    Open perticular User name (Cell value B4 in Update_Report) excel workbook from location.
    Copy data from master file.
    Paste data in user file.
    Save & close user file.

    Code is as below. Code is not able to locate user file.

    Sub Button8_Click()
    
      'Open user file & copy data
    
      FName = Range("B4")
      
      ChDir "C:\TEST\User Report"
        Workbooks.Open Filename:= _
            "C:\TEST\User Report\FName"
          Workbooks("Update_Report.xls").Activate
        Range("A1:B7").Copy
        Workbooks("FName").Activate
        Range("B5").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    
    End Sub
    I am not able to upload master file due to some error. After I press button "Manage Attachments" nothing happens.

    Please help me with this.
    Thanks in advance.

    SWapnil
    Last edited by Leith Ross; 07-27-2013 at 02:29 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Open file from location using VBA code - file name changes everytime.

    Hello Swapnil.Kupwade,

    Welcome to the Forum!

    Since you changed the directory, both workbooks must be in the same directory. If not then the code should be...
    Sub Button8_Click()
    
      'Open user file & copy data
    
      FName = Range("B4")
      
        Workbooks.Open Filename:= _
            "C:\TEST\User Report\FName"
          Workbooks("Update_Report.xls").Activate
        Range("A1:B7").Copy
        Workbooks("FName").Activate
        Range("B5").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    
    End Sub
    Check that the file name in cell "B4" contains the file extension, i.e. ".xlsx", ".xlsm", etc.
    Last edited by Leith Ross; 07-27-2013 at 02:34 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Open file from location using VBA code - file name changes everytime.

    pls send a sample

  4. #4
    Registered User
    Join Date
    06-20-2013
    Location
    India, Pune
    MS-Off Ver
    Excel 2003
    Posts
    6

    Post Re: Open file from location using VBA code - file name changes everytime.

    Thanks
    I have tried with ".xls" in cell B4. It is not working. Code gives error for FName not found. I have attached image.
    I think I am missing define FName As Varient or similar thing. Please help me to solve the error.
    I am sending folders with sample.

    Thanks again for the help.

    Regards,
    SWapnil

    FName Error.jpg
    TEST.zip
    Last edited by Leith Ross; 07-29-2013 at 12:08 PM. Reason: Repositioned Attachments

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Open file from location using VBA code - file name changes everytime.

    Hello Swapnil.Kupwade,

    This should work...
    Sub Button8_Click()
    
        Dim FName As Variant
    
          ' Open user file & copy data
            FName = Range("B4")
      
            ChDir "C:\TEST\User Report"
        
            Workbooks.Open Filename:="C:\TEST\User Report\" & FName
            
          ' FName is now the ActiveWorkbook. The range does not need to be prefixed.
            Range("B5:C11").Value = Workbooks("Update_Report.xls").Range("A1:B7").Value
               
    End Sub

  6. #6
    Registered User
    Join Date
    06-20-2013
    Location
    India, Pune
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Open file from location using VBA code - file name changes everytime.

    Thanks Leith Ross,
    Code is working well except last line
    Range("B5:C11").Value = Workbooks("Update_Report.xls").Range("A1:B7").Value

    Is it because I am using Excel 2003?


    Thanks again,
    SWapnil

+ 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] [Help]macro to open file browser, to select a location, and save the location to a cell
    By zhuleijia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-06-2013, 09:56 AM
  2. Replies: 5
    Last Post: 09-29-2012, 12:40 PM
  3. VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2011, 12:12 AM
  4. Replies: 0
    Last Post: 08-11-2006, 11:15 AM
  5. [SOLVED] code to check file size everytime an Excel file is opened
    By Kaiser in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2006, 12:50 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