Text file not getting imported to excel

xsdip

New Member
Joined
May 21, 2019
Messages
28
I have used the below code to import textfile to excel. But there is a line break in my 6th line of textfile because of which the data below the 6th line in textpad is not importing to excel file. Can anyone please help on this.

With application.filedialog(msofiledialogfolderpicker)
.title ="select folder"
.allowmultiselect=false
Selectfolder = .selecteditems(1)& "\"
Opentextfile = dir(select folder)

Do while opentextfile <>"
Workbooks.opentext filename:=selectfolder & "" & opentextfile
Importworkbook = activeworkbook
Set Wsheet = Thisworkbook.worksheets("sheet1")
Importworkbook.worksheets(1).range("A1).currentregion.copy Wsheet.range("A1")
Importworkbook.close true
...
...

Thank you in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You just need to change how the macro is determining where the last row is.
Since the macro is assuming A1 has data in it anyway try using UsedRange.
Rich (BB code):
Importworkbook.worksheets(1).UsedRange.copy Wsheet.range("A1")
 
Upvote 0
You just need to change how the macro is determining where the last row is.
Since the macro is assuming A1 has data in it anyway try using UsedRange.
Rich (BB code):
Importworkbook.worksheets(1).UsedRange.copy Wsheet.range("A1")

That was of great help. Thank you so much

Could you please help me on one of my doubt??

How to import the textfile without a line break.

The 6th line is a blank line in my textfile. When I import it to excel, the 6th row is blank.
 
Upvote 0
You can add a lot more code to filter out all blank rows but if you are pretty sure it is always just row 6 you need to delete just add the code below prior to your copy the Sheet line.

Rich (BB code):
Dim blankRowNo As Long       ' <--- Can be moved to where your other Dim statements are
blankRowNo = 6                      ' <--- Change to blank row no
Importworkbook.Worksheets(1).Rows(blankRowNo).Delete

' Add before
Importworkbook.worksheets(1).UsedRange.copy Wsheet.range("A1")
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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