I have multiple files (fanspeedA, fanspeedB....) that do not have any file extensions and are pipe-delimited (or "|"). Could the lack of file extensions cause the VBA to lose its understanding of the delimited columns during copy paste?
Below is the VBA code I'm using, which performs the following steps:
1) Define variables, store the worksheet this macro was called from
2) Open a dialog to select files
3) Start a loop based on file selection, open each file with pipe delimiter "|"
- All files correctly open and are properly delimited with all columns recognized
4) While temporary file is open, copy used range and close file
5) Open original workbook, create new sheet based on temporary file's name, paste cells into this sheet
- Manual copy/paste at this point will retain the columns, but doing this step in VBA combines all columns into the first column
Sub loopyarray()
Dim filenames As Variant
' get current workbook name to cut/paste opened sheets into
Dim strBookName As Workbook, tmpBookName As String
Set strBookName = ThisWorkbook
' set the array to a variable and the True is for multi-select
filenames = Application.GetOpenFilename(, , , , True)
counter = 1
' ubound determines how many items in the array
While counter <= UBound(filenames)
' Opens the selected files
Workbooks.OpenText filenames(counter), 437, 1, xlDelimited, xlTextQualifierDoubleQuote, 0, 0, 0, 0, 0, 1, "|"
' Copy From Temporary Book
tmpBookName = ActiveSheet.Name 'save temporary sheet name
ActiveSheet.UsedRange.Select
Selection.Copy
ActiveWorkbook.Close
' Paste to Original Book
Windows(strBookName.Name).Activate 'activate original book
Worksheets.Add(Before:=Worksheets(1)).Name = tmpBookName 'new sheet based on temp sheet name
Range("A1").Select
ActiveSheet.Paste
' increment counter
counter = counter + 1
Wend
End Sub
Edit 1: Changed part of the sub, which is now throwing an error.
' Copy From Temporary Book
tmpBookName = ActiveSheet.Name 'save temporary sheet name
Dim rngCopy As Range
Set rngCopy = ActiveSheet.UsedRange
rngCopy.Copy
ActiveWorkbook.Close
' Paste to Original Book
Windows(strBookName.Name).Activate 'activate original book
Worksheets.Add(Before:=Worksheets(1)).Name = tmpBookName 'new sheet based on temp sheet name
Cells(1, 1).Paste ' THIS IS WHERE ERROR IS OCCURING