VB error

  • Thread starter Thread starter aw
  • Start date Start date
A

aw

Would like to know the correct statement that make the following VB works


Sub import01()

Dim hfile(1 To 8), i As Integer

hfile1 = "T:\Abc\Consolid\htmPL\PLY.HTM"
hfile2 = "T:\Abc\Consolid\htmPL\PLB.HTM"
hfile3 = "T:\Abc\Consolid\htmPL\PLL.HTM"
hfile4 = "T:\Abc\Consolid\htmPL\PLPP.HTM"
hfile5 = "T:\Abc\Consolid\htmPL\PLR.HTM"
hfile6 = "T:\Abc\Consolid\htmPL\PLT.HTM"
hfile7 = "T:\Abc\Consolid\htmPL\PLSMHK.HTM"
hfile8 = "T:\Abc\Consolid\htmPL\PLSMH.HTM"

For i = 1 To 8

Workbooks.Open Filename:=hfile(i)

... vb code
... vb code
... vb code


Next

End sub
 
Sub import01()

Dim hfile(7), i As Integer

hfile(0) = "T:\Abc\Consolid\htmPL\PLY.HTM"
hfile(1) = "T:\Abc\Consolid\htmPL\PLB.HTM"
hfile(2) = "T:\Abc\Consolid\htmPL\PLL.HTM"
hfile(3) = "T:\Abc\Consolid\htmPL\PLPP.HTM"
hfile(4) = "T:\Abc\Consolid\htmPL\PLR.HTM"
hfile(5) = "T:\Abc\Consolid\htmPL\PLT.HTM"
hfile(6) = "T:\Abc\Consolid\htmPL\PLSMHK.HTM"
hfile(7) = "T:\Abc\Consolid\htmPL\PLSMH.HTM"

For i = 0 To 7

Workbooks.Open Filename:=hfile(i)

'.. vb code
'.. vb code
'.. vb code

Next

End Sub

hth

Carlo
 
While the Dim statement 'carlo' showed you is one way to do it (although his
code assume the default Option Base of 0), your Dim statement is fine too.

I'm a little surprised you got the assignment statements wrong given you
used the array syntax correctly later on inside your For-Next loop. The key
to working with arrays comes from the structure of the Dim statement... the
array name followed by an opening parenthesis, followed by and index number
followed by a closing parenthesis... you use this structure for assignments
as well as for accessing the contents of a particular element of an array.
So, your assignment statements should look like this....

hfile(1) = "T:\Abc\Consolid\htmPL\PLY.HTM"
hfile(2) = "T:\Abc\Consolid\htmPL\PLB.HTM"
hfile(3) = "T:\Abc\Consolid\htmPL\PLL.HTM"
etc.

One thing you may want to consider though.... declaring your array with a
data type. As you wrote it, the array will be Variants... but since you are
assigning String values to the array, it would be more efficient to declare
the array as data type String....

Dim hfile(1 To 8) As String, i As Integer

Rick
 
Hey Rick

thanks for writing the additional Information.

Just a question though:

he was talking about VB, so does it really matter if he assigns a Data
Type or not?
I thought VB only works with Variants, regardless on which Data Type
you work with!
Please correct me if I'm wrong.

thanks

Carlo
 
VB, as does VBA, works with data types, it is VBScript that uses all
variants.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Sorry, my bad.

Thanks for telling me.

Carlo


VB, as does VBA, works with data types, it is VBScript that uses all
variants.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)









- Show quoted text -
 

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

Back
Top