How to assign a set of text values to a string array? - arrays

How can I assign a set of text values to an array? Nothing I tried is working!
Months = Array("Jan", "Feb", ..., "Dec")
and others I tried do not work!

Here's something about VB: http://www.devx.com/vb2themax/Tip/18322
Visual Basic doesn't provide any way
to declare an array and initialize its
elements at the same time. In most
cases you end up with setting
individual elements one by one, as in:
Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
strArray(3) = "Winter"
Under VB4, VB5, and VB6 you can create
an array of Variants on the fly, using
the Array() function:
Dim varArray() As Variant
varArray() = Array("Spring", "Summer", "Fall", "Winter")
but there is no similar function to
create arrays of data types other than
Variant. If you're using VB6, however,
you can create String arrays using the
Split() function:
Dim varArray() As String
' arrays returned by Split are always zero-based
varArray() = Split("Spring;Summer;Fall;Winter", ";")

I'm pretty sure you can only do it like this:
dim months(2) as string
months(0) = "Jan"
months(1) = "Feb"
months(2) = "Mar"

If you're talking about vbscript then this works:
months = Array("may","june","july")
If it's vb.net then:
dim months() as string = {"may","june","july"}

Related

How to trim values in an array in VBA?

I got a problem I just cant fix. I have a string, want to split it at ";" (that is working) and then trim the values.
Dim cell As String
Dim objects() As String
cell = Range("X74").Text
objects= Trim(Split(cell, ";"))
I get my error on the Trim-function. I then tried the following approach:
For Each object In objects
object = Trim(object)
Debug.Print object
Next
This works, but doesnt save the trimmed value to my objects-array.
Despite naming your variables objects and object, they are an array of simple Strings resp. a simple String, and in VBA a string is not an object.
In your For Each-Loop, you are copying a string to the variable object, and no matter what you do with it, it doesn't change the content of the objects-array.
If you really need to change the content of the objects-array, use a loop like that:
Dim i As Long
For i = LBound(objects) To UBound(objects)
objects(i) = Trim(objects(i))
Debug.Print objects(i)
Next
And you should think about changing the name of your variables...
I would try to avoid vba names as variables:
Sub tst()
Dim yourcell As String, i As Long
Dim yourobjects() As String
yourcell = Range("X74").Text
yourobjects = Split(yourcell, ";")
For i = LBound(yourobjects) To UBound(yourobjects)
yourobjects(i) = Trim(yourobjects(i))
Debug.Print yourobjects(i)
Next i
End Sub

VB.NET how to convert Object(,) to String()

i am currently ready data from excel per EPPlus interface. My Excel is a csv file.
I've already the data available in a object(,) property (for me a unknown format).
Which format is that? How can i convert it to a String Array?
If you mean like this...
Private Class TestIt
Dim strvar1 As String = Nothing
Dim strvar2 As String = Nothing
Dim Split As Object = MyFunc("A")
strvar1 = Split(0)
strvar2 = Split(1)
End Class
'Extra class or something
Public Function MyFunc(Test As String) As Object
Dim var1 As String = Nothing
Dim var2 As String = Nothing
Select Case (Test)
Case "A"
var1 = "one"
var2 = "two"
Case "B"
var = "three"
var2 = "four"
End Select
Return {var1, var2}
End Function
Sorry I didn't realized that it was a two-dimensional array.
The Problem was to find out the maximum number of each dimension.
I've found the following functions:
Array.getUpperBound(dimension)
So that solves my problem.
But many thanks for your fast answer!!

Excel VBA - array used to process ranges changes the format of the cells

