Dictionary Item not populating correctly - vba

I am out of wits on this one. I have a list like below that is available on the fly. I want to be able to create an array or a collection so that I could use it further in a loop.
Xob::SISBTXTRPR-5298
Xob::SISBTXTRPR-5326
Xob::SISBTXTRPR-5327
Xob::SISBTXTRPR-5328
Yob::SISBTXTRPR-3999
Yob::SISBTXTRPR-4000
I have tried to create a dictionary item like below but the output is not what I am expecting
While i <= iPuntedIssuesCount
If sRemovedStoriesForTaskTracker.Exists(Trim(JSONObj("contents")("puntedIssues")(i)("assigneeName"))) Then
sRemovedStoriesForTaskTracker(Trim(JSONObj("contents")("puntedIssues")(i)("assigneeName"))) = sRemovedStoriesForTaskTracker(Trim(JSONObj("contents")("puntedIssues")(i)("assigneeName"))) & " + " & JSONObj("contents")("puntedIssues")(i)("key")
Else
sRemovedStoriesForTaskTracker.Add key:=Trim(JSONObj("contents")("puntedIssues")(i)("assigneeName")), Item:=JSONObj("contents")("puntedIssues")(i)("key")
End If
i = i + 1
Wend
sRemovedStoriesForTaskTracker is the dictionary item.
The output in the dictionary items that I am getting is like so:
Xob::SISBTXTRPR-5298 + SISBTXTRPR-5326 + SISBTXTRPR-5327 + SISBTXTRPR-5328.
I want the dictionary to populate like this
Xob::SISBTXTRPR-5298
Xob::SISBTXTRPR-5326
Xob::SISBTXTRPR-5327
Xob::SISBTXTRPR-5328

You don't show your source JSON, or how you're outputting the dictionary values, and your posted code doesn't seem to match what you say you want to do.
In any case, your code as-posted would benefit hugely from the use of a few variables as noted by #Damian. It's so dense it's difficult to follow.
While i <= iPuntedIssuesCount
Set d = JSONObj("contents")("puntedIssues")(i)
k = Trim(d("assigneeName"))
v = d("key")
If dictRemoved.Exists(k) Then
dictRemoved(k) = dictRemoved(k) & " + " & v
Else
dictRemoved.Add k, v
End If
i = i + 1
Wend

Related

add names of two variables and make a new dynamic variable

I am writing a code where I have a for loop in which I give a variable (named VType ) some value. For loop goes for a range of i variables. Now I want to make a new variable by concatenating names of both variables. for example if i = 1 then I want to make variable VType1. Here is my piece of code.
nrec = Split(Split(ie.document.body.innerHTML, "Found <strong>")(1), "</strong> records")(0)
If nrec = 1 Then
lnk.Click
Else
For j = 1 To nrec
link.Click
Do While ie.readyState <> 4: Wait 5: Loop
Application.Wait (Now + TimeValue("0:00:01"))
'VType , j = GetType
'Application.Wait (Now + TimeValue("0:00:01"))
IMO , j = GetValue("IMO:")
'MMSI = GetValue("MMSI:")
YBuilt , j = GetValue("Year Built:")
Flag , j = GetValue("Flag:")
DWT , j = GetValue("Deadweight:")
Next j
num = "1 - " & IMO1
For i = 2 To nrec
num = num & vbCrLf & i & "abc"
Next I
fin = InputBox(num, nrec & " records found for a. please select right one.")
Exit For
End If
There is not a way to directly do what you're specifically requesting. However, you can use arrays to get a similar outcome. Arrays are a not a topic that can be explained in a single posted answer, but if you do a little research you can probably figure out how the below might be useful...
Dim VTtyp(0 to i) as string
'while Looping...
Vtype(i) = "Whatever you want stored in this round of i"
When your code completes, you'll have all fields saved as variables that can be called from this array. An example is if you wanted to call the one that was tied to the number "2" you could type: Vtype(2) and it would call the text from the 2 iteration.
Again this example is extremely simplified and there are things to consider such as dim size, changing the dim, preserving the array, etc. and that is something you'll have to research further. However bottom line is, "there is not a way to do what you're specifically trying to do."
You can achieve this using Dictionary objects concept. Go through the below link to know more about dictionary objects.
https://www.tutorialspoint.com/vbscript/vbscript_dictionary_objects.htm

Loop through Excel Ranges Filling Combobox VB.Net

