Hi!
First post here but I'm sure it wont be my last as automating results analysis in Excel is making my life 100000000x easier.
I'm not a complete novice with VBA but FAR from an expert with it.
I would really appriciate some help with the following. I'm trying to repeat a macro that opens a txt file, imports the data as comma delimited then copies to a specific sheet (RawData) in a known workbook/the main workbook. It needs to copy each set of data to the next empty cell in column A, starting at A1. This needs to be repeated for the amount of files selected initially in a dialogue box - it's usually no more than ten but sometimes less than that.
My current issues are:
The code for dataset 1 and dataset 2 (which is then just repeated another eight times = 10 datasets in total):
Thanks in advance!!
Chris
First post here but I'm sure it wont be my last as automating results analysis in Excel is making my life 100000000x easier.
I'm not a complete novice with VBA but FAR from an expert with it.
I would really appriciate some help with the following. I'm trying to repeat a macro that opens a txt file, imports the data as comma delimited then copies to a specific sheet (RawData) in a known workbook/the main workbook. It needs to copy each set of data to the next empty cell in column A, starting at A1. This needs to be repeated for the amount of files selected initially in a dialogue box - it's usually no more than ten but sometimes less than that.
My current issues are:
- I'm just repeating the same code again and again for the amount of files I want to process inside one sub - this means changing the code for different amounts of files
- I can't seem to get the first set of copied data into A1 using the same code for dataset 2 onwards. So I currently have code for dataset 1, then different, repeating code for dataset 2 onwards
- The code is taken from here and there and recorded user input so is probably more long winded than it needs to be.
- I cant seem to close down all the text files that get opened to copy data from as the With function to move to next empty cell in coloumn A ends up breaking/getting an error.
The code for dataset 1 and dataset 2 (which is then just repeated another eight times = 10 datasets in total):
VBA Code:
Sub ImportData()
'Delete All Current Data in RawData Sheet and load number One Data
Dim txtFileNameAndPath As String
Dim ImportingFileName As String
Dim SheetName As Worksheet
Dim fd As Office.FileDialog
Dim MainWB As String
MainWB = ActiveWorkbook.Name
Dim lst As Long
'Speeds Up Processing
Application.ScreenUpdating = False
'Suppresses Clipboard Prompt
Application.DisplayAlerts = False
'Deletes all data from RawData Sheet
Sheets("RawData").Select
Range("A1:I1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
'Enable this option if you want the use to be able to select multiple files
.AllowMultiSelect = False
'This sets the title of the dialog box.
.Title = "Please select number One Data file"
'Sets the associated filters for types of files
.Filters.Clear
.Filters.Add "TXT", "*.TXT"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtFileNameAndPath = .SelectedItems(1)
Else
MsgBox "Please start over. You must select a .txt file to import."
'You don't want the sub continuing if there wasn't a file selected
Exit Sub
End If
End With
'Extracts only the file name for reference later
ImportingFileName = Right(txtFileNameAndPath, _
Len(txtFileNameAndPath) - InStrRev(txtFileNameAndPath, "\"))
'Loads in user selected workbook from dialog box and sets it to delimited, space seperated data
Workbooks.OpenText Filename:=ImportingFileName _
, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), TrailingMinusNumbers:=True
'Selects full range of data to be copied
Range("A1:I1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'ActiveWorkbook.Close
Windows(MainWB).Activate
Sheets("RawData").Select
Range("A1").Select
ActiveSheet.Paste
'Load ESS Cycle Two Data
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
'Enable this option if you want the use to be able to select multiple files
.AllowMultiSelect = False
'This sets the title of the dialog box.
.Title = "Please select number Two Data file"
'Sets the associated filters for types of files
.Filters.Clear
.Filters.Add "TXT", "*.TXT"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtFileNameAndPath = .SelectedItems(1)
Else
MsgBox "Please start over. You must select a .txt file to import."
'You don't want the sub continuing if there wasn't a file selected
Exit Sub
End If
End With
'Extracts only the file name for reference later
ImportingFileName = Right(txtFileNameAndPath, _
Len(txtFileNameAndPath) - InStrRev(txtFileNameAndPath, "\"))
'Loads in user selected workbook from dialog box and sets it to delimited, space seperated data
Workbooks.OpenText Filename:=ImportingFileName _
, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), TrailingMinusNumbers:=True
'Selects full range of data to be copied
Range("A1:I1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'ActiveWorkbook.Close
Windows(MainWB).Activate
With Sheets("RawData")
lst = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & lst).PasteSpecial xlPasteValues
End With
End Sub
Thanks in advance!!
Chris