Opening text file, fixed width macro?

RET79

Well-known Member
Joined
Mar 19, 2002
Messages
526
I get the format required for opening a text file in a table on an excel spreadsheet like this:

Category Field start Width
AGE 0 3
POL NO 3 6
DATE 9 6
PREM_TYPE 15 5
POL_TYPE 20 10
UNIT1 30 4
UNIT2 34 2
UNIT3 36 6
END_DATE 42 6
48

I get this code in my macro, which I would like to generalise and be updated automatically based on the table above:

Workbooks.OpenText Filename:="C:My Documentsfile.txt", Origin:=xlWindows _
, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, 1), Array(3, 1), Array(9, 1), Array(15, 1), Array(20, 1), Array(30, 1), Array(34, 1), Array(36, 1),Array(42, 1), Array(48, 1))

So I would like to get this code updated automatically for any given change in the column headings and widths given in the table on the excel spreadsheet. I just want to put the different column headings and widths in the spreadsheet, and just click a button to open the text file in exactly that format.

I would be grateful for any help. Thankyou!


--------------------------------------------------------------------------------
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Did anyone have a clue about this message?

I also wanted to point out that I have managed to set up a spreadsheet to generate the code required in the macro by manipulating text strings, IF functions etc.

However, I need to copy the code generated by the spreadsheet (which is just text in the range [D4:D8] say) and paste it over the old code in the module in the VBE everytime. Not a big deal that I know, but doesn't look very professional. Is there any way that you can have a macro where the macro itself 'reads' the text from the range [D4:D8] as code? That would cane so much.
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,299
Members
448,885
Latest member
LokiSonic

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