Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA code works fine in debug mode but not when I run it

Status
Not open for further replies.

dhsd

Programmer
Aug 21, 2020
1
CA
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



 
Hi,

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

BTW, you have 6 SendKeys commands to compensate for.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
...also you appear to be misusing SendKeys when all you intend is to place characters on MyScreen...
Code:
[s]SIN = Trim(oWS.Cells(Row, 1))[/s]  'you really don't appear to use this variable again
[s]MyScreen.MoveTo 1, 20
MyScreen.SendKeys (SIN)[/s]
MyScreen.[b]PutString[/b] Trim(oWS.Cells(Row, 1)), 1, 20
MyScreen.SendKeys ("<enter>")
Do Until WaitForCursor([b][highlight #8AE234]row, col[/highlight][/b]) 
    DoEvents 
Loop
Of course you'll need to use the [highlight #8AE234]Cursor Rest Coordinates[/highlight] for the screen that returns after the SendKeys executes.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
did you get it working?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hello? Are you there?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Guess you've given up!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top