I am using an array to process the values (and texts) I have in a sheet:
Dim arr As Variant
arr = Application.ActiveSheet.UsedRange
'do stuff
Application.ActiveSheet.UsedRange = arr
However, this has a side-effect: when dumping the array back into the cells, the cells formatting is changed by Excel's default behaviour. For example, numbers stored as text which start with "0" are converted to numbers, and the "0" gets deleted. Or texts like "1-11" are converted to dates ("November 1"); and probably some others which I have not spotted yet.
If I monitor the Locals window, the strings are being preserved as strings in the array until the very moment, so it is the unloading that messes things up.
Is there a way to avoid this behavior ?
Edit: I also tried:
Application.ActiveSheet.UsedRange.Value = arr
Application.ActiveSheet.UsedRange.Value2 = arr
Application.ActiveSheet.UsedRange.text = arr
Same result for each.
You can use the valuetype option to preserve formatting etc: 11 is xlRangeValueXMLSpreadsheet
Sub CopyWithFormat()
Dim var As Variant
var = Range("A8:A11").Value(11)
Range("C8:C11").Value(11) = var
End Sub
But that will make it difficult to modify the values in the array.
So its probably simplest to loop the array adding '
Sub CopyWithFormat2()
Dim var As Variant
Dim j As Long
var = Range("A8:A11").Value
For j = 1 To UBound(var)
If VarType(var(j, 1)) = vbString Then var(j, 1) = "'" & var(j, 1)
Next j
Range("C8:C11").Value = var
End Sub
Also try
YourRangeVariable.NumberFormat = "#"
YourRange.Value=Your Array
This will convert the range to text first. Works for me when using values like 01-11 and saves having to do a loop.

Getting full Split array into a VBA variable

Say I have a path in Range("A1") that looks like this:
/data/apps/server/
I would like to get the three elements into a variable. I've thought doing a Split() by the separator / I would get the full array:
Dim myElements()
myElements = Split(Range("A1").Value,"/")
'>>> EXPECTED: myElements is [data, apps, servers]
but I actually get a Type mismatch error on the line myElements = Split(Range("A1").Value,"/"). What does the Split function return? Does it actually return the array or it rather gives read-only access?
I would just like to get the array of the Split method without having to loop through them and build my own array, if possible of course.
Change Dim elements() to Dim elements As Variant
You need to declare it as a Variant.
Explanation:
The data in Excel cell can be anything. So use a Variant. In cases like below, you know it is a String so declare it like a String
Sub Sample()
Dim myElements() As String
Dim myString As String
myString = "aaa/bbb/ccc"
myElements = Split(myString, "/")
Debug.Print myElements(0)
Debug.Print myElements(1)
Debug.Print myElements(2)
End Sub
Split returns a String Array. You may want to see This
Edit: I have a feeling that I may confuse someone with my explanation so let me explain it a bit more.
Dim myElements() means "Declare myElements as array of Variants".
Split returns an array of Strings. Hence, the mismatch.
You can do either Dim myElements or Dim myElements as Variant or Dim myElements() as String to resolve the problem.
Here is why each one of these works:
Dim myElements and Dim myElements as Variant
Both of these means that you declare myElements as Variant. Variants are special types, which can accept anything. As such, they can accept array of strings easily. However, variants have large memory overheads and should be avoided wherever possible.
Dim myElements() as String
This means that you declare myElements as array of strings. Since this is the same type as what is returned by the Split function, it is accepted.
Ideally, if you know the return type of a function, you should specify the correct type for your variables.
So in this case, Dim myElements() as String which is the same type returned from the Split funcition.

Create an Array from sub arrays vb.net

I have a string array A where I store some values for instance A(1) = "a,b,c" A(2)="1,2" etc. From that array I am creating subarrays using A(1).split(",") and I have
dim subArr1() as string = {"a","b","c"}
dim subArr2() as string = {"1","2"}
etc
Now, I want to create a new two-dimensional array
dim all()() as string = {subArr1, subArr2, ...}
The initial array is created dynamically and could be have 2, 5, or whatever number of items. So I could have any number of subArrays (subArrX)
Any idea how to deal with that? I am writing in vb.net 2013
Thank you
You could use this little LINQ query:
Dim A As String() = {"a,b,c", "1,2"}
Dim parts As IEnumerable(Of String()) = From str In A Select str.Split(","c)
Dim all()() As String = parts.ToArray()
Now the array contains two arrays, the first contains "a","b" and "c" and the second contains "1" and "2".

Resources