Andyatwork
Board Regular
- Joined
- Mar 29, 2010
- Messages
- 94
Hello board,
I've been searching the internet for VB code I can hack together to allow a user to;
I managed to hack together elements of the code I found under FileDialog and the linked post but it is doing weird things and I don't know why.
I tested it on two CSV files, one of @38k rows and one of @180k rows. The macro looped until all million plus rows were full and then fell over.
I'm assuming there is something wrong with the Do...Loop
It also wraps each cell's data from the CSVs in quotes which I think I can fix with some sort of find/replace but if anyone knows a nifty bit of code to stop it from happening in the first place, that would be very helpful. However, that is a secondary concern to the fact it is endlessly pasting the contents of the selected files instead of just merging them all once.
I would very much appreciate someone casting an expert eye over the code and pointing out the obvious schoolboy error(s) I cannot see.
I'm aware that some declared variables don't get called or used, this is a work in progress.
My code:
I've been searching the internet for VB code I can hack together to allow a user to;
- select multiple CSV files (found something in the VB help resource for FILEDIALOG)
- automatically merge those files into a single worksheet while retaining a single header row (found this post http://www.mrexcel.com/forum/excel-...e-all-new-csvs-folder-into-one-worksheet.html )
- autofill some basic formula to the merged WS (date checking to determine age of a thing in the file, I think can manage this bit in VB)
- Auto pivot the results into a new WS (was just going to record doing it manually as I think that should be fairly straight forward)
- Finally prompt the user to Save As.. (which I also think I can manage)
I managed to hack together elements of the code I found under FileDialog and the linked post but it is doing weird things and I don't know why.
I tested it on two CSV files, one of @38k rows and one of @180k rows. The macro looped until all million plus rows were full and then fell over.
I'm assuming there is something wrong with the Do...Loop
It also wraps each cell's data from the CSVs in quotes which I think I can fix with some sort of find/replace but if anyone knows a nifty bit of code to stop it from happening in the first place, that would be very helpful. However, that is a secondary concern to the fact it is endlessly pasting the contents of the selected files instead of just merging them all once.
I would very much appreciate someone casting an expert eye over the code and pointing out the obvious schoolboy error(s) I cannot see.
I'm aware that some declared variables don't get called or used, this is a work in progress.
My code:
Code:
Sub pick_and_merge()
Dim strSourcePath As String
Dim strDestPath As String
Dim strFile As String
Dim strData As String
Dim x As Variant
Dim Cnt As Long
Dim r As Long
Dim c As Long
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
Do While Len(vrtSelectedItem) > 0
Cnt = Cnt + 1
If Cnt = 1 Then
r = 1
Else
r = Cells(Rows.Count, "A").End(xlUp).Row + 1
End If
Open vrtSelectedItem For Input As #1
If Cnt > 1 Then
Line Input #1, strData
End If
Do Until EOF(1)
Line Input #1, strData
x = Split(strData, ",")
For c = 0 To UBound(x)
Cells(r, c + 1).Value = Trim(x(c))
Next c
r = r + 1
Loop
Close #1
' Name strSourcePath & strFile As strDestPath & strFile
' strFile = Dir
Loop
Application.ScreenUpdating = True
If Cnt = 0 Then _
MsgBox "No CSV files were found...", vbExclamation
Next vrtSelectedItem
End If
End With
End Sub