I have amended the code below and have got it working. The problem I have now is that every time it loops it overwrites the data it wrote the previous loop
Offending line being ActiveSheet. Range ("A1: D30") = ValuesArray
I have known that somehow it should remember the last row and copy below this one but I cannot get it to work
Any help would be appreciated
Offending line being ActiveSheet. Range ("A1: D30") = ValuesArray
I have known that somehow it should remember the last row and copy below this one but I cannot get it to work
Any help would be appreciated
Code:
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Combined test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set desout = Workbooks.Add
With desout
.Title = "Compliance Test"
.Subject = "Compliance Test"
End With
desout.Worksheets.Add().Name = "dcp1"
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
'Tims Code
Dim NoOfSheets As Integer
NoOfSheets = mybook.Sheets.Count
Dim ValuesArray()
ReDim ValuesArray(NoOfSheets, 3)
For i = 1 To NoOfSheets
mybook.Sheets(i).Activate
With mybook.Sheets(i)
'get job number
rawJobNumber = .Range("A1").Value
rawJobNumberHyphen = InStr(1, rawJobNumber, "-") + 1
rawJobNumberSpace = InStr(1, rawJobNumber, " ")
JobNumber = Mid(rawJobNumber, rawJobNumberHyphen, rawJobNumberSpace - rawJobNumberHyphen)
ValuesArray(i - 1, 0) = JobNumber
'get date
ValuesArray(i - 1, 1) = .Range("B3").Value
'get actual
ValuesArray(i - 1, 2) = .Range("B4").Value
'get count of screens out
TempValue = .Range("A18:A10000").Cells.SpecialCells(xlCellTypeConstants).Count
If TempValue = 1 Then
If .Range("B18").Value = "" Then
ValuesArray(i - 1, 3) = 0
Else
ValuesArray(i - 1, 3) = 1
End If
Else
ValuesArray(i - 1, 3) = .Range("A18:A10000").Cells.SpecialCells(xlCellTypeConstants).Count
End If
End With
Next i
desout.Sheets("dcp1").Activate
ActiveSheet.Range("A1:D30") = ValuesArray
End If
mybook.Close savechanges:=False
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub