vbScript - Using Select/Case with an array - vbscript

I need help with the following:
Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"
I want to execute certain actions for each item in the array list. So I was thinking of working with a Select/Case. I tried this but didn't work:
Select Case reportesTA
Case 0
//do stuff
Case 1
//do stuff
End Select
Is there any way to get the cases switch? something like a switch/case? Anyone has a better way to work with each item of the array? Thanks very much.

Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"
For I = LBound(reportesTA) to UBound(reportesTA)
Select Case reportesTA(I)
Case "Report1"
MsgBox "Report1"
Case "Report2"
MsgBox "Report2"
End Select
Next
Explanation:
In "FOR" cycle we passing through of all elements in array, starting from first one till last one.
function "LBound" return lowest ID of available element in array.
function "UBound" return highest ID of available element in array.
In "SELECT CASE" we taking value (yes, it has string type) of element from array and then making decision what we should do -- in this sample we popup message box that displaying report name.

You need to de the Select case with an element of the array, not the array itself.
I would do it like this, more clear as to what you are doing and more DRY.
reportesTA = Array("Report1", "Report2")
Sub do_stuff(text)
WScript.echo text
End Sub
For each element in reportesTA
Select Case element
Case "Report1"
do_stuff "Report1"
Case "Report2"
do_stuff "Report2"
End Select
Next
In this example you would rather do the following
For each element in reportesTA
do_stuff element
Next

Related

Parse word document in VBScript

I got a weird mission from a friend, to parse through a bunch of Word files and write certain parts of them to a text file for further processing.
VBscript is not my cup of tea so I'm not sure how to fit the pieces together.
The documents look like this:
Header
A lot of not interesting text
Table
Header
More boring text
Table
I want to parse the documents and get all the headers and table of contents out of it. I'm stepping step through the document with
For Each wPara In wd.ActiveDocument.Paragraphs
And I think I know how to get the headers
If Left(wPara.Range.Style, Len("Heading")) = "Heading" Then
But I'm unsure of how to do the
Else if .. this paragraph belongs to a table..
So, any hint on how I could determine if a paragraph is part of a table or not would be nice.
Untested, because I have no access to MS Word right now.
Option Explicit
Dim FSO, Word, textfile, doc, para
' start Word instance, open doc ...
' start FileSystemObject instance, open textfile for output...
For Each para In doc.Paragraphs
If IsHeading(para) Or IsInTable(para) Then
SaveToFile(textfile, para)
End If
Next
Function IsHeading(para)
IsHeading = para.OutlineLevel < 10
End Function
Function IsInTable(para)
Dim p, dummy
IsInTable = False
Set p = para.Parent
' at some point p and p.Parent will both be the Word Application object
Do While p Is Not p.Parent
' dirty check: if p is a table, calling a table object method will work
On Error Resume Next
Set dummy = obj.Cell(1, 1)
If Err.Number = 0 Then
IsInTable = True
Exit Do
Else
Err.Clear
End If
On Error GoTo 0
Set p = p.Parent
Loop
End Function
Obviously SaveToFile is something you'd implement yourself.
Since "is in table" is naturally defined as "the object's parent is a table", this is a perfect situation to use recursion (deconstructed a little further):
Function IsInTable(para)
IsInTable = IsTable(para.Parent)
If Not (IsInTable Or para Is para.Parent) Then
IsInTable = IsInTable(para.Parent)
End If
End Function
Function IsTable(obj)
Dim dummy
On Error Resume Next
Set dummy = obj.Cell(1, 1)
IsTable = (Err.Number = 0)
Err.Clear
On Error GoTo 0
End Function

Ruby Search Array And Replace String

My question is, how can I search through an array and replace the string at the current index of the search without knowing what the indexed array string contains?
The code below will search through an ajax file hosted on the internet, it will find the inventory, go through each weapon in my inventory, adding the ID to a string (so I can check if that weapon has been checked before). Then it will add another value after that of the amount of times it occurs in the inventory, then after I have check all weapon in the inventory, it will go through the all of the IDs added to the string and display them along with the number (amount of occurrences). This is so I know how many of each weapon I have.
This is an example of what I have:
strList = ""
inventory.each do |inv|
amount = 1
exists = false
ids = strList.split(',')
ids.each do |ind|
if (inv['id'] == ind.split('/').first) then
exists = true
amount = ind.split('/').first.to_i
amount += 1
ind = "#{inv['id']}/#{amount.to_s}" # This doesn't seem work as expected.
end
end
if (exists == true) then
ids.push("#{inv['id']}/#{amount.to_s}")
strList = ids.join(",")
end
end
strList.split(",").each do |item|
puts "#{item.split('/').first} (#{item.split('/').last})"
end
Here is an idea of what code I expected (pseudo-code):
inventory = get_inventory()
drawn_inv = ""
loop.inventory do |inv|
if (inv['id'].occurred_before?)
inv['id'].count += 1
end
end loop
loop.inventory do |inv|
drawn_inv.add(inv['id'] + "/" + inv['id'].count)
end loop
loop.drawn_inv do |inv|
puts "#{inv}"
end loop
Any help on how to replace that line is appreciated!
EDIT: Sorry for not requiring more information on my code. I skipped the less important part at the bottom of the code and displayed commented code instead of actual code, I'll add that now.
EDIT #2: I'll update my description of what it does and what I'm expecting as a result.
EDIT #3: Added pseudo-code.
Thanks in advance,
SteTrezla
You want #each_with_index: http://ruby-doc.org/core-2.2.0/Enumerable.html#method-i-each_with_index
You may also want to look at #gsub since it takes a block. You may not need to split this string into an array at all. Basically something like strList.gsub(...){ |match| #...your block }

