I have the code below to get the value assigned to Array "UIDList()". But for what ever reason it is only getting the last value only. Can you please correct what could've been gone wrong.
Dim strTest As String
Dim strarray() As String
Dim UIDList() As String
Dim intCount As Integer
Dim TotUID As Integer
Set oFS = oFSO.OpenTextFile("C:\Pro\test.txt")
txtpro = oFS.ReadAll
strTest = txtpro
strarray = Split(strTest, "=")
For intCount = LBound(strarray()) To UBound(strarray())
If InStr(strarray(intCount), "NAME") Then
UIDList() = Split(strarray(intCount), "NAME")
End If
Next
For TotUID = LBound(UIDList()) To UBound(UIDList())
Debug.Print UIDList(TotUID)
Next
Improper nesting:
change to this:
For intCount = LBound(strarray()) To UBound(strarray())
'Erase the array
Erase UIDList
If InStr(strarray(intCount), "NAME") Then
UIDList() = Split(strarray(intCount), "NAME")
End If
'/ Check if UIDList() has any elements
If Not (Not UIDList()) Then
For TotUID = LBound(UIDList()) To UBound(UIDList())
Debug.Print UIDList(TotUID)
Next
End If
Next
Related
I tried to concat some strings in Excel with VBA.
Sub Button1_Click()
Dim lenArray As Variant
Dim strArray As Variant
Dim strCombined As String
Dim space As String
lenArray = Range("D1:J1").Value
strArray = Range("D2:J2").Value
space = " "
Dim i As Integer
Dim temp As String
For i = LBound(strArray) To UBound(strArray)
strCombined = strCombined & strArray(i)
temp = WorksheetFunction.Rept(space, CInt(lenArray(i)) - Len(strArray(i)))
strCombined = strCombined & temp
Next i
Range("B4").Value = strCombined
End Sub
However, it always gives me out of bound error.
I also tried changing
For i = LBound(strArray) To UBound(strArray)
to
For i = 1 To 7
or
For i = 1 To 2
but it never works.
I have tried to iterate only the strArray with For Each and it would work.
Any help would be great.
Edited:
The spreadsheet I am using
Let's say I have the following Array, arr that contains a maximum of 20 substrings. In this example, the array will have 4 substrings:
arr = {"AAAA: 1234567" , "BBBB: 2345678" , "CCCC: 98765432" , "DDDD: 87654321"}
Note: Capitalization is not important.
I am needing assistance with finding a substring inside an array (again, up to 20 strings possible), that Starts With CCCC:, and then assigning that entire string to it's own variable, let's call it Var1.
I understand I could assign Var1 = arr(2), but I need to figure out how to determine the index number in the array that meets my criteria.
What I would really appreciate (although any method I will be happy with), is making a stand alone function such as:
Function ArrayIndex(ArrayInput as Variant, StartsWith as String) as Byte
'Arguments Here
End Function
Then I could just use this in my Subroutine:
Var1 = arr(ArrayIndex(arr, "CCCC:"))
Update 1
Here is a snippet of my current code
Sub T_Report
'<Shortcut = Shift+Ctrl+T>
Dim Data as String, DataArray as Variant
With ActiveSession
.Copy 0, 2, 80, 21 'Just a big block of text with multiple lines, copied to clipboard
Data = Clipboard 'Set Data to the clipboard value
DataArray = Split(Data,vbCrLf) 'This is "Data" in an Array, separated by line breaks
'Just checking to see if the Array was successful (it is)
Debug.Print DataArray(0) & vbNL & DataArray(1) & vbNL & DataArray(2) & _
vbNL & DataArray(3) & vbNL & DataArray(4) & vbNL & vbNL
'Misc code here
Dim sF1 as String, sF2 as String, sF3 as String
Dim Rslt1 as String, Rslt2 as String, Rslt3 as String
sF1 = "Field1:"
sF2 = "Field2:"
sF3 = "Field3:"
MsgBox DataArray(0) ' This works fine, giving me first substring
Rslt1 = FindMyString(sF1, DataArray)
' Misc Code
End With
End Sub
However, when I use the following function, I get Type Mismatch error on the MsgBox arr(0) line, although it should be giving me the very first substring of DataArray in the above sub (should be an exact match, the array has not been modified).. However, when I do MsgBox DataArray(0) above, I do get the first substring.
Private Function FindMyString(strToFind As String, ParamArray arr() As Variant) As String
Dim i As Integer
Dim iLen As Integer
Dim strArr As String
FindMyString = "" ' Returns Blank String if not found
' I get type mismatch here (Doesn't appear Array from sub loaded into this function)
MsgBox arr(0)
iLen = Len(strToFind)
For i = 0 To UBound(arr)
strArr = CStr(arr(i))
If strToFind = Left$(strArr, iLen) Then
FindMyString = strArr
Exit Function
End If
Next i
End Function
EDIT - FIX YOUR ARRAY LOAD
Change this (it MUST give you an error!):
arr = {"AAAA: 1234567" , "BBBB: 2345678" , "CCCC: 98765432" , "DDDD: 87654321"}
To This:
Dim arr As Variant
arr = Split("AAAA: 1234567,BBBB: 2345678,CCCC: 98765432,DDDD: 87654321", ",")
If you want to use a function to do your bidding you can pass an array of data using the ParamArray type. It has to be the last parameter in your function
This should do what you want:
Private Function FindMyString(strToFind as string, ParamArray arr() As Variant) As String
Dim i As Integer
Dim iLen as Integer
Dim strArr as String
FindMyString = "" ' Returns Blank String if not found '
Debug.Print arr(0)(0) ' Print first element of array
iLen = Len(strToFind)
For i = 0 To UBound(arr(0))
strArr = CStr(arr(0)(i))
If strToFind = Left$(strArr, iLen) Then
FindMyString = strArr
Exit Function
End If
Next i
End Function
In your example you can test it by:
Var1 = FindMyString("CCCC:", arr)
I'm trying to loop through a listbox and add the contents to an array....
My code is this:
Private Sub exportfolders_Click()
Dim list As String
Dim folderlist As String
Dim folderarray() As String
'Dim i As Interger
For i = 0 To Me.selectedfolders.ListCount - 1
'folderlist = (Me.selectedfolders.Column(0, i))
'folderarray() = Join(Me.selectedfolders.Column(0, i), ",")
list = (Me.selectedfolders.Column(0, i))
folderarray() = Join(list, ",")
ReDim Preserve folderarray(i)
Next i
folderlist = folderarray
'folderarray() = Join(folderlist, ",")
MsgBox (folderlist)
End Sub
You can see the bits I have commented out, trying all sorts to get it to work. But I keep getting the message "Can't assign to array" at folderarray(i) = Join(list, ","). Any pointers as to where I am failing?
You can concatenate the list box items into a string, and then use Split() to load your array. That way, the array is sized automagically without you needing to ReDim.
I tested this code in Access 2010:
Dim folderarray() As String
Dim i As Long
Dim strList As String
For i = 0 To Me!selectedfolders.ListCount - 1
strList = strList & "," & Me!selectedfolders.Column(0, i)
Next
' use Mid() to exclude the first comma ...
folderarray = Split(Mid(strList, 2), ",")
Note I don't know what you want to do with the array after loading it. MsgBox folderarray would throw Type mismatch error. MsgBox Mid(strList, 2) would be valid, but if that's what you want, you wouldn't need the array.
1) declare the array. Take a look at https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx
2) No need of support variable
3) Assign the values to your array with the correct syntax
Private Sub exportfolders_Click()
Dim folderarray() As String
Dim i As Interger
Redim folderarray (Me.selectedfolders.ListCount-1)
For i = 0 To Me.selectedfolders.ListCount - 1
folderarray(i) = Me.selectedfolders.Column(0, i)
Next i
' Write here what you want to do with your array
End Sub
You could try something like this:
Private Sub ListToArray()
Dim folderArray() As Variant
Dim currentValue As String
Dim currentIndex As Integer
Dim topIndex As Integer
topIndex = Me.selectedfolders.ListCount - 1
ReDim folderArray(0 To topIndex, 0 To 1)
For i = 0 To topIndex
currentValue = Me.selectedfolders.Column(0, i)
folderArray(i, 0) = i
folderArray(i, 1) = currentValue
Next i
End Sub
Note my example is a multi-dimensional array which will give you the ability to add more than one item should you chose to do so. In this example I added the value of "i" as a placeholder/ index.
I have a function that returns a string array
Function getAr() As String
Dim tmpAr(3) As String
getAr(0) = "Hallo"
getAr(1) = "I"
getAr(2) = "Am"
getAr(3) = "I"
getAr = tmpAr
End Function
In a Sub I want to reassign the returned string array like
Sub test()
Dim tmpAr() As String
ReDim tmpAr(UBound(getAr))
Debug.Print tmpAr(0)
Debug.Print tmpAr(1)
End Sub
The error is
assigning to data field is not possible.
I guess that is because I have to dimension the strAr to the same dimension as the myfunc arry. But I just do not get that information.
Could anyone help me here?
Your function and sub both need changing:
Function getAr() As String()
Dim tmpAr(3) As String
tmpAr(0) = "Hallo"
tmpAr(1) = "I"
tmpAr(2) = "Am"
tmpAr(3) = "I"
getAr = tmpAr
End Function
Sub test()
Dim tmpAr() As String
tmpAr = getAr
Debug.Print tmpAr(0)
Debug.Print tmpAr(1)
End Sub
You don't need to know the dimension to assign an array, but your code wasn't correct.
Here is my amended version :
Function getAr() As String()
Dim tmpAr(3) As String
tmpAr(0) = "Hallo"
tmpAr(1) = "I"
tmpAr(2) = "Am"
tmpAr(3) = "I"
getAr = tmpAr
End Function
Notice the String(), this is how you declare the type of a function returning an array.
Also, you were dimensionning tmpAr and tried to affect values to getAr which wasn't set and as you started, it is much easier to work with a temp variable that you'll assign to the function's output at the end of the manipulations.
And your ReDim ReDim tmpAr(UBound(getAr)) was working but did not pass the array, this how you do it tmpAr = getAr and you don't even need to use ReDim before! ;)
Sub test()
Dim tmpAr() As String
tmpAr = getAr
Debug.Print tmpAr(0)
Debug.Print tmpAr(1)
End Sub
I have a text file like:
[edit] the number of line is unknown, it could be hundreds of lines.
How would I store them in a multidimensional array? I want my array to look like:
sample(0)(0) = "--------"
sample(0)(1) = "Line1"
..and so on
sample(1)(0) = "--------"
sample(1)(3) = "Sample 123"
..and so on
What I have done so far was to open the file and store in a 1-dimentional array:
logs = File.ReadAllLines("D:\LOGS.TXT")
I have tried creating an Array of string like:
Dim stringArray as String()()
stringArray = New String(varNumber0)(varNumber1)
But it returns and error.
You can use File.ReadLines/File.ReadAllLines to get the lines and a simple For Each-loop to fill a List(Of List(Of String)). Then you can use
list.Select(Function(l) l.ToArray()).ToArray()
to get the String()() (jagged array):
Dim lines = File.ReadLines("D:\LOGS.TXT")
Dim list As New List(Of List(Of String))
Dim current As List(Of String)
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
list.Add(current)
Else
current.Add(line)
End If
Next
Dim last = list.LastOrDefault()
If last IsNot Nothing Then
If Not current Is last AndAlso current.Any() Then
list.Add(current)
ElseIf Not last.Any() Then
list.Remove(last) ' last line was ("----------")'
End If
End If
Dim stringArray As String()() = list.Select(Function(l) l.ToArray()).ToArray()
If you want to include the --------- in the array at the first position:
For Each line As String In lines.SkipWhile(Function(l) Not l.TrimStart.StartsWith("----------"))
If line.TrimStart.StartsWith("----------") Then
current = New List(Of String)
current.Add(line)
list.Add(current)
Else
current.Add(line)
End If
Next
Try like this but you need to customize according to you
Dim mArray(10,10) As String
Dim i As Integer = 0
For I=0 to 10
For J=0 to 10
For Each line As String In System.IO.File.ReadAllLines("file.txt")
mArray(i,j) = cmdReader.Item(line)
Next
Next
Next
Use declaration like this (this is just a generic)
Dim dim1 As Integer = 0
Dim dim2 As Integer = 0
Dim strings(,) As String
Do
dim1 = NewDimensionNumberFromFile
dim2 = NewSecondDimensionNumberFromFile
ReDim Preserve strings(dim1, dim2)
strings(dim1, dim2) = ValueFromfile
Loop While (Not EOF()) 'this will determine