Hi All,
I am currently trying to Loop through a selected list of files, insert 2 rows at the top of each file then copying columns A to O out, then pasting those columns in A to O in a sheet called TK of my main work book, this sheet has a lot of complicated formulae in columns P to AV, and thus I need copy the results/values of these formulae based on the copied data all in row 2, to the next free line in another sheet called Data of my main work book. Then macro needs to close without saving and open the next file in the loop to do it all again.
All help and advise is most appreciated.
This is what I have so far, it currently loops through the files fine, but I am having trouble with copying the results of the formulae into the data sheet as well as the the inserting of rows/ the positioning of the data when it is copied over to the TK sheet (I understand that in the code I may be selecting the data to be copied over twice in two different ways, but cant get either way to work individually:
Kind regards,
I am currently trying to Loop through a selected list of files, insert 2 rows at the top of each file then copying columns A to O out, then pasting those columns in A to O in a sheet called TK of my main work book, this sheet has a lot of complicated formulae in columns P to AV, and thus I need copy the results/values of these formulae based on the copied data all in row 2, to the next free line in another sheet called Data of my main work book. Then macro needs to close without saving and open the next file in the loop to do it all again.
All help and advise is most appreciated.
This is what I have so far, it currently loops through the files fine, but I am having trouble with copying the results of the formulae into the data sheet as well as the the inserting of rows/ the positioning of the data when it is copied over to the TK sheet (I understand that in the code I may be selecting the data to be copied over twice in two different ways, but cant get either way to work individually:
Code:
Sub TKMAK()
Dim FolderPath As String, FileName As Variant
Dim WorkBk As Workbook
Dim SourceRange As Range, DestinationCell As Range
Dim SelectedFiles As Variant
'Set the cell where data from the first workbook will be copied to. This cell is updated as we
'loop through the merged workbooks
Set DestinationCell = ThisWorkbook.Worksheets("TK").Range("A1")
'FolderPath = "C:\Documents and Settings\lidibv0\My Documents\Metrics 2012\" ' source files location
'ChDrive FolderPath ' Set the current directory to the the folder path.
'ChDir FolderPath
' Use file dialog box, filter Excel files, allow multiple selections
SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
'Exit immediately if user clicked Cancel
If Not IsArray(SelectedFiles) Then Exit Sub
'File order returned by GetOpenFilename multi-select is different to the order visible to the user, so sort the array of files
Bubble_Sort_Array SelectedFiles
Application.DisplayAlerts = False
'Loop through the selected files
For Each FileName In SelectedFiles
'Open the workbook to be copied from
Set WorkBk = Workbooks.Open(FileName)
'Insert two rows at the top of sheet (shifting everything down by 2)
Rows("1:2").Select
Range("A2").Activate
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:N").Select
Range("N1").Activate
Selection.Copy
With WorkBk.Worksheets(1).UsedRange
Set SourceRange = .Offset(1, 0).Resize(.Rows.Count - 1)
End With
'Copy source data to destination cell
SourceRange.Copy DestinationCell
'Update destination cell to the next available row, according to number of rows
'in source data, ready for the next workbook to be merged
Set DestinationCell = DestinationCell.Offset(SourceRange.Rows.Count)
'Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
'Copy results of formulae over to next free collumns in Data sheet
ActiveWorkbook.TK.Activate
Range("2:2").Select
Selection.Copy
Sheets("Data").Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Next
Application.DisplayAlerts = True
End Sub
Private Sub Bubble_Sort_Array(theArray As Variant)
Dim i As Integer, j As Integer, temp As Variant
For i = LBound(theArray) To UBound(theArray) - 1
For j = i + 1 To UBound(theArray)
If theArray(i) > theArray(j) Then
temp = theArray(j)
theArray(j) = theArray(i)
theArray(i) = temp
End If
Next
Next
End Sub
Kind regards,