How to import txt file to a specific sheets in the samw workbook?

ChanL

Board Regular
Joined
Apr 8, 2021
Messages
65
Office Version
  1. 2019
Platform
  1. Windows
Hi there , I had write the following code to import txt file to excel without opening it.
However, the code bring the data to a new workbook which is not something I want.
What I want is to import the txt file into a sheet name "copy from text" within the same workbook "Data compilation"
Do advise how can I modify my VBA code. Thanks!

VBA Code:
Sub import()

dim fname as string

fname= Sheets("main").cells(2,3).value + "\" + "data.txt"

workbook.opentext filename:=fname, origin:=437,startrow:=1, datatype:=xlfixedwidth, textqualifier:=xldoublequote, _
consecutivedelimiter:= false, tab:=false, semicolon:=false, comma:= true, space:=false, other:= false, _
fieldinfo:= array(array(0,1),array(5,1),array(23,1),array(42,1),array(54,1),array(69,1), array(79,1), array(97,1), array(109,1), array(123,1)), _
trailingminusnumbers:=true

end sub 

/CODE]
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,​
from your actual code you can add a Range.Copy statement …​
For a direct import different ways exists.​
You can activate the Macro Recorder and use the Excel feature Import from file in the Data tab …​
 
Upvote 0
Hi,​
from your actual code you can add a Range.Copy statement …​
For a direct import different ways exists.​
You can activate the Macro Recorder and use the Excel feature Import from file in the Data tab …​
thanks!
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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