Results 1 to 3 of 3

excel macro run time error '1004'- Application defined or object defined error

Threaded View

  1. #1
    Registered User
    Join Date
    06-12-2014
    Posts
    2

    excel macro run time error '1004'- Application defined or object defined error

    Hi,

    My requirement is to write a VBA code in Excel 2010 to merge the multiple excel files into one master excel file. I have written the below code-
    **************************
    Sub Combine_Multiple_Excel_Files()
        Set wb = ThisWorkbook
        
        InitialFoldr$ = ThisWorkbook.Path & "\"    '<<< Startup folder to begin searching from
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select the files folder"
            .InitialFileName = InitialFoldr$
            .Show
            
            If .SelectedItems.Count <> 0 Then
                xDirect$ = .SelectedItems(1) & "\"
                xFname$ = Dir(xDirect$, 7)
                Do While xFname$ <> ""
                
                    Workbooks.Open (xDirect$ & xFname$), UpdateLinks:=False
                    
                    Application.Workbooks(xFname$).Activate
                    Set srcwb = Workbooks(xFname$)
        
                    For Each sh In srcwb.Sheets
                        LR1 = wb.Sheets(1).Cells(Rows.Count, 3).End(xlUp).Row
                        LR2 = sh.Cells(Rows.Count, 1).End(xlUp).Row
                        wb.Sheets(1).Range("A" & LR1 & ":A" & (LR1 + LR2 - 4)).Value = (xDirect$ & xFname$)
                        wb.Sheets(1).Range("B" & LR1 & ":B" & (LR1 + LR2 - 4)).Value = sh.Name
                        sh.Range("A4:R" & LR2).Copy Destination:=wb.Sheets(1).Range("C" & LR1)
                    Next
                    
                    Application.CutCopyMode = False 'Clear Clipboard
                    srcwb.Close savechanges:=False
                    xFname$ = Dir
                    
                Loop
            End If
        End With
        
        Application.GoTo reference:=wb.Sheets(1).[A1]
        
    End Sub
    **************************
    
    However, when i run this macro, i receive the following error-
    
    **************************
    excel macro run time error 1004
    
    Application defined or object defined error
    
    **************************
    
    When i press Debug, i see that the error is coming at-
    
    **************************
    
    wb.Sheets(1).Range("A" & LR1 & ":A" & (LR1 + LR2 - 4)).Value = (xDirect$ & xFname$)
    
    **************************
    I am new to VBA programming. So, Can anyone please help me here? This is urgent.

    Thanks,
    Karan
    Last edited by vlady; 06-12-2014 at 11:03 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 03-12-2014, 12:42 PM
  2. [SOLVED] Run time error 1004 Application-Defined or Object-Defined Error - Placing data into next e
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-27-2014, 12:57 PM
  3. [SOLVED] Macro causes Run time error 1004 Application defined or object-defined error
    By Firefighter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-12-2012, 04:09 AM
  4. [SOLVED] run-time error '1004' application-defined or object-defined error - Excel 2007
    By kaurka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2012, 02:46 AM
  5. Macro - Run-time Error '1004': Application defined or object defined error
    By robby10 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-14-2010, 07:55 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