Vba excel массивы констант

Is it possible to either:

  1. Declare an array as a constant

    OR

  2. Use a workaround to declare an array that is protected from adding, deleting or changing elements, and therefore functionally constant during the life of a macro?

Of course I could do this:

Const myConstant1 As Integer = 2
Const myConstant2 As Integer = 13
Const myConstant3 As Integer = 17
Const myConstant4 ...and so on

…but it loses the elegance of working with arrays. I could also load the constants into an array, and reload them each time I use them, but any failure to reload the array with those constant values before use could expose the code to a «constant» value that has changed.

Any workable answer is welcome but the ideal answer is one that can be setup once and not require any changes/maintenance when other code is modified.

Zoe stands with Ukraine's user avatar

asked Dec 8, 2016 at 18:15

ChrisB's user avatar

You could use a function to return the array and use the function as an array.

Function ContantArray()
    ContantArray = Array(2, 13, 17)
End Function

enter image description here

enter image description here

answered Dec 8, 2016 at 18:32

11

How about making it a function? Such as:

Public Function myConstant(ByVal idx As Integer) As Integer
    myConstant = Array(2, 13, 17, 23)(idx - 1)
End Function

Sub Test()
    Debug.Print myConstant(1)
    Debug.Print myConstant(2)
    Debug.Print myConstant(3)
    Debug.Print myConstant(4)
End Sub

Nobody can change it, resize it, or edit its content… Moreover, you can define your constants on just one line!

answered Dec 8, 2016 at 18:22

A.S.H's user avatar

A.S.HA.S.H

29k5 gold badges22 silver badges49 bronze badges

I declared a String constant of "1,2,3,4,5" and then used Split to create a new array, like so:

Public Const myArray = "1,2,3,4,5"

Public Sub createArray()

        Dim i As Integer
        A = Split(myArray, ",")

        For i = LBound(A) To UBound(A)
                Debug.Print A(i)
        Next i

End Sub

When I tried to use ReDim or ReDim Preserve on A it did not let me. The downfall of this method is that you can still edit the values of the array, even if you can’t change the size.

answered Dec 8, 2016 at 18:21

Soulfire's user avatar

SoulfireSoulfire

4,21821 silver badges32 bronze badges

2

If the specific VBA environment is Excel-VBA then a nice syntax is available from the Excel Application’s Evaluate method which can be shortened to just square brackets.

Look at this

Sub XlSerialization1()
    Dim v
    v = [{1,2;"foo",4.5}]

    Debug.Assert v(1, 1) = 1
    Debug.Assert v(1, 2) = 2
    Debug.Assert v(2, 1) = "foo"
    Debug.Assert v(2, 2) = 4.5

    '* write all cells in one line
    Sheet1.Cells(1, 1).Resize(2, 2).Value2 = v
End Sub

answered Apr 17, 2018 at 22:43

S Meaden's user avatar

S MeadenS Meaden

8,0313 gold badges31 silver badges65 bronze badges

2

If you don’t need a new instance each time you can use a Static local variable to avoid multiple objects creation and initialization:

Private Function MyConstants()
    Static constants As Variant

    If IsEmpty(constants) Then
        constants = Array(2, 13, 17)
    End If

    MyConstants = constants
End Function

answered Jan 30, 2019 at 18:52

Pragmateek's user avatar

PragmateekPragmateek

13.1k9 gold badges73 silver badges108 bronze badges

Can an array be declared as a constant? No.

Workarounds — Simplest one I can think of is to define a constant with delim and then use Split function to create an array.

Const myConstant = "2,13,17"

Sub Test()
    i = Split(myConstant, ",")

    For j = LBound(i) To UBound(i)
        Debug.Print i(j)
    Next
End Sub

answered Dec 8, 2016 at 18:20

Pankaj Jaju's user avatar

Pankaj JajuPankaj Jaju

5,3212 gold badges25 silver badges41 bronze badges

Is this too simplistic?

