belinea2010
New Member
The code below is the result of recording a macro while importing a tab delimited text file via the import wizard but this method has a major downside in that it creates a new workbook and sheet in which to place the imported data. The code shows that the imported data is loaded into an array before it is outputted to the new (unwanted) workbook.
If a user then selects that data in the new workbook and copies it to the clipboard it can then be pasted in to any workbook and sheet the user chooses.
So my question is - using VBA can the result of the array be intercepted (or captured to the clipboard?) just before it writes the results to a new file and instead pasted from the clipboard into the worksheet of my choice as set within the VBA code?
I have looked at Power Query but I really don't think that is the way to go (unless someone can say otherwise).
In my very unqualified mind I muse that an array is virtual in that while data is in the array it exists as memory somehow and when the array output is created the data is then written to where ever the code states so is there a reason that data cannot be placed in the clip board or even better simply written to the workbook & worksheet from where the macro was called.
I am probably very wrong hence my question?
If a user then selects that data in the new workbook and copies it to the clipboard it can then be pasted in to any workbook and sheet the user chooses.
So my question is - using VBA can the result of the array be intercepted (or captured to the clipboard?) just before it writes the results to a new file and instead pasted from the clipboard into the worksheet of my choice as set within the VBA code?
I have looked at Power Query but I really don't think that is the way to go (unless someone can say otherwise).
In my very unqualified mind I muse that an array is virtual in that while data is in the array it exists as memory somehow and when the array output is created the data is then written to where ever the code states so is there a reason that data cannot be placed in the clip board or even better simply written to the workbook & worksheet from where the macro was called.
I am probably very wrong hence my question?
Code:
Sub ImportTXT()
Dim Answer As VbMsgBoxResult
Dim fDialog As FileDialog, result As Integer
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
'Optional: FileDialog properties
fDialog.AllowMultiSelect = False
fDialog.Title = "Select a file"
fDialog.InitialFileName = "F:\"
'Optional: Add filters
fDialog.Filters.Clear
fDialog.Filters.Add "Text/CSV files", "*.txt"
Answer = MsgBox("Are You Sure You Want To Import A Text File?", vbYesNo + vbCritical, "Import A Text File")
If Answer = vbYes Then
Application.ScreenUpdating = False
FName = Application.GetOpenFilename()
' ImportTXT code copied from Macro recording
Workbooks.OpenText FileName:=FName, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier _
:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:= _
False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array _
(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 9), Array(8 _
, 9), Array(9, 9), Array(10, 1), Array(11, 9), Array(12, 9), Array(13, 9), Array(14, 9), _
Array(15, 9), Array(16, 9), Array(17, 9), Array(18, 9), Array(19, 9), Array(20, 9), Array( _
21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 9)), TrailingMinusNumbers _
:=True
End If
End Sub