Error Message - Text file opened on Excel

lxd

Board Regular
Joined
Sep 15, 2010
Messages
79
My intention is to open a txt file in Excel, do text-to-column separation, then copy the data and paste on a template workbook the macro opens. The following macro does well upto parsing the data, but fails at copying the data. I get error message when it gets to that point. Two questions, how can I (i). successfully copy the data and paste it on the template, and (ii). close the first file (source file) without saving it.

I am using Excel 2007 and the macro is on the template. Thanks.

The code I have been using:

Private Sub OpenFile()
ChDir "C:\Documents and Settings\Desta\Desktop"
Workbooks.OpenText Filename:= _
"C:\Documents and Settings\Sun\Desktop\Drums.txt", Origin:=437, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False, Space:=True, Other:=True, OtherChar:=">", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)),TrailingMinusNumbers:=True

Cells.Select
Selection.Copy

Workbooks.Open Filename:= _
"C:\Documents and Settings\Sun\Desktop\Drums Template.xls", Origin:= xlWindows

ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False

Windows("Drums.xls").Close

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
Private Sub OpenFile()
Workbooks.OpenText Filename:= _
"C:\Documents and Settings\Sun\Desktop\Drums.txt", Origin:=437, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False, _
Space:=True, Other:=True, OtherChar:=">", FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), _
Array(12, 1), Array(13, 1)), TrailingMinusNumbers:=True
Cells.Copy
Workbooks.Open Filename:= _
"C:\Documents and Settings\Sun\Desktop\Drums Template.xls", Origin:=xlWindows
'How do you know that the correct area will be selected for pasting?
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Workbooks("Drums.txt").Close False 'I changed this to txt as I presume it is the text file you want to close
End Sub

If you tell us what the error message is, it would really help us in helping you.

Cheers

Dan<!-- / message -->
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,838
Members
449,597
Latest member
buikhanhsang

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top