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

ChanL

New Member
Joined
Apr 8, 2021
Messages
45
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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,238
Office Version
  1. 2010
Platform
  1. Windows
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 …​
 

ChanL

New Member
Joined
Apr 8, 2021
Messages
45
Office Version
  1. 2019
Platform
  1. Windows
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!
 

Forum statistics

Threads
1,141,132
Messages
5,704,458
Members
421,351
Latest member
Sheogorath

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
Top