+ Reply to Thread
Results 1 to 7 of 7

Macro to Find correct File, Copy a Specific Tab and Close file

  1. #1
    Registered User
    Join Date
    01-30-2017
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    4

    Macro to Find correct File, Copy a Specific Tab and Close file

    Hello All,

    I am trying to write a Macro to prompt the user to select a file, open the selected file and copy a specific tab into my existing worksheet. I need this because the tab I need to copy will change every month. I am using the below code to prompt the user but I cannot figure out how to make it actually open and copy the tab I need. Any help would be appreciated.

    Private Sub CommandButton1_Click()
    Dim fDialog As FileDialog, result As Integer, it As Variant
    Set fDialog = Application.FileDialog(msoFileDialogOpen)

    'Optional: FileDialog properties
    fDialog.Title = "Select a file"
    fDialog.InitialFileName = "http://Filename.xlsx"

    'Optional: Add filters
    fDialog.Filters.Clear
    fDialog.Filters.Add "All files", "*.*"
    fDialog.Filters.Add "Excel files", "*.xlsx"

    If fDialog.Show = -1 Then
    Debug.Print fDialog.SelectedItems(1)
    End If
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False



    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Hi, welcome to the forum, If I look at the code (you forgot the code tags, read the forum's rules/instructions to see how this is done) I am assuming you have not used it since it does not even open the file. and why the URL reference? Is the file on a shared drive or a web page?
    The Tab you speak of I assume is a worksheet in that file that you want the user to open.
    Where in your file do you want to copy this worksheet? Into a new one? Replace the contents of an existing Worksheet?
    Will the file with this macro be the same one as the one where you are saving the worksheet to?
    Which version of Excel?
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    01-30-2017
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Hello,
    Thank you for your quick reply. Apologies on my delayed response as well as not using the correct code tags. I have been out of the office for the last week. I will do my best to answer all your questions. I am very new to Macros.
    1) No. I have not been able to use the code at all.
    2) Yes. The file is stored on our company Web Portal.
    3) Yes. Tab = Worksheet. Apologies for not using the correct.
    4) I would like the copied worksheet to replace the contents of an existing worksheet.
    5) Yes. the Macro will be in the workbook that I wish to copy the worksheet to.
    6) I am using Excel 2010.

    Thank you in advance for your help.

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Depending a bit of how you wish user to select file there are several options but in this macro I just hard coded folder and file name. I also assume that relevant information is found on sheet1 in the file you wish to open.

    Macro starts by clearing active sheet in the workbook where you have added the macro. This workbook is known to the macro as "ThisWorkbook"

    Please Login or Register  to view this content.
    After file is opened rellevant aheet is activated and all information copied to "ThisWorkbook" and then the opened file is closed without saving any canges. There is also the option of making a copy of the sheet of interest and exporting it to "ThisWorkbbok". A thing to consider if the sheet you wish to copy has some very special formating that you would like to keep.

    Alf
    Last edited by Alf; 03-27-2017 at 12:28 PM.

  5. #5
    Registered User
    Join Date
    01-30-2017
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Hello,

    I need the user to be able to select a specific file so hard coding the file name and folder will not work.

    Thanks,

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Your are welcome but consider that a proposed solution depends very much on the information given.

    Alf

    Ps the magic word you are looking for is perhaps "Application.Dialogs(xlDialogOpen).Show"

  7. #7
    Registered User
    Join Date
    01-30-2017
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Find correct File, Copy a Specific Tab and Close file

    Hello All,
    Here is the solution I was able to piece together. It seems to be working fine. Feedback appreciated.

    Please Login or Register  to view this content.

+ 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. Macro to close excel file at specific time
    By Cremorneguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-21-2016, 02:40 AM
  2. [SOLVED] Macro to open Excel file, run other macros save, and close file & repeat.
    By Ronnet2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2015, 09:47 AM
  3. Script/macro to save a copy as tab delimited text file and then close original file
    By Merlin803 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-02-2015, 01:34 PM
  4. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  5. Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file
    By Langchop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2013, 05:09 AM
  6. [SOLVED] Macro to copy only specific data from a txt file to an excel file
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 48
    Last Post: 04-13-2012, 02:11 AM
  7. [SOLVED] [SOLVED] Close file and run macro from newly opened file
    By Pradip Jain in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-23-2005, 07:06 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