Declare and Initialize String Array in VBA

前端 未结 6 1683
刺人心
刺人心 2020-11-28 02:54

This should work according to another stack overflow post but its not:

Dim arrWsNames As String() = {\"Value1\", \"Value2\"}

Can anyone let

6条回答
  •  [愿得一人]
    2020-11-28 03:34

    Public Function _
    CreateTextArrayFromSourceTexts(ParamArray SourceTexts() As Variant) As String()
    
        ReDim TargetTextArray(0 To UBound(SourceTexts)) As String
        
        For SourceTextsCellNumber = 0 To UBound(SourceTexts)
            TargetTextArray(SourceTextsCellNumber) = SourceTexts(SourceTextsCellNumber)
        Next SourceTextsCellNumber
    
        CreateTextArrayFromSourceTexts = TargetTextArray
    End Function
    

    Example:

    Dim TT() As String
    TT = CreateTextArrayFromSourceTexts("hi", "bye", "hi", "bcd", "bYe")
    

    Result:

    TT(0)="hi"
    TT(1)="bye"
    TT(2)="hi"
    TT(3)="bcd"
    TT(4)="bYe"
    

    Enjoy!

    Edit: I removed the duplicatedtexts deleting feature and made the code smaller and easier to use.

提交回复
热议问题