PUBLIC CONST MyArray = "1,2,3,4"

then later in a module:

Dim Arr as Variant

SET Arr = split(MyArray,",")

DaveyDaveDave's user avatar

answered Mar 3, 2019 at 21:29

Webrohm's user avatar

1

I know this is an old question, but these archives are often scanned for many years after being posted, so I don’t see a problem with adding things long after the origin date.

How about creating a class, with a read-only property returning the ‘array’ value? You can specify a parameter using the same syntax as an array index, and defining only a GET property effectively makes it read-only. Define the constant values inside the class and it will work just like a constant array, even though the actual construction is different.

answered Mar 21, 2021 at 23:17

pdanes's user avatar

Using above information, I came to following working solution for comparing short text information of a month, independent from Excel using German language:

Const MONATE = «,Jän,Feb,März,Apr,Mai,Jun,Jul,Aug,Sep,Okt,Nov,Dez»

.. and later in the code:

    If StringToCompare = Split(MONATE, ",")(Month(dt)) Then

NOTE: as the Split-Array starts with index 0 I added the comma in the beginning.

answered Oct 4, 2021 at 18:42

CSohu's user avatar

CSohuCSohu

111 bronze badge

Don’t know when this changed, but in Excel 365, this works (or, at least, does not generate a compiler error):

Const table1Defs As Variant = Array("value 1", 42, Range("A1:D20"))

z32a7ul's user avatar

z32a7ul

3,2423 gold badges20 silver badges42 bronze badges

answered Sep 4, 2019 at 16:22

BillR's user avatar

2

dk

MrExcel MVP


  • #2

I’d be very suprised if you could do this. You’d be better off using a variable array, or a custom function which returns the number of days in a given month.

  • #3

I need to revisit this question. There is a need for arrays of constants. I need to add some cells to a worksheet to show average, min, and max. The worksheet is quite asymetrical and the columns to be evaluated are not definable. Here is what I have been able to do as a workaround.

Code:

Dim Evaluation_Array As Variant
Evaluation_Array = Array(3, 4, 5, 6, 8, 11, 15)

For Each Sum_Column_Number In Evaluation_Array

   Call Build_Evaluation_Cell(My_Worksheet, _
                              Sum_Column_Number, _
                              Sumation_Row, _
                              Top_Sumation, _
                              Bottom_Sumation)
Next

The subroutine (in part) adds a summing cell in a row for each of the columns specified. There is more to this, but I hope the code extract above will convey the general concept.

Evaluation_Array really should be an integer, not a variant, as should be Sum_Column_Number. The use of variant requires that the subroutine be declared with a variant rather than an integer.

The original post is approaching five years old now. Can an array of constants be declared?

  • #4

I don’t understand why you would need to do this.

What is the difference between defining this array as a constant, and just defining it by hardcoding the values in to be compiled at run time?

It makes no difference as far as I can see…

You can have the array as an integer, there is no problem there either…. so long as you know the array will be a predetermined length.

e.g.

Code:

option base 1
dim myarray(3) as integer
myarray(1) = 3 : myarray(2) = 4: myarray(3) = 5

Even if you don’t know it would be a pre-determined length, you can use the

keyword to extend the array as and when required.

Am I missing something here?

  • #5

Then again, why do you need the array of days in a month at all? Couldn’t you just use:

Code:

Dim monthnum As Integer
monthnum = 2
MsgBox Day(DateSerial(2006, monthnum + 1, 0))

or similar?

  • #6

Question: Why do we need to be able to declare an array of constants?

First, for the same reason that we need constants, we need an array of constants. And for all of those reasons. I don’t want to build it a run time, I want it predefined at compile time. I have some deadlines and don’t have time to entertain that discussion right now. Get a few books on software engineering and read about software design and constants. Read about writing software for flexibility, robustness and maintainability.

Please bear in mind that example code posted here is often a vehicle used to convey a complex idea. The simplest possible code segment that can convey the question is posed, the often huge amount of complexity behind it is deliberatly omitted. Because the quesion is simple does not mean that the needs are simple.

