1

I'm trying to work with global dynamical arrays in vba excel. the idea: I fill in information via an userform:

enter image description here

Then I click on the button ADD. There the "columnletter" and "name column" is saved in 2 arrays (arrBoxColumnLetters, arrBoxColumnNames). When I click on Start I want to use the information from the arrays above. But the arrays are empty...

My code:

My global variables:

Public i As Integer
Dim arrBoxColumnLetters() As Variant
Dim arrBoxColumnNames() As Variant

Private Sub UserForm_Initialize()

    'empty textbox
    TxtBoxExcelName.Value = ""
    TxtBoxStartRow.Value = ""
    TxtBoxTitleRowCR.Value = ""
    TxtBoxTitleRowCG.Value = ""
    TxtBoxTitleRowCB.Value = ""
    TxtBoxHeaderRowCR.Value = ""
    TxtBoxHeaderRowCG.Value = ""
    TxtBoxHeaderRowCB.Value = ""
    TxtBoxCLetter.Value = ""
    TxtBoxCName.Value = ""
    TxtBoxColumn.Value = ""
    TxtBoxTab.Value = ""
    'set focus on TxtBoxStartRow
    TxtBoxExcelName.SetFocus

    'initialize variables
    i = 0
    End Sub


    Private Sub BtnAddC_Click()
    Dim ColumnDataOri, ColumnData As String

    ReDim Preserve arrBoxColumnLetters(i + 1)
    ReDim Preserve arrBoxColumnNames(i + 1)

    ColumnDataOri = TxtBoxColumn.Value
    ColumnData = TxtBoxCLetter.Value & vbTab & vbTab & TxtBoxCName.Value
    TxtBoxColumn.Value = ColumnDataOri & vbCrLf & ColumnData

    arrBoxColumnLetters(i) = TxtBoxCLetter
    arrBoxColumnNames(i) = TxtBoxCName

    TxtBoxCLetter.Value = ""
    TxtBoxCName.Value = ""
    TxtBoxCLetter.SetFocus
    i = i + 1

    End Sub

    Private Sub BtnCancel_Click()
        Unload Me
    End Sub

    Private Sub BtnClear_Click()
        Call UserForm_Initialize
    End Sub

    Private Sub BtnSart_Click()
        Dim sh As Worksheet
        Dim wbori As Workbook
        Dim strRGBTitleRow, strRGBHeaderRow, strFilenameOrigineel, strBoxColumnInfo As String
        Dim arrBoxColumnInfo(), arrBoxColumnLetters(), arrBoxColumnNames() As String
        Debug.Print "i=" & i
        ReDim Preserve arrBoxColumnLetters(i + 1)
        ReDim Preserve arrBoxColumnNames(i + 1)

        Application.ScreenUpdating = False
        Application.DisplayAlerts = False

        TxtBoxSave.Value = ""

        strRGBTitleRow = TxtBoxTitleRowCR.Value & TxtBoxTitleRowCG.Value & TxtBoxTitleRowCB.Value
        strRGBHeaderRow = TxtBoxHeaderRowCR.Value & TxtBoxHeaderRowCG.Value & TxtBoxHeaderRowCB.Value
        strFilenameOrigineel = TxtBoxExcelName.Value


        Debug.Print arrBoxColumnLetters(0)
        Debug.Print arrBoxColumnNames(0)
2
  • 1
    Don't use Dim at module-level. Use Public for public/global variables, and Private for private ones. Dim works exactly like Private, and Public works exactly like the obsolete/deprecated Global scope. Commented Jun 18, 2018 at 14:12
  • 1
    Also, anything declared in a form's code-behind is instance state, since a form is a class that defines an object and so its public members need to be accessed through an instance of that class. Global variables are declared in standard/procedural modules, not classes/forms/worksheets. Commented Jun 18, 2018 at 14:16

1 Answer 1

2

Global variables are rarely a good idea in VBA. However, to make sure that your variables work, declare these in a separate module, not on the form. Like this:

Public i As Integer
Public arrBoxColumnLetters() As Variant
Public arrBoxColumnNames() As Variant

Furthermore - write Option Explicit on the top of your code. It will let you know, that the variable arrBoxColumnLetters is declared twice, thus this is a bit problematic.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the tips. If you say that global variables are a bad idea, how do you suggest I solve this problem? basically, I want to use the variables from the "ADD" button in the "START" prcedure.
Arrays are not allowed as "public members of object modules"
@VeVi - Avoiding global variables is an architectural problem, thus it is a bit offtopic - there is not a simple solution to it. And concerning the Arrays are not allowed as "public members of object modules" - what do you mean?
@VeVi seems you skipped the part where this answer says "write them in a separate module, not on the form".
Sorry, you are right, forgot the separate module part. TX!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.