Hello everyone, I am quite new to excel and have tried making a simple macro that just copies all of the available data from a workbook and pastes it in another. I have looked at information available on the internet and combining data from multiple sources I have reached the following code that does not do what I want:
I have tried a search on the forums but I could not find anything that I could apply with my limited understanding. Any indications on how to proceed would be greatly appreciated. Thank you all for your assistance on this matter.
Code:
Option Explicit
Sub GatherData()
Dim wbCSV As Workbook
Dim wsMstr As Worksheet
Dim FileToOpen As String
Dim fPath As String
Dim fCSV As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set wsMstr = ThisWorkbook.Sheets("Data")
'If MsgBox("Clear the existing Data?", vbYesNo, "Clear?") _
= vbYes Then wsMstr.UsedRange.Clear
' MsgBox "Please select a folder with the relevant files"
' Do
' With Application.FileDialog(msoFileDialogFolderPicker)
' .InitialFileName = "C:\Users\And\Desktop\MP1X"
' .AllowMultiSelect = False
' .Show
' If .SelectedItems.Count > 0 Then
' fPath = .SelectedItems(1) & "\"
' Exit Do
' Else
' If MsgBox("No folder has been chosen, do you wish to abort?", _
' vbYesNo) = vbYes Then Exit Sub
'End If
' End With
'Loop
FileToOpen = Application.GetOpenFilename("Excel Files (*.xls), *.xls, All Files (*.*), *.*", , "Look for your file below", "XLS", False)
'Application.GetOpenFilename ("Text Files (*.csv), *.csv")
If FileToOpen <> False Then
Set wbCSV = Workbooks.Open(FileToOpen & fCSV)
'insert col A and add CSV name
Columns(1).Insert xlShiftToRight
Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
'copy date into master sheet and close source file
ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
wbCSV.Close False
'ready next CSV
fCSV = Dir
End If
ErrorExit: 'Cleanup
ActiveSheet.Columns.AutoFit
Application.DisplayAlerts = True 'turn system alerts back on
Application.EnableEvents = True 'turn other macros back on
Application.ScreenUpdating = True 'refreshes the screen
End Sub