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
Bookmarks