Creating, Loading, and using data from a 2-Dimensional Array - How

B

Bill

I used to write in VB quite a bit (back in the 90s) but haven't had occasion
to write anything in a while. Now I am trying to get my head back in the
game. After surmounting the loss of Application.FileSearch with six hours of
frustration until I found the Dir() command I thought I would ask a basic
question prior to jumping into the frustration again.

I have a set of files (143) that I have created using VB and three
templates. The files are all named cryptically, for example:

A26Checklist.doc
A27Studyoutline.doc
B01checklist.doc
B02checklist.doc
B03studyoutline.doc

What I would like to do is cycle through the 143 files in the folder, open
each one, take the first three characters of the document name, fill a
variable with a text string based on the first three characters, do a little
searching and replacing with the text string, and then close the document in
question.

I know how to cycle through the files using Dir(pathname & "*.doc). I know
how to use the Mid() command to get my text strings out of the doc.name. I
know how to do the searching and replacing once I have the text string in
variable. But for some reason my aging brain isn't getting the right code
for setting up and populating the array.

I tried to get around the question by just creating a giant "Select Case"
statement, but unfortunately VB won't let me do that inside of the
"For...Next" loop that I wanted to do to go through the Dir()'s.

The 3-character names and text strings all are unique:

A26 = "EmergencyResponse"
A27 = "Board Operation"
B01 = "Unit Overview"
B02 = "Unit Utilities"
B03 = "Tank Gauging"

This seems to be a natural two-dimensional array. The question is how do I
define it and populate it? Then how do I get VB to give me, for example,
"Tank Gauging" for the B03 code?

I did a lot of searching in the internet before I thought to come look into
Microsoft's online community. So perhaps I am just frustrated and should
read all the strings in here.

I would appreciate any help.
Bill Dannenmaier
 
S

Steve Rindsberg

I used to write in VB quite a bit (back in the 90s) but haven't had occasion
to write anything in a while. Now I am trying to get my head back in the
game. After surmounting the loss of Application.FileSearch with six hours of
frustration until I found the Dir() command I thought I would ask a basic
question prior to jumping into the frustration again.

I have a set of files (143) that I have created using VB and three
templates. The files are all named cryptically, for example:

A26Checklist.doc
A27Studyoutline.doc
B01checklist.doc
B02checklist.doc
B03studyoutline.doc

What I would like to do is cycle through the 143 files in the folder, open
each one, take the first three characters of the document name, fill a
variable with a text string based on the first three characters, do a little
searching and replacing with the text string, and then close the document in
question.

I know how to cycle through the files using Dir(pathname & "*.doc). I know
how to use the Mid() command to get my text strings out of the doc.name. I
know how to do the searching and replacing once I have the text string in
variable. But for some reason my aging brain isn't getting the right code
for setting up and populating the array.

I tried to get around the question by just creating a giant "Select Case"
statement, but unfortunately VB won't let me do that inside of the
"For...Next" loop that I wanted to do to go through the Dir()'s.

It shouldn't deny you that pleasure, but it's probably just as well that
something else stopped you. Generally, you want to enumerate files as quickly
as possible. I'm probably explaining this wrong, but conceptually, I think it
works about right: Dir$(filespec) sets up a kind of filter at the OS level,
Dir() returns the next matching file. If another app does the same thing while
yours is running, you might start getting results from its filter, not yours.
Ooops.

To hold the results you could use a dynamic array rather than a static one:

Dim arrayMyResults() as String

Have a look in Help for dynamic array, Redim (with the Preserve option) for more
info.

Basically, as Dir$/Dir returns each found file, you'd ReDim Preserve the array
to number of elements in the array + 1 and add the new filename to the array.

Then later you could iterate through the array with your Select Case:

Select Case Mid$(arrayMyResults(1,x),whatever,whatever) ' might want to Ucase

Case Is = "ABC"
arrayMyResults(2,x) = "Some value or other"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top