I have two variables (getYear and getBranch) in my page.
getYear-1,4,11
getBranch-4,5,7
GetYearSingle = Split(getYear, ",")
I get single array value after Split() function like this:
For Each iLoop In GetYearSingle
response.write "<br>Year= " & iLoop
Next
I get result like this
year=1
year=4
year=11
but I need result like this
year=1
Branch=4
year=4
Branch=5
year=11
Branch=7
Going out on a limb I'll assume that
getYear-1,4,11
getBranch-4,5,7
was actually meant to look like this:
getYear = "1,4,11"
getBranch = "4,5,7"
If that's the case you want to split both strings at commas and use a For loop (not a For Each loop) to iterate over the elements of both arrays.
arrYear = Split(getYear, ",")
arrBranch = Split(getBranch, ",")
For i = 0 To UBound(arrYear)
response.write "<br>Year= " & arrYear(i)
response.write "<br>Branch= " & arrBranch(i)
Next
You need to loop over both arrays via the (syncronized) index:
Option Explicit
Dim y : y = Split("1,4,11", ",")
Dim b : b = Split("4,5,7", ",")
If UBound(y) = UBound(b) Then
Dim i
For i = 0 To UBound(y)
WScript.Echo y(i), b(i)
Next
End If
output:
cscript 44118915.vbs
Microsoft (R) Windows Script Host, Version 5.812
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
1 4
4 5
11 7
Related
I have a hard-coded 2d array that prints to a text box in visual basic. However I am unable to split the lines between each row. This is an extract from the array:
stpeople(0, 0) = "Bob"
stpeople(1, 0) = "Last"
stpeople(0, 2) = "Jamie"
stpeople(1, 2) = "Smart"
This will show as one line like so: "Bob Last Jamie Smart"
this is the code that I am using to display the array am I using vbNewLine incorrectly? I am using visual basic.net.
Dim stoutput As String
For y As Integer = 0 To 9
For x As Integer = 0 To 4
stoutput = stoutput & stpeople(x, y) & " "
Next
stoutput = stoutput & vbNewLine
Next
ListArray.Items.Add(stoutput)
I don't know what type is ListArray but I'll try to guess. Sometime, these things don't display new lines, you would need to add each line to the Items.
Dim stoutput As String
For y As Integer = 0 To 9
stoutput = ""
For x As Integer = 0 To 4
stoutput = stoutput & stpeople(x, y) & " "
Next
ListArray.Items.Add(stoutput)
Next
Your code should work with textbox, if not, it mean you didn't set them to support multiline.
I have attached a code, however, this will only find the files that is present in the folder.
What I want to have is an incremental counter for the files. Thing is that sometimes the version will start something else than 0 or 1, e.g. 3.
Amesto non AN suppliers TEST W20-3 AN then I want the next string to be 4.
I am currently using this, but it will only work if 1 is the first, etc.
I am really stuck.
' Version check
Do While Len(Dir(strPath2 & "Amesto non AN suppliers TEST W" & week & "-" & version & "*.cif")) <> 0
version = version + 1
strPath = getDirectoryPath & "Amesto non AN suppliers TEST W" & week & "-" & version & " " & UserName & ".cif"
Loop
Sub loadversion()
Dim MyFile As String
Dim Counter As Long
'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("C:\Users\niclas.madsen\Desktop\AP\WAVE3\CIF\*.*")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1
Loop
' do something here?!
If MyFile = vbNullString Then
Else
End If
'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
For Counter = 0 To UBound(DirectoryListArray)
'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
Debug.Print DirectoryListArray(Counter)
Next Counter
End Sub
To get the highest version on a filename in your directory, insert the following functions:
Function CheckHighestVersion(path As String, cutLettersAtWordBeginning As Integer) As Integer
Dim file As Variant
Dim toBeCut As String
Dim verLength As Integer
Dim highestVersion As Integer
highestVersion = 0
file = Dir(path)
While (file <> "")
toBeCut = file
toBeCut = Mid(toBeCut, cutLettersAtWordBeginning + 1)
verLength = FindVerLength(toBeCut)
If verLength = -1 Then
CheckHighestVersion = 0
Exit Function
End If
toBeCut = Left(toBeCut, verLength)
If Val(toBeCut) > highestVersion Then
highestVersion = Val(toBeCut)
End If
file = Dir
Wend
CheckHighestVersion = highestVersion
End Function
Function FindVerLength(fileName As String) As Integer
Dim i As Integer
For i = 1 To Len(fileName)
If Not IsNumeric(Mid(fileName, i, 1)) Then
If i = 1 Then
MsgBox "Couldn't obtain the highest version of the files: " & _
"The first letter of the version is not numeric. The letter is " & Mid(fileName, i, 1) & _
". Please use correct amount of letters to be cut at the beginning of the file name."
FindVerLength = -1
Exit Function
End If
FindVerLength = i - 1
Exit Function
End If
Next i
FindVerLength = i
End Function
Call CheckHighestVersion in your Sub. the path is only the directory (e.g. C:\Test\ ), the second parameter is the number of letters you don't need at the beginning of the word. If I counted correctly, that value should be 30+(length of week, week 25 would be 2, week 7 would be 1) in your case. The function returns the highest version contained in that folder.
First of all let me all congratulate you on a superb forum! it's helped me Loads! so far with getting my little program working... and now I'm finally stuck.
I am attempting to automate Outlook to send tailored Offers to clients through a UserForm.
We offer 5 types of solutions and I don't know if the client will want 1 (DCF) or 2 (Top-Slice) or 3 (Ertragswert) or 4 (Belwert) or the 5 (Sachwert) of them. So I need a way for the code to check how many Checkboxes are ticked and then order them into a string (I've named it ValTyp) and separate them with comas and insert an "and"before the last. Say client wants 1, 3 and 5. The solution would be DCF, Ertragswert and Sachwert. So far I have my checkboxes all checking for values as follows:
Public iSach As String
Private Sub CKSach_Click()
Dim Sach As Boolean
Sach = CKSach.Value
If Sach = True Then
iSach = "Sachwert "
ValCount = ValCount + 1
Else
iSach = ""
ValCount = ValCount - 1
End If
End Sub
I have attempted at building an IF statement for a similar part which has 3 options and one is a must:
If (iRics <> "" And iBelSTD <> "" And iImmo <> "") Then
Standard = (iRics & ", " & iBelSTD & "und " & iImmo)
ElseIf (iBelSTD <> "" Or iImmo <> "") Then
Standard = (iRics & "und " & iImmo & iBelSTD)
Else
Stadard = iRics
End If
I am thinking of creating an array, with the length of ValCount... but I seem to be completely unable t get it to work:
Dim Services() As String
ReDim Services(0 To 4) As String
If iDCF <> "" Then
Services(0) = iDCF
End If
If iDCF <> "" Then
Services(1) = iCore
End If
If iDCF <> "" Then
Services(2) = iErtrag
End If
If iDCF <> "" Then
Services(3) = iSach
End If
If iDCF <> "" Then
Services(4) = iBelVT
End If
Debug.Print Services(0, 1, 2, 3, 4)
I get an runtime-error 9 index outside bounds.
I have no idea what to do and I haven't even got to how to include the commas and "and".
Any help at all will be much appreciated!
Thanks in advance!
Cliff
I would approach it like this: first, you store your checkboxes in a collection:
Dim cbs As New Collection
cbs.Add checkbox1
cbs.Add checkbox2
'...
cbs.Add checkbox5
Hence, you loop inside it to add the checked values into a new collection:
Dim myStr As String: myStr = ""
Dim cbsCheck As New Collection
'count "true"
For j = 1 To cbs.Count
If cbs(j).Value = True Then
cbsCheck.Add cbs(j)
End If
Next j
'hence you compose the string
If cbsCheck.Count = 0 Then
myStr = "No element selected"
ElseIf cbsCheck.Count = 1 Then
myStr = "You selected " & cbsCheck(1).Caption
Else
k = 1
myStr = "You selected "
While k < cbsCheck.Count
myStr = myStr & cbsCheck(k).Caption & ", "
k = k + 1
Loop
myStr = myStr & "and " & cbsCheck(k+1).Caption
End If
NOTE when you want to compose an array of elements of which you do NOT know the size in advance, in 95% of cases the object Collection is better than an object String Array.
You can use a collection instead of an array, which is most of the cases the better solution. If you really need an array for some reason, you can use the redim function to resize an array:
Example:
Dim myArray(3) As String
myArray(1) = "a"
myArray(2) = "b"
myArray(3) = "c"
ReDim myArray(4) As String
myArray(4) = "d"
You can also add a Preserve after ReDim to make sure the values won't get lost.
It seems that more "complex" ArrayLists are not widely used, since I'm unable to find any concrete, helpful info about it.
I'm trying to create an ArrayList of Arrays (and eventually an ArrayList of ArrayLists of Arrays), but I seem unable to either add Arrays to the ArrayList, or access the Array's elements. All this is done using VBScript in QTP.
(The code reads from an Excel file, which is working fine.)
Set my_sheet = ExcelObject.sheets.item(testCaseSheet)
testCase = CreateObject("System.Collections.ArrayList")
Function getTestsCaseActions (row, col)
Do While my_sheet.cells(row, 2).Value <> ""
MsgBox tempArray(0) & " -> " & tempArray(1) 'WORKS FINE - THE VALUES ARE PRINTED
testCase.Add tempArray
row = row+2
Loop
End Function
getTestsCaseActions 3, 4
'This is not working - how do I access the arrays and their values in the arraylist?
For Each ArrayItem in testCase
MsgBox ArrayItem(0)' & ", " & ArrayItem(1)
'MsgBox "Hey!"
Next
Now, I realize that For Each ArrayItem in testCase is probably wrong, but I cannot find out what to use? The elements added to the ArrayList are, after all, Arrays. If I uncomment the line MsgBox "Hey!", it's written once, even though the ArrayList should have 3 Arrays.
Short answer: The correct way to use an ArrayList Of Arrays if you just need read access (after a successful initialization):
Option Explicit
Dim alA : Set alA = CreateObject("System.Collections.Arraylist")
alA.add Split("A B C")
alA.add Split("D E F")
alA.add Split("I J K")
WScript.Echo "---- For Each In"
Dim aX
For Each aX In alA
WScript.Echo TypeName(aX), Join(aX)
Next
WScript.Echo "---- For To"
Dim i
For i = 0 To alA.Count - 1
WScript.Echo TypeName(alA(i)), Join(alA(i))
Next
output:
cscript 19915175.vbs
---- For Each In
Variant() A B C
Variant() D E F
Variant() I J K
---- For To
Variant() A B C
Variant() D E F
Variant() I J K
ReDim Preserve answer(UBound(answer) + 1):
No problems with an ArrayList Of ArrayLists of Arrays (as long we are talking about read access and you don't mess it up):
Dim alB : Set alB = CreateObject("System.Collections.Arraylist")
alB.Add alA
WScript.Echo "alB(0)(0)(0) =>", alB(0)(0)(0)
WScript.Echo "alB(0)(2)(2) =>", alB(0)(2)(2)
output:
alB(0)(0)(0) => A
alB(0)(2)(2) => K
The code below is a fragment of a macro I'm writing to alert users that they may not have a sufficient number of compatible installation equipment on an order. n shortfalls (i.e., each instance of an equipment item not having compatible installation equipment are saved in the yankees() array in elements 1 to n. What I want to do is prompt users with a message box stating "Please review your order to ensure you have you sufficient compatible installation equipment- we detected the following shortfalls"
and below that
include all each element of yankees(1 to n) on separate lines in a message box with two options below "This is okay, I'll submit my order now" and "Let me go back,I want to modify my order".
How can I create such a message box?
I have:
MsgBox "Please review your order to ensure you have you sufficient compatible installation equipment- we detected the following concerns" & yankee(1), vbOKCancel
currently but this only includes the first shortfall. How can I include all elements of yankee() and put them on their own line?
This question really boils down to: "How do I put all non-blank elements of an array variable onto their own lines in a message box prompt"?
Do
If rip(qbert) < k(qbert) Then
yankee(jets) = "Your order for" & s(qbert) & " contains " & k(qbert) - rip(qbert) & " too few " & g(qbert)
jets = jets + 1
qbert = qbert + 1
Else
qbert = qbert + 1
End If
Loop Until qbert > echo
You can use the Join function:
Sub Test()
Dim var As Variant
'Populate a dummy vector array from a comma-separated list:
var = Split("Alpha,Beta,Gamma,Delta,Epsilon", ",")
'Display the contents of the array as delimited list, use the Carriage Return to delimit:
MsgBox Join(var, vbCR)
End Sub
The above does not ignore blanks. To ignore blanks, per your specific question, you can iterate over the array and test for blank values. I would do this in a Function:
How do I put all non-blank elements of an array variable onto their own lines in a message box prompt
In your sub, just pass yankees to this function, like:
MsgBox = GetMessageText(yankees)
Here is the function:
Function GetMessageText(var As Variant) As String
'Assumes a vector array
On Error GoTo EarlyExit
Dim sMsg As String
Dim v As Variant
For Each v In var
If Not v = vbNullString Then
sMsg = sMsg & v & vbCr
End If
Next
EarlyExit:
If Err.Number = 0 Then
GetMessageText = sMsg
Else:
GetMessageText = "invalid array"
End If
End Function
Alternate:
Sub tgr()
Dim yankee(1 To 5) As String
Dim strMsg As String
yankee(1) = "Did this"
yankee(3) = "experiment"
yankee(5) = "really work?"
'yankee => ["Did this", , "experiment", , "really work?"] _
the yankee array has two blanks at positions 2 and 4 _
and it also has spaces in some of the element strings
strMsg = Replace(Replace(WorksheetFunction.Trim(Replace(Replace(Join(yankee, "|"), " ", "_"), "|", " ")), " ", vbCrLf), "_", " ")
'strMsg => "Did this _
experiment _
really work?"
'Yes it did, see result
MsgBox strMsg
End Sub