For now I take it that there is no declaration of constant arrays and will move on. I appreciate the time you took to respond and will leave this thread monitored.

  • #7

It’s not exactly the an array when it’s set as a constant, but the below has similar effects to what you ask:

Code:

Const z As String = "1, 2, 3, 4, 5, 6, 7"

Sub Test()
    x = Split(z, ",")
    For y = LBound(x) To UBound(x)
        MsgBox x(y)
    Next
End Sub

  • #8

First, for the same reason that we need constants, we need an array of constants. And for all of those reasons. I don’t want to build it a run time, I want it predefined at compile time. I have some deadlines and don’t have time to entertain that discussion right now. Get a few books on software engineering and read about software design and constants. Read about writing software for flexibility, robustness and maintainability.

I read your response with interest.

First and foremost, this is a discussion forum where people offer help to others for free. I am not being paid to sit here and read your posts, I do it for the love of helping people out.

If you read many of the posts on this forum, as I have, you will notice a trend with posts. That trend is that people often ask questions, under the presumption that the question they pose will help them fix their solution. All to often, however, it emerges that if more detail is given on the actual problem (as opposed to the solution they believe will fix it), other caveats to the problem can be unearthed that lead to the conclusion that the OP’s question can actually be reposed in another form, leading to an answer that satisfies what is the orginal issue.

I stated I did not understand why you would want an array of constants. To this point, I still don’t understand your intentions. If your motivation was made clearer, then as you say, we could cut through this whole portion of analysis and simply offer you solutions that achieve what you want.

I have to take significant issue with your tone. You know nothing about anyones software development history on this board. As it happens I am a graduate in Computer Science from a top UK university. I do not need your patronising advice on getting some ‘books on software design’. I am very aware of software design principles. I work as a software designer here in London, with Java and C++, as well as a little dabbling in VBA. I have answered numerous posts on this forum with no complaints from the OP’s who have got things working as a result. You cannot declare a constant array in VBA. I could have left it there, but decided to try and explore your problem and offer alternative solutions that could act as a workaround for you. Sure constants are only evaluated once as opposed to everytime a variable is used. Yes, there is an overhead to that, but if its important as you say it is, you can use a variable to hold the array, and then use some (now its my time to get patronising) common sense to leave the array alone at run time, and therefore protect its values.

Furthermore, with this high and mighty attitude you seem to have, I would expect you would get little help from the people on this board. This is a community that involves mutual respect, and a culture of helpfulness and learning. Both myself, and Oaktree (a Microsoft most valued professional) have taken the time out of our lives to attempt to address your problem, to try and talk you through your thought process so as to ascertain not only what you want, but why you want it as well. Frankly I find your consequent posts abhorrently patronising and full of negativity.

My advice — change your attitude, and fast, else you’ll find yourself going down in a very negative fashion not only on this medium, but in life in general.

Good day to you.

  • #9

Hi bkelly

1. I agree with you. We should be able to define constant arrays. Whenever the language allows it I define most of the constants at the beginning of the modules, outside the routines.

2 . It is not true what you say: «The use of variant requires that the subroutine be declared with a variant rather than an integer.»

You can (should) use a type conversion function and define the subroutine properly.

Try this:

Code:

Sub ConvType()
Dim v As Variant

    v = 2
    Call paramtype(CInt(v))
End Sub

Sub paramtype(i As Integer)

    MsgBox i & ", " & TypeName(i)
End Sub

3. You can use a workaround like BJungheim’s. This will let you define the constant array at the beginning of the module

Code:

Const sArr As String = "1,3,5,7"

Sub a()
Dim v

For Each v In Split(sArr, ",")
    MsgBox CInt(v)
Next
End Sub

4 — It might be better if along with a syntax question you would explain your problem.
There may be other ways of doing what you want that may be more efficient and easier to implement.

