Redim Arrangement VBA

0

I have a small problem that I hope can guide me, it turns out that I have to store an array and for each data I'm going to store, verify if it has not been previously entered, the difficulty I have is that I define an array of 10 elements. , but the user may need to enter 15, 20, 30 on items. I have tried to use the ReDim instruction but I have not managed to do it since I get the error of The matrix already has dimensions

Annex code if you can guide me.

 Sub buscarClaves()   
    Dim AClaves(10) As String
    Dim cont As Integer
    cont = 0

 ReDim Preserve AClaves(cont)
    Dim nClave As String

    Do While nClave <> "n"
        nClave = InputBox("Ingrese Clave")
        If bClaves(nClave, AClaves()) Then
            MsgBox ("Clave duplicado")
             Else
                AClaves(cont) = nClave
                cont = cont + 1
           End If
    Loop


    End Sub

Function to search for repeated data within the array.

    Function bClaves(ByVal clve As String, ByRef Datos() As String) As Boolean
        bClaves = False
        Dim clave As Variant 

        For Each clave In Datos
            If clave <> "" Then
                If clave = clve Then
                    bClaves = True
                End If
            End If
        Next
    End Function
    
asked by Silvestre Silva 19.06.2018 в 19:00
source

1 answer

0

I have found the solution to my problem, attach code in case someone serves you.

Option Explicit
Dim AClaves() As String
Dim impr As String

Sub buscarClaves()
    impr = ""
    Erase AClaves()
    ReDim AClaves(10)

    Dim cont As Integer
    cont = 0
    Dim nClave As String
    Dim iRet As Integer

    Do While nClave <> "n" ' Presionar n para terminar de capturar
        nClave = InputBox("Ingrese Clave")
        If bClaves(nClave, AClaves()) Then
            ' Aca ira la condicion para registrar duplicado o no.
                 iRet = MsgBox("Clave duplicado - ?Desea registrarlo?", vbYesNo, "Registrar claves.")
                 If iRet = vbYes Then
                    If cont > 10 Then
                        ' Si rebasa el indice inicial(en este caso 10 elementos) redimensionar arreglo de 1 en 1
                            ReDim Preserve AClaves(cont)
                    End If
                    AClaves(cont) = nClave
                    cont = cont + 1
                 End If
            Else
                If cont > 10 Then
                MsgBox ("ReDim")
                    ReDim Preserve AClaves(cont)
                End If
                AClaves(cont) = nClave
                cont = cont + 1
              End If
        Loop

    ' Imprimir datos ingresados... Opcional
    Dim nnclave As Variant
    Dim n As Integer
    For n = 0 To UBound(AClaves)
        If AClaves(n) <> "" Then
            impr = impr & " " & AClaves(n)
        End If
    Next n

    MsgBox (impr)

    End Sub

    Function bClaves(ByVal clve As String, ByRef Datos() As String) As Boolean
        bClaves = False
        Dim clave As Variant ' Peticion del VBA
        For Each clave In Datos
            If clave <> "" Then
                If clave = clve Then
                    bClaves = True
                    Exit Function
                End If
            Else
                bClaves = False
                Exit For
            End If
        Next
    End Function
    
answered by 20.06.2018 / 00:34
source