VB2010 - For Loop Exiting Issue - vb.net

I have a simple for loop with the following code
For i As Integer = 0 To 4
Snake(i).X = (120 - 20 * i)
Snake(i).Y = 120
SnakeBody(i).Location = New Point(Snake(i).X, Snake(i).Y)
Snake(i).Facing = 3
Next i
But for some reason I unable to debug it. I place a breakpoint on the line Snake(i).X = (120 - 20 * i) and When I try to see what the values are the second time the loop iterates it simply exits the loop. Any Ideas?
Thanks

Place a breakpoint on the first line of the loop and step through it line by line.

Related

How to return SAP error message in excel cell through VBA?

I wrote an automation script that uses the following SAP GUI:
objSess.findById("wnd[0]").Maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "flqaf"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]").sendVKey 17
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").CurrentCellColumn = "TEXT"
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").SelectedRows = "0"
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").DoubleClickCurrentCell
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").Text = W_BPNumber
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").SetFocus
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 8
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/tbar[0]/btn[3]").press
Everything works fine, however sometimes the problem aborts in SAP and exactly when that happens I want to capture it by writing the term "error" into an excel cell.
I tried adding this line in vba
objSheet.Cells(iRow, 5) = GuiStatusbar.Text
As well as
objSheet.Cells(iRow, 5) = objSessFindById("wnd[0]/sbar").Text
The code still runs fine but my cells in column 5 remain empty. Any ideas how to solve this?
It might help if you wait a little.
fo example:
...
objSess.findById("wnd[0]").sendVKey 0
waitTill = Now() + TimeValue("00:00:01")
While Now() < waitTill
DoEvents
Wend
if objSess.findById("wnd[0]/sbar").messageType = "E" then 'other types: W, I, S, A
objSheet.Cells(iRow, 5) = objSessFindById("wnd[0]/sbar").Text
end if
objSess.findById("wnd[0]/tbar[0]/btn[3]").press
...
Regards, ScriptMan

For Loop Step -1

I want to know why this loop doesn't show anything in VB.NET.
I think this code will create an infinite loop. But it doesn't show anything.
Dim i as Integer
For i = 1 to 3 Step - 1
MessageBox.Show(i)
Next
Is that loop different with this code (in java/c#) ?
for(int i = 1;i <= 3;i--)
{
// print i
}
http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
With a negative step size the loop only executes if counter >= end. So in this case with i = 1, that is less than the ending value so the loop doesn't execute at all.
It doesn't show anything because you are running the counter backwards without reversing the start and end conditions.
Think of the loop like this:
Dim counter As Int32 = 1
Do
If counter <= 1 Then
Exit Do
End If
Console.WriteLine("The counter is at " & counter)
counter +=1
Loop
Obviously this won't work properly.
You need to reverse the start and end conditions:
For counter = 3 To 1 Step -1
Console.WriteLine("counter: " & counter)
Next
For i = 1 to 3 Step - 1
It won't create an infinite loop. The loop will simply be skipped, because you can't get from 1 to 3 with a step value of -1.
Is that loop different with this code (in java/c#) ?
This loop will also end immediately, because the initial value (i = 1) meets the exit condition (i <= 3).

VBA Excel Transform to loop

Here i have my hundred line of code please enlighten me about how do i put line code into loop
here is my try but i wont work out still trying
If sp.Name Like "Rounded Rectangle*" Or sp.Name Like "Oval*" Then
For i = 11 To 100
x = i - 9
Sheet2.Shapes.Range(Array("Rounded Rectangle " + i)).TextFrame.Characters.Text = Sheet1.Range("A" + x)
Next i
End If
and repeat until X = 110
in this case how can i change it in to correct loop please advice
thank you
This is a general approach to making a loop to cover a string variable....say we want to loop over Shape("Rectangle 1")....Shape("Rectangle 2")....Shape("Rectangle 3)..... , etc.
Dim str As String, i As Long
For i = 11 To 100
str = "Rectangle " & CStr(i)
Sheets2.Shapes(str)................
Next i
and use a similar approach to make "A2"..."A3".........
simple math:
For i = 11 To 100
change to
For i = 11 To 119

MS Word 2013 crash when objects are generated / deleted

I'm using Word 2013 to generate some objects and also delete them before a new generation. But sometimes Word crashes in such case, that all the VBA code is dropped. Here is the code for adding the generated objects:
For i = 1 To nodes
Set arrShapes(j) = docNew.Shapes.AddShape(MsoAutoShapeType.msoShapeDiamond, arrRawPoints(i, 1) - 2, arrRawPoints(i, 2) - 2, 4, 4)
arrShapes(j).title = "A" + Str(j) + "d"
arrShapes(j).Fill.ForeColor.RGB = RGB(255, 0, 0)
j = j + 1
Next i
The deleting code is as follows:
For Each sp In arrShapes
If Not (sp Is Nothing or IsEmpty(sp)) Then
tl = Left(sp.title, 1)
If tl = "A" Then
tl = Mid(sp.title, 2, Len(sp.title) - 2)
nr = Int(tl)
sp.Delete
Set arrShapes(nr) = Nothing
End If
End If
Next sp
Sometimes there occurs a crash, but if I call this routine 50 times or more, it runs perfectly. It happens that a user deletes such a generated object by hand, then I've got a crash. To find the reason, I've set a breakpoint on the 1st line within the For Each loop, but then Word crashes every time. What's wrong in this concept?
I think solution is quite obvious- when deleting you need to loop from last item to first therefore you need to switch into different kind of loop. Your (second) code could looks as follows:
For i=arrShapes.Count to 1 step -1
'you dont need if statement here
tl = Left(arrShapes.Item(i).Title, 1)
if tl = "A" then
tl = Mid(arrShapes.Item(i).Title, 2, Len(arrShapes.Item(i).Title) - 2)
'rather dont' need it any more: nr = Int(tl)
arrShapes.Item(i).Delete
'rather dont' need it any more: Set arrShapes(nr) = Nothing
end if
Next i
Not tested! Some adjustment can be required as not everything is clear in your code.

Not completed file read

I'm trying to debug what went wrong in my code. My file.txt contains 1763 lines but when I run it, it always ends up not completed. Always stops somewhere at 1680 and up (printed by the row in my code); the thing is it stops at different line every time I run it, so I don't think the problem's with my text file.
row = 0
for line in io.lines("file.txt") do
row = row+1
local new_row1 = {}
for n in line:gmatch'%S+' do
table.insert(new_row1, tonumber(n))
end
if #new_row1 > 0 then
table.insert(input, new_row1)
end
print(row)
end
Is there something wrong in my code?
It looks like in your code, you opened a file handle to "file.txt" at the beginning of your script and it remains open till the end where you close the file. During that time, you attempt to reopen "file.txt" again in your loop which is causing the strange behavior you're seeing.
When I moved your file open and close scopes to the middle section after first loop but before the last outer loop, that fixes the issue:
file = assert(io.open("file.txt", "w"))
for i = 1, 1000 do
j = math.random(i, row-one)
u[i], u[j] = u[j], u[i]
for k = 1, 11 do
file:write(input2[u[i]][k], " ")
end
file:write"\n"
end
num = (row-one)+1
for i = 1, one do
for k=1, 11 do
file:write(input2[num][k], " ") --writes to the file all the rows starting from where '1' in column11 was seen
end
file:write("\n")
num = num + 1
end
file:close()
-----------------------------------Access file.txt.--------------------------
-- ...
This gives the expected output:
Done 1762 1762
--------------------------