VBA code works fine in debug mode but not when I run it
VBA code works fine in debug mode but not when I run it
(OP)
Hi,
I'm new to VBA and it's really hard to any information online. I have written a code to basically scrape values from the DD.3 screen and paste them into excel sheet.
The code executes without any issues in debug mode, but the cursor seems to have a mind of its own when I run the code.
Any help would be much appreciated.
Sub Main()
Dim Sys As Object, Sess As Object, MyScreen As Object, MyArea As Object
Dim oWS As Worksheet
Set Sys = CreateObject("EXTRA.System")
Dim SIN As String
' Assumes an open session
Set Sess = Sys.ActiveSession
Set MyScreen = Sess.Screen
Set oWS = ThisWorkbook.Sheets("DATA") '... Stores reference to data worksheet
Row = 2
lastRow = oWS.Cells(50000, 1).End(xlUp).Row
For Row = 2 To lastRow
SIN = Trim(oWS.Cells(Row, 1))
MyScreen.MoveTo 1, 20
MyScreen.SendKeys (SIN)
MyScreen.SendKeys ("<enter>")
Do Until Len(MyScreen.Search("T4 ")) > 0 Or MyScreen.GetString(2, 2, 6) = "ME 039"
MyScreen.MoveTo 1, 1
MyScreen.SendKeys ("<pf8>")
Loop
If Len(MyScreen.Search("T4 ")) > 0 Then
MyScreen.MoveTo MyScreen.Search("T4 ").Bottom, MyScreen.Search("T4 ").Right - 12
MyScreen.SendKeys ("x")
MyScreen.SendKeys ("<enter>")
Do Until MyScreen.GetString(1, 67, 3) <> "T4 " Or MyScreen.GetString(2, 2, 6) = "ME 039"
For i = 2 To 15
If MyScreen.GetString(1, 67, 3) = "T4 " Then
oWS.Cells(Row, i) = MyScreen.GetString(21, 16, 10)
MyScreen.SendKeys ("<pf8>")
If MyScreen.GetString(2, 2, 6) = "ME 039" Then
i = 15
End If
End If
Next i
Loop
If MyScreen.GetString(2, 2, 6) = "ME 039" Then
End If
End If
Next
End Sub
I'm new to VBA and it's really hard to any information online. I have written a code to basically scrape values from the DD.3 screen and paste them into excel sheet.
The code executes without any issues in debug mode, but the cursor seems to have a mind of its own when I run the code.
Any help would be much appreciated.
Sub Main()
Dim Sys As Object, Sess As Object, MyScreen As Object, MyArea As Object
Dim oWS As Worksheet
Set Sys = CreateObject("EXTRA.System")
Dim SIN As String
' Assumes an open session
Set Sess = Sys.ActiveSession
Set MyScreen = Sess.Screen
Set oWS = ThisWorkbook.Sheets("DATA") '... Stores reference to data worksheet
Row = 2
lastRow = oWS.Cells(50000, 1).End(xlUp).Row
For Row = 2 To lastRow
SIN = Trim(oWS.Cells(Row, 1))
MyScreen.MoveTo 1, 20
MyScreen.SendKeys (SIN)
MyScreen.SendKeys ("<enter>")
Do Until Len(MyScreen.Search("T4 ")) > 0 Or MyScreen.GetString(2, 2, 6) = "ME 039"
MyScreen.MoveTo 1, 1
MyScreen.SendKeys ("<pf8>")
Loop
If Len(MyScreen.Search("T4 ")) > 0 Then
MyScreen.MoveTo MyScreen.Search("T4 ").Bottom, MyScreen.Search("T4 ").Right - 12
MyScreen.SendKeys ("x")
MyScreen.SendKeys ("<enter>")
Do Until MyScreen.GetString(1, 67, 3) <> "T4 " Or MyScreen.GetString(2, 2, 6) = "ME 039"
For i = 2 To 15
If MyScreen.GetString(1, 67, 3) = "T4 " Then
oWS.Cells(Row, i) = MyScreen.GetString(21, 16, 10)
MyScreen.SendKeys ("<pf8>")
If MyScreen.GetString(2, 2, 6) = "ME 039" Then
i = 15
End If
End If
Next i
Loop
If MyScreen.GetString(2, 2, 6) = "ME 039" Then
End If
End If
Next
End Sub
RE: VBA code works fine in debug mode but not when I run it
I'd suggest you READ, fully UNDERSTAND and then IMPLEMENT the Wait measures you absolutely need to include in your code, the FAQ link below.
The tip-off was in "The code executes without any issues in debug mode, but the cursor seems to have a mind of its own when I run the code."
When you're in Debug Mode, the mainframe has a virtual ETERNITY to process each statement, but when your code is operating seemingly at WARP SPEED, compared to what's happening between your PC and the mainframe (I/O) and in the mainframe, all happening in a "black box" of indeterminable duration, your code is haplessly plunging into the next statement instead if WAITING for feedback to GO!
Naturally, if you need help implementing these measures in your code, please do not hesitate to ask for help after you read the FAQ.
FAQ99-7887: Why WAIT? Or how long should I WAIT?
BTW, you have 6 SendKeys commands to compensate for.
Skip,
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: VBA code works fine in debug mode but not when I run it
CODE
Skip,
Just traded in my OLD subtlety...
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: VBA code works fine in debug mode but not when I run it
Skip,
Just traded in my OLD subtlety...
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: VBA code works fine in debug mode but not when I run it
Skip,
Just traded in my OLD subtlety...
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
RE: VBA code works fine in debug mode but not when I run it
Skip,
Just traded in my OLD subtlety...
for a NUance!
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein