+ Reply to Thread
Results 1 to 5 of 5

excel vba import file with three worksheets

  1. #1
    Registered User
    Join Date
    08-05-2022
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    71

    excel vba import file with three worksheets

    Hi.

    I am trying to import a file which has 3 sheets in it into a workbook I already have open using vba. The below works if it only has one sheet but not in this case as it has 3. And I don't know how excel decides why which sheet it imports out of the 3.
    I basically want it to prompt me to select the file and import all sheets from that file

    Cheers.
    sample of code below

    //code
    Sub Import_files()

    Dim fd As FileDialog
    Dim FileChosen As Long
    Dim i As Long

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.Title = "Select EngTimeReview file"
    fd.AllowMultiSelect = False
    fd.InitialView = msoFileDialogViewList
    fd.Filters.Clear
    fd.Filters.Add "Excel Workbook", "*.xls"
    FileChosen = fd.Show
    If FileChosen = -1 Then
    For i = 1 To fd.SelectedItems.Count
    Call ReadDataFromSourceFile(fd.SelectedItems(i))
    Next i
    End If

    End Sub


    Private Sub ReadDataFromSourceFile(sSrcFilename As String)

    Dim shtDest As Worksheet: Set shtDest = ThisWorkbook.Sheets.Add
    Dim wbSrc As Workbook: Set wbSrc = Workbooks.Open(sSrcFilename)

    With wbSrc.Sheets(1)
    .UsedRange.Copy shtDest.Range(.UsedRange.Address)

    On Error Resume Next
    shtDest.Name = .Name
    On Error GoTo 0
    End With

    wbSrc.Close SaveChanges:=False
    Exit Sub
    End Sub

  2. #2
    Registered User
    Join Date
    10-16-2023
    Location
    Poland
    MS-Off Ver
    Excel 2019 32bit WIN10
    Posts
    88

    Re: excel vba import file with three worksheets

    Use a For/Next loop:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-05-2022
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    71

    Re: excel vba import file with three worksheets

    Hi Tajan.

    I got a run time error 91. Object variable or With block variable not set

    If i go into debug mode its stopped ion line shtDest = ThisWorkbook.Sheets.Add

    it says shtDest = nothing

    Thanks

  4. #4
    Registered User
    Join Date
    10-16-2023
    Location
    Poland
    MS-Off Ver
    Excel 2019 32bit WIN10
    Posts
    88

    Re: excel vba import file with three worksheets

    Sorry, it should be:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-05-2022
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    71

    Re: excel vba import file with three worksheets

    thats great.

    works perfectly.

    Many thanks

+ 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. VBA to Import Worksheets Based on File Names in List
    By Gitty in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2019, 05:25 PM
  2. Import Multiple Worksheets into Master File
    By Dan_B in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-09-2015, 03:09 PM
  3. [SOLVED] VBA code to import data from multiple worksheets in the same file
    By WHATSXL in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-01-2014, 11:24 AM
  4. VBA to import csv file scattered over different worksheets
    By endoskeleton in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2011, 11:48 AM
  5. Import Worksheets from Another File
    By cratediggah in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2006, 08:43 AM
  6. No answer yet... Data Import from Many Worksheets to on file...
    By M Imran Buhary in forum Excel General
    Replies: 2
    Last Post: 06-13-2006, 03:35 AM
  7. Amalgamate Several Worksheets into one file thru Data import
    By M Imran Buhary in forum Excel General
    Replies: 0
    Last Post: 05-28-2006, 06:35 AM

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