Array loops

RET79

Well-known Member
Joined
Mar 19, 2002
Messages
526
At work, I get a lot of textfiles that I need to open in a particular format in excel. I have to open them with fixed width, and the column widths, including the column headings are usually given to me in a seperate excel spreadsheet. I have recorded a macro to do this, but, it only works for a particular format. I get this type of code coming up which I wish to generalise:

Array(Array(0,1),Array(3,1),Array(4,1),.....)

I am given an excel spreadsheet with the values 0,3,4 etc. in column A of the spreadsheet. My wish is for the code above to read this in automatically, at present I have to go into the code and change the 0,3,4 to be the new format all the time which is a pain.

Also, I am given in column B, the column headings. It would be nice if those column headings could be placed in the top row of the resulting text file, I am sure this could be done with transposing the range in column B and inserting it in the top row of the text file.

But, as a general point, assume I have the following values in Range("a1:a5")

2,5,6,7,8

ok. now I sometimes want to loop using these values which is not a problem. But, sometimes, I need the index number to be used. i.e. I would like B(2)=5 and so on .
This ties into what I thought would work for the above. Ideally I would like to get something of this sort:

array(array(A(0),1), array(A(1),0), array(A(2),0),.....)

where A = Range("A1:A5") or something like that? Somethign along these lines woul dbe awesome. Especially so if we could get A to be a dynamic range.

Cool, now please try and help me.

Alternatively, at the moment what I have done is use the spreadsheet I have been given with the widths and column headings format and used them to generate the code on the spreadsheet by manipulating text strings etc. So I have th actual macro as text in Range("D4:D7") say. Is there a way that a macro in a module could actually 'read' the text in D4:D7 of this worksheet and exectute it? Wouldn't that be cool.

Thanks.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If you give an example of your current code, and what you would have in your range A1:A5, I think I can help you (and I'm sure that others could too).

Thank you,

-rh
 
Upvote 0
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!
 
Upvote 0
Ok, let's start with the values you use for your fields. You want the starting values, like you have in your second column, but you don't need the lengths. In the third column instead of the lengths, you should have the Data Type you want for the field. In your example, you have each field as General. The possible values are:<table border="1" width="220" style="border-style: solid"><tr><td width="100" align="center">1</td><td width="120">General</td></tr><tr><td width="100" align="center">2</td><td width="120">Text</td></tr><tr><td width="100" align="center">3</td><td width="120">MDY Date</td></tr><tr><td width="100" align="center">4</td><td width="120">DMY Date</td></tr><tr><td width="100" align="center">5</td><td width="120">YMD Date</td></tr><tr><td width="100" align="center">6</td><td width="120">MYD Date</td></tr><tr><td width="100" align="center">7</td><td width="120">DYM Date</td></tr><tr><td width="100" align="center">8</td><td width="120">YDM Date</td></tr><tr><td width="100" align="center">9</td><td width="120">Skip the column</td></tr></table>


Here is some code that I used to open a text file, hopefully it will give you some ideas. Happy Excelling,

Russell<pre><font color='#000000'><font color='#000080'>Option</font><font color='#000080'>Explicit</font><font color='#000080'>Sub</font> TestArray()<font color='#000080'>Dim</font> rng<font color='#000080'>As</font> Range<font color='#000080'>Dim</font> arr()<font color='#000080'>As</font><font color='#000080'>Integer</font><font color='#000080'>Dim</font> intRow<font color='#000080'>As</font><font color='#000080'>Integer</font><font color='#000080'>Dim</font> intCol<font color='#000080'>As</font><font color='#000080'>Integer</font><font color='#008000'>' Here are 3 examples of how to get your range:</font><font color='#000080'>Set</font> rng = Selection<font color='#000080'>Set</font> rng = Range("A1:B7")<font color='#000080'>Set</font> rng = Range("MyTextInfoRange")<font color='#000080'>If</font> rng.Columns.Count<> 2<font color='#000080'>Then</font>
MsgBox "Range must have exactly 2 columns - one for the " & _
"starting position of the field, and the other for " & _
"the data type"<font color='#000080'>Exit</font><font color='#000080'>Sub</font><font color='#000080'>End</font><font color='#000080'>If</font><font color='#000080'>ReDim</font> arr(1 To rng.Rows.Count, 1 To 2)<font color='#000080'>For</font> intRow = 1 To rng.Rows.Count<font color='#000080'>For</font> intCol = 1 To 2<font color='#008000'>'Or: For intCol = 1 to rng.Columns.Count</font>
arr(intRow, intCol) = rng(intRow, intCol)<font color='#000080'>Next</font> intCol<font color='#000080'>Next</font> intRow


Workbooks.OpenText FileName:="D:MyFile.txt", Origin:=xlWindows, _
StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=arr<font color='#000080'>End</font><font color='#000080'>Sub</font></font></pre>
This message was edited by Russell Hauf on 2002-03-21 13:51
This message was edited by Russell Hauf on 2002-03-21 13:52
This message was edited by Russell Hauf on 2002-03-21 14:05
This message was edited by Russell Hauf on 2002-03-21 14:05
 
Upvote 0
You're welcome - sorry about the big space everyone...fooling around with HTML (at which I am pretty much a rookie!).
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,645
Members
448,974
Latest member
DumbFinanceBro

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