Error while copying extracted text file data

krishna334

Active Member
Joined
May 22, 2009
Messages
391
Hi All,
I am trying to develop a code which extracts the data from text files inside a folder (Folder test in my desktop) into one sheet.
The Macro is in the workbook “Text Extract” which is an excel 2007 file. The data of each text should be copied to Sheet1 of this workbook one below the other.
For testing purpose I have kept only one text file in the folder and was trying to copy the data from the text data extracted sheet to Cell A1 of Sheet1 of workbook “Text Extract”.
The code works fine till copying the data, but shows below error in the line “Selection.Paste”:
Run time error 438: Object doesn’t support this property or method.
Can someone help me out.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Below is the code:
Sub LoopThroughFiles()
Dim strFile As String
Dim strPath As String
Dim colFiles As New Collection
Dim i As Integer
Dim k As Integer

strPath = "C:\Users\kkumar\Desktop\test\"
strFile = Dir(strPath)

While strFile <> ""
colFiles.Add strFile
strFile = Dir
Wend

'List filenames in Column A of the active sheet
If colFiles.Count > 0 Then
For i = 1 To colFiles.Count
Workbooks.OpenText Filename:="C:\Users\kkumar\Desktop\test\" & colFiles(i), _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:= _
Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 2), Array(6, 1), Array(7 _
, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 2), Array(12, 1), Array(13, 1), Array _
(14, 2), Array(15, 1), Array(16, 1)), TrailingMinusNumbers:=True

ActiveSheet.UsedRange.Copy
Windows("Text Extract.xlsm").Activate
Range("a1").Select
Selection.Paste
Next i

End If

End Sub
<o:p></o:p>
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Selection doesn't have a Paste method but it does have a PasteSpecial method.
 
Upvote 0
Thanks VoG. I got it.

Now, using your inputs i have made it loop though all the text files and also i have coded it to close the sheet from which the data is being copied but there is always a pop up window in which i need to select "No" option to move on to the text file.
How do i code so that selecting No is also automated Or how can i code so that this pop up does not appear?

Below is the pop up message:

"There is a large amount of information on the clipboard. do you want to be able to paste the information into another program later?"

In the options i ve yes and no and i need to select NO.


Sub LoopThroughFiles()
Dim strFile As String
Dim strPath As String
Dim colFiles As New Collection
Dim i As Integer
Dim k As Integer

strPath = "C:\Users\kkumar\Desktop\test\"
strFile = Dir(strPath)

While strFile <> ""
colFiles.Add strFile
strFile = Dir
Wend

'List filenames in Column A of the active sheet
If colFiles.Count > 0 Then

For i = 1 To colFiles.Count


Workbooks.OpenText Filename:="C:\Users\kkumar\Desktop\test\" & colFiles(i), _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:= _
Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 2), Array(6, 1), Array(7 _
, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 2), Array(12, 1), Array(13, 1), Array _
(14, 2), Array(15, 1), Array(16, 1)), TrailingMinusNumbers:=True

ActiveSheet.UsedRange.Copy
Windows("Text Extract.xlsm").Activate
k = ActiveSheet.UsedRange.Rows.Count + 1
Range("a" & k).Select
Selection.PasteSpecial Paste:=xlPasteAll
Windows("" & colFiles(i)).Close
Next i

End If

End Sub
 
Last edited:
Upvote 0
If you clear the clipboard you should not see that message. Try

Code:
Selection.PasteSpecial Paste:=xlPasteAll
Application.CutCopyMode = False
 
Upvote 0

Forum statistics

Threads
1,215,197
Messages
6,123,585
Members
449,108
Latest member
rache47

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