+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: import from 4 xls files to 1

  1. #1
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    import from 4 xls files to 1

    Hello,
    I am new to this forum and I am in need of assistance; i am trying to append data from 4 excel sheets into 1 excel sheet keeping the tab names intact and also copy the data by using a macro. The code:

    HTML Code: 
    Sub Transferdata()
        Dim basebook As Workbook
        Dim mybook As Workbook
        Dim i As Long
        Application.ScreenUpdating = False
        With Application.FileSearch
            .NewSearch
            .LookIn = "C:\Report Beta 1.1\Test Dump"
            .SearchSubFolders = False
            .FileType = msoFileTypeExcelWorkbooks
            If .Execute() > 0 Then
                Set basebook = ThisWorkbook
                For i = 1 To .FoundFiles.Count
                    Set mybook = Workbooks.Open(.FoundFiles(i))
                    mybook.Worksheets(1).Copy after:= _
                                                     basebook.Sheets(basebook.Sheets.Count)
                    ActiveSheet.Name = mybook.Name
                    With ActiveSheet.UsedRange
                        .Value = .Value
                    End With
                    mybook.Close
                Next i
            End If
        End With
        Application.ScreenUpdating = True
    End Sub
    This works for the initial run and imports the data but if I run the macro again it tries to keep adding the same tabs name which gives an error, i don't want it to add again just append the data to its respective tabs. Another thing that I would like is an import date to be inserted every time i run it. The date column will have to be specified or added to each sheet as I don't have one. Can someone please help with this problem?

    Thanks in advance,
    Rick
    Last edited by RickR; 12-20-2010 at 12:14 PM. Reason: to get code lined properly

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    This should work. I've taken out the .Filesearch method as it was taken out of Excel in 2007+, this version will continue to work regardless of which version of Excel you run it on, once it's working the way you want. This should at least get you most of the way there...

    ...code removed, see further down
    Last edited by JBeaucaire; 12-20-2010 at 04:54 PM. Reason: code removed, see below.
    _________________
    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!)

  3. #3
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    Thank you for the quick response JBeaucaire but when I replaced my code with yours it seems to run things but never shows anything and doesn't create any tabs.
    any reason why?

    Also, does this create an import date column when it imports (or copies over to the one excel sheet)? if not can you add it?

    Sorry I am kind of new to this stuff.
    Last edited by RickR; 12-20-2010 at 02:51 PM.

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    Ok, try this, small tweaks and I tested this on some files on my system, seems good.
    Option Explicit
    
    Sub TransferData()
    Dim wbBase As Workbook:     Set wbBase = ThisWorkbook
    Dim MyBook As Workbook
    Dim fName As String
    Dim fPath As String
    Dim wsName As String
    Dim NR As Long
    
    fPath = "C:\2011\"     'don't forget the final \
    fName = Dir(fPath & "*.xls")
    
    On Error Resume Next
    Application.ScreenUpdating = False
    
    Do While Len(fName) > 0
        Set MyBook = Workbooks.Open(fPath & fName)
        wsName = Left(MyBook.Name, InStrRev(MyBook.Name, ".") - 1)
        
        'check to see if sheet exists in main workbook already
        If Not Evaluate("ISREF([" & wbBase.Name & "]" & wsName & "!A1)") Then
            'if not, add it
            MyBook.Sheets(1).Copy after:=wbBase.Sheets(wbBase.Sheets.Count)
            With ActiveSheet
                .Name = wsName
                .UsedRange.Value = .UsedRange.Value
            End With
        Else
            'if so, add to it
            With wbBase.Sheets(wsName)
                NR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), _
                    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                MyBook.Sheets(1).UsedRange.Copy
                .Range("A" & NR).PasteSpecial xlPasteValues
            End With
        End If
                
        MyBook.Close False
        
        fName = Dir
    Loop
        
    Application.ScreenUpdating = True
    End Sub
    _________________
    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!)

  5. #5
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    Works great so far, is there anyway to have it generate an import date in a new column in each sheet that way i can distinguish from like yesterdays imports, today's, tomorrows etc.?

    thanks again!
    Last edited by RickR; 12-20-2010 at 05:21 PM.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    Maybe this:
    Option Explicit
    
    Sub TransferData()
    Dim wbBase As Workbook:     Set wbBase = ThisWorkbook
    Dim MyBook As Workbook
    Dim fName As String
    Dim fPath As String
    Dim wsName As String
    Dim NR As Long
    
    fPath = "C:\2011\"     'don't forget the final \
    fName = Dir(fPath & "*.xls")
    
    On Error Resume Next
    Application.ScreenUpdating = False
    
    Do While Len(fName) > 0
        Set MyBook = Workbooks.Open(fPath & fName)
        wsName = Left(MyBook.Name, InStrRev(MyBook.Name, ".") - 1)
        
        'check to see if sheet exists in main workbook already
        If Not Evaluate("ISREF([" & wbBase.Name & "]" & wsName & "!A1)") Then
            'if not, add it
            MyBook.Sheets(1).Copy after:=wbBase.Sheets(wbBase.Sheets.Count)
            With ActiveSheet
                .Name = wsName
                .UsedRange.Value = .UsedRange.Value
                .Columns(1).Insert xlShiftToRight
                .Range("A:A").SpecialCells(xlBlanks) = Date
            End With
        Else
            'if so, add to it
            With wbBase.Sheets(wsName)
                NR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), _
                    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                MyBook.Sheets(1).UsedRange.Copy
                .Range("B" & NR).PasteSpecial xlPasteValues
                .Range("A:A").SpecialCells(xlBlanks) = Date
            End With
        End If
                
        MyBook.Close False
        
        fName = Dir
    Loop
        
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 12-21-2010 at 12:23 PM.
    _________________
    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!)

  7. #7
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    I tried the code and got an error on this line:
    .Range("A:A").SpecialCells(xlblank) = Date
    The error said "variable not defined"
    It highlighted the "xlblank"

    Thanks

    Rick
    Last edited by RickR; 12-21-2010 at 10:06 AM.

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    Should be xlBlanks, plural.
    _________________
    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!)

  9. #9
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    Man you are the GREATEST! I can not tell you how much you have made my life easier!!
    I have one more small favor to ask if I may, On the import from the other sheets it imports the row 1 which is header column like ID, Name, Description, etc. Every time i run the macro you made it also imports that line ; is there a way to add to the code you gave me to delete only the duplicate of row 1 in the current excel sheet everything is going to?
    If not no big deal..

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    Maybe this:
    MyBook.Sheets(1).UsedRange.Offset(1).Copy
    _________________
    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!)

  11. #11
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    now it keeps adding the tabs instead of appending to the one there and adds a (2) at then end of the name, also the data it imports it knocks the date into a text instead of keeping it date. (not the date field but other fields i have in the other workbooks that are dates; yes those are formatted for date) It works on the initial run but after that when it appends is when it doesn't keep format.
    Last edited by RickR; 12-21-2010 at 01:37 PM.

  12. #12
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    I actually found for some reason that when i added a new excel sheet to the mix it would only duplicate the tabs that one instead of append not sure why but the offset works fine. So i guess it was me???!!!??? I believe it was due to spaces in the name.
    Last edited by RickR; 12-21-2010 at 02:13 PM.

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    We can merge all these fixes into one tweak:
            With wbBase.Sheets(wsName)
                NR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), _
                    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                MyBook.Sheets(1).UsedRange.Offset(1).Copy .Range("B" & NR)
                .Range("A:A").SpecialCells(xlBlanks) = Date
            End With
    _________________
    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!)

  14. #14
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: import from 4 xls files to 1

    Quote Originally Posted by RickR View Post
    I believe it was due to spaces in the name.
    Hidden spaces are notorious for messing up text match logic.
    _________________
    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!)

  15. #15
    Registered User
    Join Date
    12-20-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: import from 4 xls files to 1

    Again you amaze me; wish i knew a fraction as you on this stuff.

    The import date that you created always ends by itself for instance:

    12/21/10 | data | data | data
    12/21/10 | data | data | data
    12/21/10

    causes a blank row besides import date at end.
    Last edited by RickR; 12-21-2010 at 02:52 PM.

+ 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.2.0