+ Reply to Thread
Results 1 to 6 of 6

Open excel file read the data and close

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2011
    Posts
    14

    Open excel file read the data and close

    Hi All,
    I am new to this forum.
    Can anyone help me in writing a macro code for Opening a excel sheet and copy the data in it and close the file???

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Open excel file read the data and close

    this macro opens file, copy sheet1 on sheet2, closes the file
    Sub Open_File_Copy_to_Sheet()
    Dim fd As FileDialog, SelFile
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    Set wb = ActiveWorkbook
    If fd.Show = -1 Then
       SelFile = fd.SelectedItems(1)
       ActiveCell.Value = SelFile
       Workbooks.Open Filename:=SelFile
    End If
    Set fd = Nothing
    Set wb1 = ActiveWorkbook
    ActiveSheet.Copy wb.Sheets(2)
    wb1.Close
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    03-04-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2011
    Posts
    14

    Re: Open excel file read the data and close

    Thanks for your reply.
    But I need that to be copied to a new file....

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open excel file read the data and close

    Hi, girish1989,

    maybe try something like this (there´s still some information from your side missing (save to which name, xlFileFormat and where to save):
    Sub Open_File_Copy_to_NewWorkbookt()
    Dim fd As FileDialog
    Dim wbData As Workbook
    
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    If fd.Show = -1 Then
      Set wbData = Workbooks.Open(Filename:=fd.SelectedItems(1))
      wbData.ActiveSheet.Copy
      With ActiveWorkbook
        .SaveAs Format(Now, "yymmdd_hhnnss") & "_test", FileFormat:=xlExcel8
        .Close False
      End With
      wbData.Close False
      Set wbData = Nothing
    End If
    Set fd = Nothing
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    03-04-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2011
    Posts
    14

    Re: Open excel file read the data and close

    Thanks a lot

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open excel file read the data and close

    If that takes care of your original query, please select Thread Tools from the menu above and mark the thread as solved. Thanks.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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