VBA code with multiple For loops running very slow

my code is running very slow, is it the 2 For loops causing it?
Thanks
For x = LBound(dataArray) To UBound(dataArray) 'define start and end of array, lower bound to upper bound
For Each rngcell In Range("A:B") 'lookup each cell in row 1
If dataArray(x) = rngcell.Value Then ' if cells in header row match with values in array
rngcell.EntireColumn.Copy ' then copy whole column of data for that parameter
Sheets(3).Select ' select sheet to paste data
Range("A:B").End(xlUp).Offset(rowOffset:=0, columnOffset:=x).Select 'select area to paste, paste in next column - no. x
Selection.PasteSpecial xlPasteValues ' paste
End If
Next rngcell ' next header row cell
Next x
End Sub
Just a few suggestions:
Doing .Select causes Excel to update the UI, which is expensive. Try to calculate the target cell/range ONCE and use that to call PasteSpecial and not Selection.
Selecting the Sheet(3) could be done before the loop, as it doesnt change.
IF (!) max. ONE dataArray Element matches ONE rngcell.Value, you could abort the rest of the inner loop by using Exit For before the End If, saving the useless rest of the loop.
You're using Range(A:B) which is definitely slowing your code down.
Excel will basically read every cell in that range according to your code.
That's 2 million cells.
Try to limit the range on the B by using something like Replace(Range("B").End(Xldown).Address,"$B$","").

Search WebList for particular item count from Excel sheet

I need a help in VBScript or either in QTP for the below case.
For example:
I have nearly 40 items in the weblist. I have only one item in the Excel sheet that is one among the 40 in the weblist. If I run the script, the one in the Excel should be select in the weblist. How do I perform this? I tried many scenarios, but couldn't get it to work.
Below are some of the sample pieces of code I tried in QTP:
ocount=Browser("name:=brw").Page("title:=brw").WebList("htmlid:=tabContainerBrandSite_123&rtyoh").GetROProperty("items count")
msgbox ocount
var7=mySheet2.Cells(2,"C")
For k=2 to ocount
ocount2=Browser("name:=brw").Page("title:=brw").WebList("html id:=tabContainerBrandSite_123&rtyoh").GetItem(k)
msgbox ocount2
merchantName = DataTable("Merchant_Name","Global") 'an example if value is saved in global sheet
items_count = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetROProperty("Items Count") 'This will get all the items from your weblist.
i = 1
Do
webListName = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetItem(i)
'this will get first value from the web list
If merchantName = webListName Then 'comparing first value from your value from global sheet
Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").Select(i) 'selects that value
Exit do 'because it has found your only value from the local sheet, it exits
else
i = i + 1
End If
Loop While i <= items_count

VBscript select case for combobox

He there, I'm new to VB script and I am trying to make a select case to change an emailadress to a combobox choice. I know i'm thinking to easy but i need help with the direction. the combobox works but i cannot get the value chosen in the combobox to trigger the select case.
Sub Item_Open()
Set FormPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = FormPage.Controls("Subject")
Control.PossibleValues = "SPOED;STATUS;MANCO;KLACHT;TRANSPORT;TRACKING;INKOMEND;REPARATIE;RETOUREN;LADEN;MILKRUN"
Set MyPage = Item.GetInspector.ModifiedFormPages("Message")
Set Mail = MyPage.Item("Subject").Value
Select Case Mail
Case SPOED
Item.To = "hihi#blabla.com"
Case STATUS
Item.To = "haha#blabla.com"
Case else
Item.To = ""
End Select
End Sub
Assuming MyPage.Item("Subject").Value returns a string value like "STATUS". Then you must pick it up in a string variable:
strMail = MyPage.Item("Subject").Value ' look ma, no Set!
The Select Case X statement evaluates the X expression and compares it to the values Y, ... in the Case Y parts and executes the first block for which (the values of) X and Y are equal. To apply this to strMail containing strings like "SPOED", the Case Y expressions should be string literals:
Select Case strMail
Case "SPOED"
...
Case "STATUS"
...
If you would have used Option Explicit, VBScript would have told, that SPOED (etc) is (understood as) an unitialized variable that can't be equal to a (decent) strMail.

Resources