In this case, although I don’t have enough information about your problem, I got the feeling that maybe you could use a multiarea range.
If you had explained your problem better, I might be able suggest it or understand that it makes no sense

5. Last but not least:

I have some deadlines and don’t have time to entertain that discussion right now. Get a few books on software engineering and read about software design and constants. Read about writing software for flexibility, robustness and maintainability.

Let me tell you that what you wrote is very unpolite and even a bit aggressive.
As you know we all try to help each other in this board. The attitude from one member towards the others is expected to be polite, even friendly.
An attitude like the one you displayed, if meant, is not welcome.

Best regards
PGC

  • #10

Hi pcg,

I just want to point out that it’s Bryan Kelly who is being so rude, and not Hedges.

vashin

0 / 0 / 0

Регистрация: 21.07.2007

Сообщений: 47

1

Как задать массив констант

09.01.2008, 19:51. Показов 19918. Ответов 11

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Здравствуйте!
————-
У меня такая проблема. Я не знаю как задать массив констант с присвоением значений.

Хочу получить примерно следующее:

Visual Basic
1
массив Chisl = ('один','два','три','четыре','пять','шесть')

но не хочу задавать каждый эл-т массива отдельно, например

Visual Basic
1
2
3
4
5
Chisl(1)='один'
Chisl(2)='два'
.
.
Chisl(6)='шесть'

Заранее благодарен. =Миша



0



sharig

2 / 2 / 1

Регистрация: 30.07.2007

Сообщений: 206

11.01.2008, 04:27

2

