VBA to import fixed width text file into Excel

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all, Hoping to find some assistance with a pesky task. Each week, I run a report that produced a text file. This task is going to be handed off to other individuals with limited Excel experience and need to automate as much as possible. I have been successful in prompting the user to open the file, but really running into issues with the actual import There are 17 columns with varying widths. I cannot seem to get this to work correctly.
The columns start at 1, 6, 8, 10, 18 (these are text) 32 (MDY) 41, 50 (General) 62, 71 (MDY) 80 (YMD) 87, 91, 93, 96 (Text), 100 (MDY) 109 (General).

Google found the below, which in theory would seem perfect, lets me enter the character where the break occurs along with the format, but I cannot get it to run without getting a Compile Error: Syntax error at the file location
VBA Code:
Const xlFixedWidth = 2




Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = True
objExcel.Workbooks.OpenText _
    “C:\Scripts\Test.txt”,,,xlFixedWidth,,,,,,,,,Array(Array(0,1),Array(14,1),Array(32,1)))

Any assistance would be most appreciated!!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The above code is meant for calling Excel from some other VBA environment (Word, Powerpoint, etc). For an Excel VBA macro you don't need this:

VBA Code:
Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = True

Have a look at this thread.
 
Upvote 1
Solution
Have a look at this thread.
Oh my gosh!! This is just PERFECT! I was able to edit the array's to the breaks I needed and use the formats that suited. THANK YOU SO MUCH!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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