Results 1 to 5 of 5

Run-time error '1004': VBA code fails to read and copy data from the files

Threaded View

  1. #1
    Registered User
    Join Date
    02-13-2011
    Location
    Mlebourne
    MS-Off Ver
    Excel 2003
    Posts
    9

    Run-time error '1004': VBA code fails to read and copy data from the files

    Hi

    I've copied the code for looping through the files in the folder from a web site and included my own code to capture the data from the worksheets.Initially, it worked find when I was reading data from multiple sheets and write in row-wise in one sheet.Then I changed the code to write column-wise and the error starting to pop-up - Run-time error '1004':Application-define or object-defined error.And now its giving the same error for row-wise writing data.

    It may be simple thing but it is giving me a lot of grief.

    Please refer to the code below and many thanks in advance.
    Sub LoopAllExcelFilesInFolder_Version2()
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim c As Range
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    Dim i As Integer
    Dim k As Integer
    Dim wsx As Worksheet
    
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    Set wsx = ActiveSheet
     Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    NextCode:
        myPath = myPath
        If myPath = "" Then GoTo ResetSettings
        myExtension = "*.xls*"
    
    myFile = Dir(myPath & myExtension)
    
    Do While myFile <> ""
     j = InputBox("input row no")
    
     i = 1
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        DoEvents
        
        For Each ws In wb.Worksheets
        ws.Activate
        If (ws.Index <> 21 And ws.Index <> 22) Then
        LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
        For Each c In ws.Range("A2:A" & LastRow).Cells
        If c.Offset(0, 4) <> "" Then
            wsx.Cells(j, i) = c.Offset(0, 4)
        ElseIf c.Offset(0, 5) <> "" Then
            wsx.Cells(j, i) = c.Offset(0, 5)
            Else
          wsx.Cells(j, i) = "RNP"
       
        i = i + 1
        Next
        End If
        Next
        
         'Save and Close Workbook
          wb.Close SaveChanges:=True
         
        'Ensure Workbook has closed before moving on to next line of code
          DoEvents
    
        'Get next file name
          myFile = Dir
      Loop
    
    'Message Box when tasks are completed
      MsgBox "Task Complete!"
    ResetSettings:
      'Reset Macro Optimization Settings
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by mshaji; 05-03-2019 at 01:21 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy Data outside Excel and PasteSpecial inside Worksheet - Run-time error '1004'
    By deisouss in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-17-2021, 02:27 AM
  2. Replies: 0
    Last Post: 03-05-2019, 03:14 PM
  3. creating hyperlinks fails after row 65530 with run time error 1004
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-09-2017, 05:00 AM
  4. VBA Code...error = run time error 1004 autofilter method of range class failed
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-15-2013, 04:49 PM
  5. Code fails with error 1004
    By cdjones in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-22-2010, 06:27 PM
  6. Macro fails at pasting (Run-time error '1004' Paste method fail)
    By SergioS in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-09-2010, 02:17 PM
  7. Run time error 1004-print files
    By Brad in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-03-2005, 10:06 AM

Tags for this Thread

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