ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Constants as arrays? (https://www.excelbanter.com/excel-programming/426776-constants-arrays.html)

Charlotte E

Constants as arrays?
 
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

....but that doesn't allow me to run through the documents in a loop, like a
For-To-Next.


Any suggentions?


TIA,



Mike H

Constants as arrays?
 
Charlotte,

You could do something like this

Sub sonic()
Dim WordApp As Object, WordDoc As Object
Set WordApp = CreateObject("Word.Application")
mypath = "C:\"
MyArr = Array("Rules.doc", "Personal.doc", "Reporting.doc")
For x = LBound(MyArr) To UBound(MyArr)
WordApp.documents.Open mypath & MyArr(x)
WordApp.Visible = True
'do things
Next
End Sub


Mike

"Charlotte E" wrote:

Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

....but that doesn't allow me to run through the documents in a loop, like a
For-To-Next.


Any suggentions?


TIA,




Peter T

Constants as arrays?
 
Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T

"Charlotte E" <@ wrote in message
...
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

...but that doesn't allow me to run through the documents in a loop, like
a For-To-Next.


Any suggentions?


TIA,





keiji kounoike

Constants as arrays?
 
I might misunderstand the meaning of Charlotte's "To make Constants as
Arrays". But I can change elements of the constant array(arr). I wonder
this could be called as a constant array.


Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next

For i = 0 To UBound(arr)
arr(i) = Replace(UCase(arr(i)), "R", "A")
Next

For i = 0 To UBound(arr)
MsgBox arr(i), , "After Modifing For i = " & i
Next

End Sub

Keiji

Peter T wrote:
Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T

"Charlotte E" <@ wrote in message
...
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

...but that doesn't allow me to run through the documents in a loop, like
a For-To-Next.


Any suggentions?


TIA,





Peter T

Constants as arrays?
 
Constants can only refer to a value, not an array or object. The example I
posted makes a variable array from the constant string using the Split
function with the delimiter "\". After making the variable array from the
fixed constant you can of course change the array as you wish.

Regards,
Peter T


"keiji kounoike" <"kounoike AT mbh.nifty.com" wrote in message
...
I might misunderstand the meaning of Charlotte's "To make Constants as
Arrays". But I can change elements of the constant array(arr). I wonder
this could be called as a constant array.


Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next

For i = 0 To UBound(arr)
arr(i) = Replace(UCase(arr(i)), "R", "A")
Next

For i = 0 To UBound(arr)
MsgBox arr(i), , "After Modifing For i = " & i
Next

End Sub

Keiji

Peter T wrote:
Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T

"Charlotte E" <@ wrote in message
...
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

...but that doesn't allow me to run through the documents in a loop,
like
a For-To-Next.


Any suggentions?


TIA,







Charlotte E

Constants as arrays?
 
Oh, yeah...

This is the way to do it - thanks, Peter :-)


Peter T wrote:
Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T

"Charlotte E" <@ wrote in message
...
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

...but that doesn't allow me to run through the documents in a loop,
like a For-To-Next.


Any suggentions?


TIA,




keiji kounoike

Constants as arrays?
 
In my thought, if you say a variable is constant, you cannot change it's
value. In some other languages like C, you can make a constant array. In
VBA, it seems to be impossible to make a constant array. then, I wonder
why the poster needs the constants in this case. I think a code like
this would be enough.

Dim arr as Variant

Sub test()
arr = Array("Rules.DOC", "Personal.DOC", "Reporting.DOC")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next
End Sub

Keiji

Peter T wrote:
Constants can only refer to a value, not an array or object. The example I
posted makes a variable array from the constant string using the Split
function with the delimiter "\". After making the variable array from the
fixed constant you can of course change the array as you wish.

Regards,
Peter T


"keiji kounoike" <"kounoike AT mbh.nifty.com" wrote in message
...
I might misunderstand the meaning of Charlotte's "To make Constants as
Arrays". But I can change elements of the constant array(arr). I wonder
this could be called as a constant array.


Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next

For i = 0 To UBound(arr)
arr(i) = Replace(UCase(arr(i)), "R", "A")
Next

For i = 0 To UBound(arr)
MsgBox arr(i), , "After Modifing For i = " & i
Next

End Sub

Keiji

Peter T wrote:
Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T

"Charlotte E" <@ wrote in message
...
Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

...but that doesn't allow me to run through the documents in a loop,
like
a For-To-Next.


Any suggentions?


TIA,







All times are GMT +1. The time now is 05:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com