Hey Guys,
I am trying to create a macro to help automate the data analysis process for my research. I have CSV files that are grouped in sections of 8 which all contain data which I need to consolidate and then average for further analysis. I've written the following code which effectively opens all 8 of my desired files with a pretty simple Do Loop. Ideally, I wanted all of the data to be copied directly to my worksheet titled "master" however due to continual formatting errors within Excel I have decided to create 8 different sheets which the data from each of my 8 files are copied and pasted. So far all of this is working just fine however when I try to copy each of these columns of data and paste them onto my "master" sheet nothing is being pasted. After a little trouble shooting I noticed that after all of the data is pasted to their respective sheets each of the columns containing data are still highlighted. I've tried activating individual sheets or even selecting individual cells but nothing seems to work. I've included my code below as well as my workbook to better illustrate my set up. Any helps is greatly appreciated thanks again.
Public strFileName As String
Public currentWB As Workbook
Public dataWB As Workbook
Public strCopyRange As String
Sub DataAnalysis()
Dim strWhereToCopy As String, strStartCellColName As String
Dim strInfoSheet As String
Dim M As Long
Dim LR As Long
strInfoSheet = "Info"
LR = Range("A" & Rows.Count).End(xlUp).Row
M = Application.WorksheetFunction.Max(Range("A:A"))
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Formatting data sheets
Sheets("Master").Cells.Clear
Sheets("Sheet1").Cells.Clear
Sheets("Sheet2").Cells.Clear
Sheets("Sheet3").Cells.Clear
Sheets("Sheet4").Cells.Clear
Sheets("Sheet5").Cells.Clear
Sheets("Sheet6").Cells.Clear
Sheets("Sheet7").Cells.Clear
Sheets("Sheet8").Cells.Clear
On Error GoTo ErrH
Sheets(strInfoSheet).Select
Range("B2").Select
'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
Do While ActiveCell.Value <> ""
strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
strCopyRange = ActiveCell.Offset(0, 2) & ":" & ActiveCell.Offset(0, 3)
strWhereToCopy = ActiveCell.Offset(0, 4).Value
strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)
Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True
Set dataWB = ActiveWorkbook
Range(strCopyRange).Select
Selection.Copy
currentWB.Activate
Sheets(strWhereToCopy).Select
lastRow = LastRowInOneColumn(strStartCellColName)
Cells(lastRow + 1, 1).Select
Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
Application.CutCopyMode = False
dataWB.Close False
Sheets(strInfoSheet).Select
ActiveCell.Offset(1, 0).Select
Loop
Exit Sub
ErrH:
MsgBox "It seems some file was missing. The data copy operation is not complete."
Exit Sub
'Consolidate to Master Sheet
'Sheet One
Sheets("Sheet1").Activate
Range("A2:A" & LR).Select
Selection.Copy
Sheets("Master").Select
Range("B2").Select
ActiveSheet.Paste
End Sub
Public Function LastRowInOneColumn(col)
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
LastRowInOneColumn = lastRow
End Function
Bookmarks