Для версии VB 6.0.
тогда создай файл на диске, в который впиши все значения твоих констант, а дальше… используй пример ‘записи данных из файла на диске в массив’, который в скором времени будет размещен на сайте VBcode.FAQ (http://sharig.webzone.ru)
Необходимое пояснение: про использование функции Split можно прочитать здесь — http://sharig.webzone.ru/global/pages/Function.htm
В случае следующего примера, впиши все свои константы в файл через абзац:

Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Private Sub Command1_Click()
Dim txt ' As Long
Dim FN As Integer
FN = FreeFile
Dim FName As String
FName = 'D:1.txt'
Open FName For Input As #FN
txt = Input(LOF(FN), #FN)
Close #FN
Dim str1() As String
str1 = Split(txt, vbCrLf)
For i = 0 To UBound(str1()) - 1
MsgBox str1(i)
Next
End Sub



0



CyberUser

4 / 4 / 2

Регистрация: 15.06.2012

Сообщений: 18

15.06.2012, 15:19

3

Цитата
Сообщение от vashin
Посмотреть сообщение

У меня такая проблема. Я не знаю как задать массив констант с присвоением значений.

Visual Basic
1
2
3
4
5
6
7
8
Option Base 1
DIM Chisl As Variant
.
.
Chisl=Array ("один","два","три","четыре","пять","шесть")
.
.
Form1.Print Chisl(6)



0



Апострофф

Заблокирован

15.06.2012, 15:25

4

CyberUser, Вам не кажется, что Ваш ответ несколько запоздал[/OFF]



0



4 / 4 / 2

Регистрация: 15.06.2012

Сообщений: 18

15.06.2012, 15:39

5

Цитата
Сообщение от Апострофф
Посмотреть сообщение

CyberUser, Вам не кажется, что Ваш ответ несколько запоздал[/OFF]

Да, Миша уже наверное уже с бородой и служит где-нибудь сисадмином.
Но я сам только что озадачился такой проблемой, гугл вывел на эту ветку. Я понял, что тема не раскрыта.
Немного подумав решил, что мое решение будет оптимально. Ну и расшарил идею… Не создавать же новую тему.



0



Эксперт Hardware

3139 / 1907 / 323

Регистрация: 25.10.2011

Сообщений: 5,541

15.06.2012, 15:57

6

Насколько я знаю в вб массива констант нет, а жаль.
Есть одиночная константа или глобальный массив, как в примере.



0



Catstail

Модератор

Эксперт функциональных языков программированияЭксперт Python

34706 / 19227 / 4039

Регистрация: 12.02.2012

Сообщений: 32,183

Записей в блоге: 13

15.06.2012, 16:53

7

Да вот так:

Visual Basic
1
arr=Array("раз","два","три","четыре")  ' и т.д.

Только помни, что «раз» будет иметь индекс 0. А если так:

Visual Basic
1
arr=Array("","раз","два","три","четыре")  ' и т.д.

то «раз» будет иметь индекс 1



0



es geht mir gut

11264 / 4746 / 1183

Регистрация: 27.07.2011

Сообщений: 11,437

15.06.2012, 16:58

8

Цитата
Сообщение от CyberUser
Посмотреть сообщение

Option Base 1

Catstail, внимательнее.



0



Модератор

Эксперт функциональных языков программированияЭксперт Python

34706 / 19227 / 4039

Регистрация: 12.02.2012

Сообщений: 32,183

Записей в блоге: 13

15.06.2012, 17:02

9

А я не использую Option Base… Имею право. И это не вопрос внимания/невнимания.



0



4 / 4 / 2

Регистрация: 15.06.2012

Сообщений: 18

16.06.2012, 00:26

10

Не спорьте мальчики.
Такие мелочи вылазят на первом же тестовом прогоне и сразу правятся.



0



Модератор

Эксперт функциональных языков программированияЭксперт Python

34706 / 19227 / 4039

Регистрация: 12.02.2012

Сообщений: 32,183

Записей в блоге: 13

16.06.2012, 13:27

11

Поучи, поучи…



0



Почетный модератор

21371 / 9105 / 1082

Регистрация: 11.04.2010

Сообщений: 11,014

16.06.2012, 21:06

12

Ладно тему подняли 2001 года, но флудить в ней — это уже через край.
Закрыто



1



13

Dim Количество As Byte, Вес As Single

При вводе кода программы с клавиатуры среда программирования оказывает помощь пользователю – после набора ключевого слова As и пробела раскрывается список, в котором наряду с другими типами объектов указаны базовые типы переменных:

Тип переменной можно установить, щелкнув дважды по имени типа в этом списке.

Неявное объявление

В этом способе определения переменных никакие инструкции для объявления переменной вообще не используются. Когда транслятор VBA первый раз встречает в коде программы необъявленную переменную, то он назначает ей тип Variant, и переменная может принимать значение любого типа. Следовательно, в программе VBA, в принципе, можно вообще не использовать инструкцию Dim. Тем не менее использование типа Variant рекомендуется использовать только в следующих случаях:

для ускорения процесса отладки программы;

пользователь не уверен, какой тип данных обрабатывается инструкцией в конкретной ситуации.

Явное объявление переменных позволяет:

увеличить скорость решения задачи;

оптимизировать распределение памяти для переменных;

документировать переменные и избежать ошибок при использовании одной переменной в принципиально различных ситуациях.

Для того чтобы избежать неприятностей в случае ошибочной записи имени переменной, необходимо в общей области программного модуля помещать оператор Option Explicit. В этом случае Visual Basic будет расценивать любую неявно объявленную переменную как ошибочную.

Константа по определению является постоянной величиной и поэтому не меняет своего значения при выполнении программы. Константы в VBA подразделяются на константы, определяемые пользователем, и встроенные константы. Встроенная константа должна объявляться; при этом используется оператор Const, аналогичный оператору Dim:

Const <ИмяКонстанты> [As <ТипДанных>] = <Выражение>

14

где <Выражение> – это любое значение или формула, возвращающая значение, которое должно использоваться в качестве константы.

Пример

Const ПИ As Single = 3,14

Встроенные константы используются обычно при работе с объектами приложения. Эти константы не требуют предварительного объявления. Имена встроенных констант начинаются с префикса, который указывает, к объекту какого приложения Microsoft Office они относятся. Например, встроенные константы объектов Excel имеют префикс xl, встроенные константы языка VBA имеют префикс vb и т.д. Например, встроенные константы vbYes и vbNo используются в функции MsgBox, предназначенной для вывода данных на экран.

Операторы объявления переменных и констант можно размещать в любом месте программного кода, но обязательно до первого оператора,

в котором переменная или пользовательская константа применяется. Хороший стиль программирования предполагает, что объявление переменных и констант осуществляется в начале процедуры.

2.4. Строковые переменные

Различают строки переменной и фиксированной длины. Строки переменной длины могут содержать до двух миллиардов символов; их размер заранее не определяется. Когда такой переменной присваивается значение, то размер переменной изменяется так, чтобы он соответствовал длине присвоенного строкового значения.

Строка фиксированной длины – это строка постоянного размера, указанного при объявлении переменной. Если такой строке присваивается значение более длинное, то лишние символы отбрасываются. Если значение, которое присваивается, короче, то остающееся место заполняется пробелами. Строковые переменные фиксированной длины должны объявляться явно.

Синтаксис объявления строковой переменной следующий: Dim VarName As String [* ДлинаСтроки]

где ДлинаСтроки – целочисленная переменная или константа, содержащая число, которое указывает длину строковой переменной.

Например:

Dim strMyName As String* 20

Объявляется строковая переменная фиксированной длины в 20 символов

Dim intLen As Integer Intlen = 10

Dim MyName As String * Intlen

Объявляется строковая переменная длиной в 10 символов.

15

2.5. Массивы

представляет собой структуру, все элементы которой имеют одинаковый тип. Массивы могут быть одномерными и многомерными. Так, для отображения отдельного столбца или отдельной строки таблицы, содержащей данные одинакового типа, может быть использован одномерный массив, для отображения таблицы – двумерный, а для отображения совокупности таблиц – трёхмерный массив. Количество размерностей массива может достигать 60.

В VBA массив объявляется следующим образом:

[Public | Private] Dim ИмяМассива([Индексы]) [As ТипДанных]

где

ИмяМассива – идентификатор, определяющий имя массива; Индекс – значение номера элемента в размерности массива, зада-

ваемого одним из двух способов:

1.указанием номера последнего элемента в каждой размерности массива;

2.указанием номеров первого и последнего элемента в каждой размерности массива.

Примечание

Номера элементов (индексы) должны быть указаны целым числом.

При использовании первого метода первому элементу по умолчанию присваивается номер “ноль”. Например, объявление одномерного массива Товар из трёх элементов, каждый из которых состоит не более чем из 15 символов, по умолчанию выглядит следующим образом:

Dim Товар(2) As String*15

Для обращения к элементам массива необходимо поместить в круглых скобках за идентификатором массива целочисленное выражение, например, Товар(0), Товар(К*2) – для одномерного массива и Продажи(I,L), Продажи(3,4) ) – для двумерного массива; величины I, K, L должны быть целыми.

В приведённой ниже процедуре ОбъявлениеМассива1 Sub ОбъявлениеМассива1()

Dim Товар(2) As String*15

Товар(0) = «Стул мягкий» MsgBox Товар(0)

End Sub

выражения Товар(0), Товар(1), Товар(2) являются элементами массива;

инструкция Товар(0) = «Стул мягкий» присваивает первому элементу массива Товар значение текстовой константы «Стул мягкий»;

функция MsgBox Товар(0) выводит значение этого элемента на экран:

16

Примечание

Инструкция присваивания и функция MsgBox будут подробно рассмотрены ниже.

Для изменения начала нумерации индексов массива можно поступить одним из следующих способов.

1. Использовать инструкцию Option Base 1. При этом инструкция Option Base 1 должна находиться в самом начале модуля VBA, перед первой процедурой модуля, например,

Option Base 1

Sub ОбъявлениеМассива2()

Dim Товар(3) As String * 15

Товар(1) = «Стул мягкий» MsgBox Товар(1)

End Sub

По умолчанию инструкция Option Base имеет значение 0 (ноль).

2.Выполнить явное указание номера первого элемента в каждой размерности массива, например,

Sub ОбъявлениеМассива3()

Dim Товар(1 To 3) As String * 15

Товар(1) = «Стул мягкий» MsgBox Товар(1)

End Sub

При объявлении многомерного массива в поле индекса указывается несколько индексов, в соответствии с размерностью массива: номер последней строки, номер последнего столбца и номер последней таблицы – для трёхмерного массива. Например, двумерный массив из пяти строк и десяти столбцов объявляется без использования инструкции Option Base 1 одним из следующих способов:

Dim Продажи(4,9) As String

или

Dim Продажи(0 To 4,0 To 9) As String

17

Примечания

1.Наличие инструкции Option Base 1 не влияет на индексацию элементов массива, если в его объявлении используется конструкция

<нижняя граница> To <верхняя граница>

Массив Продажи можно объявить так: Dim Продажи(1 To 5, 1 To 10) As String

2.Элементы массива Продажи имеют тип строковая переменная неопределённой длины.

Выше рассмотрено объявление массивов, связанных с обработкой только статических данных, т.е. таких, память под которые выделяется во время компиляции и сохраняется в течение всей работы программы.

В VBA существует другой способ выделения памяти под массивы, который называется динамическим, в котором память под массивы отводится и может быть перераспределена во время выполнения программы. Такие массивы называются динамическими.

Использование динамических величин предоставляет возможность подключать память динамически, что позволяет:

увеличить объем обрабатываемых данных;

если потребность в каких-то данных отпала до окончания программы, то занятую ими память можно освободить для другой информации;

определять динамически изменяющееся количество реально существующих элементов массива.

Динамический массив объявляется следующим образом:

[Public | Private] Dim ИмяМассива() [As ТипДанных]

т.е. размерность динамического массива в его объявлении не указывает-

ся.

Пример

Dim Товар() As String * 15

Перед использованием динамического массива необходимо переопределить его размеры. Это выполняется при помощи инструкции ReDim, используемой на уровне процедуры:

ReDim [Preserve] ИмяМассива(Индексы) [As ТипДанных] Параметр Preserve используется для сохранения данных в сущест-

вующем массиве при изменении размерности. В противном случае все старые значения элементов массива будут удалены. Однако если новый размер массива меньше, чем количество помещенных в него элементов, часть данных будет потеряна.

Примеры

ReDim Товар(1 To 3) As String * 15

ReDim Preserve Товар(1 To 10) As String * 15

18

При использовании параметра Preserve можно изменить размер только последней размерности, а количество размерностей изменить нельзя.

Массивы типа Variant можно создавать и заполнять одновременно при помощи встроенной функции Array(<СписокЗначений>), например:

Dim Товар As Variant

Товар = Array(“Стул”, “Стол”, “Шкаф”)

Освободить память, занимаемую элементами динамического массива, и повторно инициализировать элементы массива фиксированной длины можно инструкцией Erase:

Erase СписокМассивов

Здесь СписокМассивов – один массив или список разделенных запятыми массивов. Инициализация элементов массива фиксированной длины осуществляется следующим образом:

Тип массива

Действие инструкции Erase

Фиксированный числовой массив

Устанавливает каждый элемент

в нуль

Фиксированный массив строк

Устанавливает каждый элемент

(переменной длины)

на нулевую длину строки («»)

Фиксированный массив строк

Устанавливает каждый элемент

(фиксированной длины)

в нуль

Фиксированный массив типа Variant

Устанавливает каждый элемент

в Empty

Массив данных определенных

Устанавливает каждый элемент, как

пользователем типов

будто это отдельная переменная

Массив объектов

Устанавливает каждый элемент

в величину Nothing

Если количество элементов в динамическом массиве не известно, то для его определения используется функция

UBound (ИмяМассива [, Измерение])

Необязательный параметр Измерение имеет тип Variant или Long. Это целое число, указывающее размерность, для которой определяется верхняя граница индекса. Для первой размерности используется число 1, для второй – 2 и т.д. если аргумент не указывается, по умолчанию используется значение 1.

Пример

Dim Товар(1 To 3) As String * 15 L=UBound (Товар)

Переменная L получит значение 3.

Функция UBound обычно используется с функцией LBound для определения размера массива, позволяющей найти наименьшее значение индекса указанной размерности. Формат функции:

LBound (ИмяМассива [, Измерение])

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]

  • #
  • #
  • #
  • #
  • #
  • #
  • #
  • #
  • #
  • #
  • #
  1. 10-30-2004, 05:54 AM


    #1

    Solved: Declare a Constant Array

    Hi,

    Can I declare an array in a similar fashion to a constant so that the data can be used by separate elements of a userform and sent to a function for processing. I don’t want to put them on the Userform itself, if I can avoid it.
    Something like the following
    [VBA] Cols = Array(«A», «D», «G», «H»)

    Sub Test1()
    ‘Cols = Array(«A», «D», «G», «H»)
    Range(Cols(0) & «3»).Select
    End Sub

    Sub test2()
    Dim Cols()
    ‘Cols = Array(«A», «D», «G», «H»)
    For Each gc In GetCols(Cols)
    msg = msg & gc & «, «
    Next
    MsgBox msg
    End Sub

    Function GetCols(Cols() As Variant) As Variant
    Dim MyCols()
    ReDim MyCols(UBound(Cols))

    For i = 0 To UBound(Cols)
    MyCols(i) = Range(Cols(i) & «:» & Cols(i)).Column()
    Next
    GetCols = MyCols

    End Function

    [/VBA]


  2. 10-30-2004, 01:02 PM


    #2

    Hi MD,

    I don’t believe that you can declare a Constant array.

    Possible alternatives:

    1. A Public array variable that you fill with a ‘starter’ routine.

    2. Store the data in a worksheet.


  3. 10-31-2004, 10:29 PM


    #3

    How about a public function that returns the array. Function works just like any variables.

    Try this.
    [vba]
    Public Function Arr()
    Arr = Array(«a», «b», «c»)
    End Function

    Sub try()
    For Each a In Arr
    MsgBox a
    Next a
    End Sub
    [/vba]


  4. 11-01-2004, 09:57 AM


    #4

    That’s pretty good Sixth Sense. If that were the method, as I sometimes find myself lacking options, you can add different qualifying lines to your code so you don’t have to keep writing multiple Public Functions. You can just call from the sub routine and assign them in your Function …

    [vba]Option Explicit
    Public Function Arr(test)
    Select Case test
    Case «yes»
    Arr = Array(«a», «b», «c»)
    Case «no»
    Arr = Array(«d», «e», «f»)
    End Select
    End Function

    Sub try()
    Dim a As Variant, test As String
    test = «no»
    For Each a In Arr(test)
    MsgBox a
    Next a
    End Sub[/vba]

    Change the ‘test = «no» ‘ to whatever you wanted, msgbox, returned value, whatever. Just thought I’d throw that out there, fwiw.


  5. 11-01-2004, 10:40 AM


    #5

    Hi,
    Thanks to all. I’ve decided to use the following, (which works on my test form at least!)
    MD
    [VBA]
    Function Arr(MySet As String)
    Select Case MySet
    Case Is = «A»
    Arr = Array(«A», «B», «C», «D»)
    Case Is = «B»
    Arr = Array(«E», «F», «G», «H»)
    Case Is = «C»
    Arr = Array(«I», «J», «K», «L»)
    End Select

    End Function
    Private Sub UserForm_Initialize()
    For Each A In Arr(«B»)
    ListBox1.AddItem A
    Next
    End Sub
    [/VBA]



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Понравилась статья? Поделить с друзьями:
  • Vba excel макрос для всех листов
  • Vba excel макрос для всех книг
  • Vba excel лучший самоучитель
  • Vba excel логический оператор или
  • Vba excel логические выражения