4

I'm trying to accomplish this obviously simple thing, but somehow VBA keeps serving me weird errors. I would like to have a global array named styles containing the following strings: Settings, Titles, Comment and Direct Copy. in Code:

Public styles(4) As String

Directly assigning the array was not working for me so i did this via a sub:

sub Populate()
  styles(0) = "Settings"
  styles(1) = "Titles"
  styles(2) = "Comment"
  styles(3) = "Direct Copy"
  Debug.Print styles
End Sub

However this does not work as it gives a compile error: Type mismatch on the debug.print line... The expected result was something like: ("Settings", "Titles", ..) etc like any programming language would return.

So how do I get a public array containing strings in VBA Excel such that I can use them in the same module across functions and subs?

2

2 Answers 2

9

Try this form of public declaration and array element assignment.

Option Explicit

Public styles As Variant

Sub printStyles()

    styles = Array("Settings", "Titles", "Comment", "Direct Copy")

    Debug.Print LBound(styles) & "to" & UBound(styles)

    Dim i As Long
    For i = LBound(styles) To UBound(styles)
        Debug.Print styles(i)
    Next i

    Debug.Print Join(styles, ", ")

End Sub

BTW, there a reserved Styles Object which you may have difficulty using if you continue to use reserved words as the names of your public and private variables.

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

1 Comment

Works like a charm! Thanks for the extra explanatory code!
2

This is how to print the array (twice):

Public styles(4) As String

Sub Populate()
    styles(0) = "Settings"
    styles(1) = "Titles"
    styles(2) = "Comment"
    styles(3) = "Direct Copy"

    '1. print is here ---v
    Dim cnt As Long
    For cnt = LBound(styles) To UBound(styles)
        Debug.Print styles(cnt)
    Next cnt
    '--------------------^        
    '2. print is here ---v
    Debug.Print Join(styles, vbCrLf)
    '--------------------^
End Sub

This is what you get (twice):

Settings
Titles
Comment
Direct Copy

The first time you loop by the elements and print each of them on a new line. The second time you return a string created by joining a number of substrings contained in the array.

Join MSDN Reference

8 Comments

Is there a way to declare a public array like you would do in python or java like: array = ("value1", "value2", etc..)
The ultimate goal of the array is to verify styles of an excel and if they are in an array they need to be written to a txt file
I don't know if the OP was ever expecting a fifth array element but styles(4) leaves 5 elements for a zero-based array.
@Brilsmurfffje - it should be in a procedure (within a Sub or Function).
@Brilsmurfffje - this is because you do not have Dim Styles As Variant within the sub, but you are using the Public styles(4) As String outside the sub. Write Public styles As Variant outside or Dim Styles As Variant within.
|

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.