1

Im learning programing logic with VBA and im trying to do a array, but i have a error in the running


Dim auxTexto As String
Dim auxNumero As Long
Dim auxIterador As Integer

Dim auxArrayEstatico(0 To 5) As Integer
Dim auxArrayDinamico As String


Sub prueba()

    auxTexto = "Hola"
    auxNumero = 1
    
    auxArrayEstatico(0) = 2
    auxArrayEstatico(1) = 2
    auxArrayEstatico(2) = 2
    auxArrayEstatico(3) = 2
    auxArrayEstatico(4) = 2
    
    MsgBox ("Valor de Array en posicion 3 es " & auxArrayEstatico(3))
    
    ReDim auxArrayDinamico(0 To 3)
    auxArrayDinamico(0) = "Hola"
    
    MsgBox (auxArrayDinamico(0))
    
End Sub

The error say "Error of copilation, expected a array" Please, somebody can help me?

1 Answer 1

1

removed "as string", also moved variables inside sub.

For faster debugging, try replacing msgbox "hola"... with debug.print "hola".... this will just send the text to the immediate window for quick viewing.

Option Explicit
Sub prueba()

Dim auxTexto As String
Dim auxNumero As Long
Dim auxIterador As Integer

Dim auxArrayEstatico(0 To 5) As Integer
Dim auxArrayDinamico

    auxTexto = "Hola"
    auxNumero = 1
    
    auxArrayEstatico(0) = 2
    auxArrayEstatico(1) = 2
    auxArrayEstatico(2) = 2
    auxArrayEstatico(3) = 2
    auxArrayEstatico(4) = 2
    
    MsgBox ("Valor de Array en posicion 3 es " & auxArrayEstatico(3))
    
    ReDim auxArrayDinamico(0 To 3)
    auxArrayDinamico(0) = "Hola"
    
    MsgBox (auxArrayDinamico(0))
    
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Removing "as string" works because VBA interprets the declaration to be implicitly Variant. It is better to declare explicitly: Dim auxArrayDinamico As Variant.
Thanks @CameronCritchlow, can you explain me why i must place variables inside sub?
@BZngr thanks, your answer was really help, which is the difference beetween As String and As Variant
When declaring any variable you need to specify the data type. See here for more info

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.