This should work according to another stack overflow post but its not:
Dim arrWsNames As String() = {"Value1", "Value2"}
Can anyone let me know what is wrong?
This should work according to another stack overflow post but its not:
Dim arrWsNames As String() = {"Value1", "Value2"}
Can anyone let me know what is wrong?
Try this:
' Variant array
Dim myVariantArray As Variant
myVariantArray = Array("Cat", "Dog", "Rabbit")
' String array
Dim myStringArray() As String
myStringArray = Split("Cat,Dog,Rabbit", ",")
myArray = Array("A", "B", 12345, "D"...)
–
Defoe Dim arrWsNames() As String: arrWsNames = Split("Value1,Value2", ",")
The initialization from comment above does not work for me, because Array() creates an Array of Variants and not Strings –
Miscellaneous Dim a() As String ... a = Array(...)
worked, but it doesn't -- that's a Type mismatch error... Unfortunately there's no good way to do this in VBA. –
Lananna In the specific case of a String array you could initialize the array using the Split Function as it returns a String array rather than a Variant array:
Dim arrWsNames() As String
arrWsNames = Split("Value1,Value2,Value3", ",")
This allows you to avoid using the Variant data type and preserve the desired type for arrWsNames.
The problem here is that the length of your array is undefined, and this confuses VBA if the array is explicitly defined as a string. Variants, however, seem to be able to resize as needed (because they hog a bunch of memory, and people generally avoid them for a bunch of reasons).
The following code works just fine, but it's a bit manual compared to some of the other languages out there:
Dim SomeArray(3) As String
SomeArray(0) = "Zero"
SomeArray(1) = "One"
SomeArray(2) = "Two"
SomeArray(3) = "Three"
Dim myStringArray() As String
*code*
redim myStringArray(size_of_your_array)
Then you can do something static like this:
myStringArray = { item_1, item_2, ... }
Or something iterative like this:
Dim x
For x = 0 To size_of_your_array
myStringArray(x) = data_source(x).Name
Next x
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.
variant
–
Adage An only-what's-needed function that works just like array() but gives a string type. You have to first dim the array as string, as shown below:
Sub UseStringArray()
Dim sample() As String
sample = StringArray("dog", "cat", "horse")
End Sub
Function StringArray(ParamArray ArgList())
ReDim tempArray(UBound(ArgList)) As String
For i = 0 To UBound(ArgList)
tempArray(i) = ArgList(i)
Next
StringArray = tempArray
End Function
For more on converting array types see here: How transform Variant to Double format and vice versa in VBA
Using the same solution as @matan_justme and @mark_e, I think the structure can be cleaned up a bit.
Just as the built in function Array
we can build our own custom function that uses a ParamArray
to accept an array of items as the argument, and return a String Array.
By default, when assigning values to a String Array it will implicitly convert any non-String values into a String.
Public Function StringArray(ParamArray values() As Variant) As String()
Dim temp() As String
ReDim temp(LBound(values) To UBound(values))
Dim index As Long
For index = LBound(temp) To UBound(temp)
temp(index) = values(index)
Next
StringArray = temp
End Function
The nice thing with this structure is that it can be applied to different data types with intuitive naming conventions. For instance, if we need an Array with Long
values, we simply need to change every instance where String
is located.
Public Function LongArray(ParamArray values() As Variant) As Long()
Dim temp() As Long
ReDim temp(LBound(values) To UBound(values))
Dim index As Long
For index = LBound(temp) To UBound(temp)
temp(index) = values(index)
Next
LongArray = temp
End Function
Other Data Type examples could include:
The beauty is an error will be thrown when a value is not the correct data type and it can not be converted over, you will receive a Run-time error '13': Type mismatch
error.
Using
Dim myarray As Variant
works but
Dim myarray As String
doesn't so I sitck to Variant
Dim MyArray() as String
, or a fixed size Array : Dim MyArray(1 to 10) as String
. –
Garland © 2022 - 2024 — McMap. All rights reserved.
Dim x() As Variant: x = [{"Value1", "Value2"}]
– ChickamaugaDim x() As Variant: x = [{"Value1", "Value2"}]
IF you are using variables... i.e. ifv1 = "Value1"; v2 = "Value2"
, thenx = [{v1, v2}]
will generate an error, whereasx = [{"Value1", "Value2"}]
will not. – Nympholepsy