Thanks for the quick reply,
In fact, I tried to improve the code, by using more ranges to capture several columns, but somehow the data copied from these ranges never reaches the new workbook and most of the transmission is lost, being able to insert the last headers only:
Option Explicit
Private Sub Obtain_Data_Click()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim SelectedFiles() As Variant
Dim NRow As Long, NRow2 As Long, NRow3 As Long, NRow4 As Long, NRow5 As Long
Dim FileName As String
Dim NFile As Long
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range, DestRange2 As Range, DestRange3 As Range, DestRange4 As Range, DestRange5 As Range
Dim i As Integer
Dim SourceRange2 As Range, SourceRange3 As Range, SourceRange4 As Range, SourceRange5 As Range
Dim lastrow As Long, lastcolumn As Long
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\Mark\Desktop\PV Data"
' Set the current directory to the the folder path.
ChDrive FolderPath
ChDir FolderPath
' Open the file dialog box and filter on Excel files, allowing multiple files
' to be selected.
SelectedFiles = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
NRow2 = 1
NRow3 = 1
NRow4 = 1
NRow5 = 1
' Loop through the list of returned file names
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
' Set FileName to be the current workbook file name to open.
FileName = SelectedFiles(NFile)
' Open the current workbook.
Set WorkBk = Workbooks.Open(FileName)
'Set where to get data from
'column A, for Date
Set SourceRange = WorkBk.Worksheets(1).Range("A:A")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange = SummarySheet.Range("C" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
'Column B Irradiance Horizontal
Set SourceRange2 = WorkBk.Worksheets(1).Range("B:B")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange2 = SummarySheet.Range("D" & NRow2)
Set DestRange2 = DestRange2.Resize(SourceRange2.Rows.Count, _
SourceRange2.Columns.Count)
' Copy over the values from the source to the destination.
DestRange2.Value = SourceRange2.Value
' Increase NRow so that we know where to copy data next.
NRow2 = NRow2 + DestRange2.Rows.Count
'column N Irradiance Tilted
Set SourceRange3 = WorkBk.Worksheets(1).Range("N:N")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange3 = SummarySheet.Range("E" & NRow3)
Set DestRange3 = DestRange3.Resize(SourceRange3.Rows.Count, _
SourceRange3.Columns.Count)
' Copy over the values from the source to the destination.
DestRange3.Value = SourceRange3.Value
' Increase NRow so that we know where to copy data next.
NRow3 = NRow3 + DestRange3.Rows.Count
'column X Rated PV
Set SourceRange4 = WorkBk.Worksheets(1).Range("X:X")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange4 = SummarySheet.Range("F" & NRow4)
Set DestRange4 = DestRange4.Resize(SourceRange4.Rows.Count, _
SourceRange4.Columns.Count)
' Copy over the values from the source to the destination.
DestRange4.Value = SourceRange4.Value
' Increase NRow so that we know where to copy data next.
NRow4 = NRow4 + DestRange4.Rows.Count
'column G Energy Delivered to the Grid
Set SourceRange5 = WorkBk.Worksheets(1).Range("G:G")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange5 = SummarySheet.Range("I" & NRow5)
Set DestRange5 = DestRange5.Resize(SourceRange5.Rows.Count, _
SourceRange5.Columns.Count)
' Copy over the values from the source to the destination.
DestRange5.Value = SourceRange5.Value
' Increase NRow so that we know where to copy data next.
NRow5 = NRow5 + DestRange5.Rows.Count
SummarySheet.Range("A1").Value = "Name of the Site"
' Set the cell in column F2 to be the file name.
SummarySheet.Range("A2").Value = WorkBk.Name
SummarySheet.Range("D1") = "Irradiance (GHI)"
SummarySheet.Range("E1") = "Tilted Irradiance"
SummarySheet.Range("F1") = "Rated PV Energy"
SummarySheet.Range("G1") = "Calculated PV Energy (exc. Reflection losses)"
SummarySheet.Range("H1") = "Energy To The Grid"
SummarySheet.Range("I1") = "Projected PR"
SummarySheet.Range("J1") = "Calculated PR"
SummarySheet.Range("K1") = "Area of the Site in m2"
SummarySheet.Range("L1") = "Panel Efficiency in %"
Next NFile
' Call AutoFit on the destination sheet so that all data is readable.
SummarySheet.Columns.AutoFit
End Sub
I think this method is better, but no clue what kind of mistake I am doing, as the program runs without errors.
Thanks for your patience
Bookmarks