I have a program that needs to iterate through a very large excel range, to combine two ranges into one combo box value. I have the following code to do so, but all it does is iterate the first value five times. If I remove the first FOR loop, then it simply only returns the first value and never completes. Any suggestions on what I can do to polish this code up and get it working?
Dim i As Integer
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\Schedule.xlsx")
For i = 0 To 5 Step +1
For Each CNSCell In WFMBook.Range("A3:A1441").Cells
f = CNSCell.Value.ToString
Next
For Each tst In WFMBook.Range("B3:B1441").Cells
l = tst.Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
If (i = 5) Then
Exit For
End If
Console.WriteLine(i)
Next
End If
Try this, just change the x to 1 if VBA is 1 based.
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\XHSchedule.xlsx")
Dim colCount = WFMBook.Range("A3:A8").Cells.Count
For x = 1 To colCount Step +1
For Each CNSCell In WFMBook.Range("A3:A8").Cells
f = WFMBook.Range("A3:A8").Cells(x).Value.ToString
l = WFMBook.Range("B3:B8").Cells(x).Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
Next
End If

Making text go to a new line when in a while loop

I have a richTextbox, and a while loop, x = 0, and every time this loop runs, x += 1 till it reaches a certain value.
Heres what I want to happen:
while x <> 10 then
it will say item 0 +1 on a new line, and then item 1 + 1 on the line under it, etc, so you will see all 10 values after.
What happens is, it will change that line into the new value.
My question is: How do I make it put the words on a new line instead?
Here is my code:
While readListItem <> PaymentList.Items.Count
If readListItem > PaymentList.Items.Count Then
readListItem = 0
Exit While
End If
readListItem += 1
MessageBox.Show(readListItem)
resultBox.Text = vbNewLine + PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "." + vbNewLine
End While
readListItem is "x", and that is being iterated by 1 every time the loop runs
PaymentList is my listbox containing an unknown value (the user sets the number of items in this list)
The If if there because, after x = lets say 10, it would add another to x (so now it = 11) and try to print out the 11th item, as it doesnt exist, it crashes. This is to attempt to prevent that. I didnt want to go with a try catch
Try adding the new value instead of replacing the entire value:
resultBox.Text &= Environment.NewLine() & PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "."
The expression &= is equivalent, in this case, to resultBox.Text = resultBox.Text & "...".
And once advice, you can simplify your loop by using this condition:
While readListItem < PaymentList.Items.Count
'...
End While

Check if each line from a text file contains a certain string, and add the ones that do to a listbox?

The following code works, to simply get each line as a line in the listbox.
Reader = IO.File.OpenText(textlocation)
Dim bookmarks() As String = Reader.ReadToEnd.Split(vbNewLine)
Dim i As Integer = 0
Do Until i = bookmarks.Length
lstFavorites.Items.Add(bookmarks(i))
i += 1
Loop
But I don't want every line to go into the text box. I only want the lines that contain the text "Bookmark" to go into the listbox. What can I do to achieve this? I've tried everything I can think of.
Heres some code I tried, I can't see the problem in it, but it seems to just crash my program.
Do Until i = bookmarks.Length
If bookmarks(i).Contains("at") Then
If radBookmarks.Checked Then
If bookmarks(i).Contains("Bookmark") Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
Else
i += 1
End If
ElseIf radEverything.Checked Then
Original = bookmarks(i)
BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
DemoName = BeginningOfDemoName.Split(Chr(34))(0)
ToList = DemoName + " at " + Tick
lstFavorites.Items.Add(ToList)
i += 1
End If
End If
Loop
Try to change this line
If bookmarks(i).Contains("Bookmark") Then
to
If bookmarks(i).IndexOf("Bookmark",
StringComparison.CurrentCultureIgnoreCase) >= 0 Then
Contains do a case sensitive comparison and your input string contains a lower case 'bookmark'

A better way of writing this : growing array

Was looking at some code earlier, and am thinking that there has to be a more elegant way of writing this....
(returnVar.Warnings is a string array, it could be returned as any size depending on the number of warnings that are logged)
For Each item In items
If o.ImageContent.ImageId = 0 Then
ReDim Preserve returnVar.Warnings(returnVar.Warnings.GetUpperBound(0) + 1)
returnVar.Warnings(returnVar.Warnings.GetUpperBound(0)) = "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString
End If
Next
use the generic List(of string) then get an array containing the list data if you need it
dim list = new List(of string)
list.Add("foo")
list.Add("bar")
list.ToArray()
Can't you use ArrayList which does this for you?
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx
Start by moving the If statement out of the loop.
If you are using framework 3.5, you can use LINQ to loop the items.
If o.ImageContent.ImageId = 0 Then
returnVar.Warnings = items.Select(Function(item) "Section: " & section.<header>.<title>.ToString & " , Item: " & item.<title>.ToString).ToArray()
Else
returnVar.Warnings = New String() {}
End If