×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help needed with syntax/logic in Attachmate macro

Help needed with syntax/logic in Attachmate macro

Help needed with syntax/logic in Attachmate macro

(OP)
Hello Again!

After a few years since my last post I've been able to adapt the original macro that was created with great help from the community and make it perform numerous functions. Now, I'm looking to essentially have an extension of that original macro in excel screen scrape some different data from the Attachmate session but have run into a roadblock with the logistics.

The below code (still a work in progress and apologies as the code feature was presenting an error) has 'PO' as the input from excel into Attachmate to begin the process. The Attachmate session will then return records based off the 'PO' value into rows 8-21 on the screen. The macro will go record by record and look for location 'S' to be null while location 'I' has a value, it will then use input the selection code 'I' value to move into another screen for that specific record. This secondary screen will have more detailed information which will be screen scraped into excel before sending the PF3 function key to return to the list of records. Once the macro reaches the last record in row 21, it will then use the PF8 function key to move onto the next page of records and will loop the process. The issue I am running into is that for records on the second screen the macro will use the 'I' value to go into a record's secondary screen and scrape the data correctly but the PF3 function key returns to the original/first page of records and would start looping through that page again rather than moving back to the second page and continuing on with the next record. There are no identifiers between the pages (i.e. a location in the session that says page 1 of 2 or something similar) which I could have the macro look for unfortunately so I'm thinking there needs to be syntax built into the macro to use the PF8 function key the necessary amount of times to return to the screen it left off on but I'm not familiar with what the best method would be or really how to do that. Any ideas are appreciated!

Sub RecordLookUp()
Dim Sessions, System As Object, Sess0 As Object, PO As String, WB As Workbook, WS As Worksheet
Set System = CreateObject("EXTRA.System")
Set Sessions = System.Sessions
Set Sess0 = System.ActiveSession
Set WB = ActiveWorkbook
Set WS = Sheets("Look-up")

rw = 2
rw1 = 1
With Worksheets("Look-up")
Do
For X = rw To ActiveSheet.Rows.Count
PO = Left(Sheets("Look-up").Cells(X, 1).Value, 9)
If PO = "000000000" Then Exit Sub

Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("BA")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("BA")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("CD")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
'Input value from Excel to Attachmate
Sess0.Screen.MoveTo 3, 44
Sess0.Screen.SendKeys PO
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Do
For r = 8 To 21
dd = 1
S = Trim(Sess0.Screen.GetString(r, 33, 8))
I = Trim(Sess0.Screen.GetString(r, 2, 2))
ML = Trim(Sess0.Screen.GetString(2, 23, 7))

'If both S and I are null -> end
If S = "" And I = "" Then
Exit Do

'If S is null but I is not, move into secondary screen with more detailed information
ElseIf S = "" Then
Sess0.Screen.MoveTo 5, 18
Sess0.Screen.SendKeys I
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
If ML = "INQUIRY" Then
CONT = Sess0.Screen.GetString(4, 28, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(18, 64, 8)
Date2 = Sess0.Screen.GetString(18, 73, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(8, 23, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 78, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

Else
CONT = Sess0.Screen.GetString(4, 28, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(20, 21, 8)
Date2 = Sess0.Screen.GetString(20, 31, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(9, 72, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 77, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

End If

'Return to records screen
Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
'Exit Do

End If

Next r

'Next page
Sess0.Screen.SendKeys ("<Pf8>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

Loop

Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

Next X 'next row/group
Loop
End With





End Sub

RE: Help needed with syntax/logic in Attachmate macro

Hi there, duckin' the wind sweepin cross the plane, 🥴

Worked at a place that had DOZENS of screen-scrape macros, like your situation, in Excel. I started getting folks wanting selected data abc, from screen xyz, given list mno, thus generating macro qrs.

After a half-dozen of these requests, I wondered if I could make a generalized macro, where any complete screen could be specified by DATA, and the screen data output to a specific worksheet in the screen-scrape workbook given an input sheet specifying the SCREEN and the INPUT DATA TABLE.

It's much simpler to pick data columns in Excel to report than modifying an existing macro to define columns and rows to scrape.

Unfortunately, I've lost my MASTER.xlsm workbook.

That being said, you mention PF3 not getting you a second page. Did you mean PF8?
Okay I see you are using PF3 on subsequent pages. How does it work manually?

What method do you use manually? It is the manual process that the screen-navigation-code patterns.

You need some sort of indication when the paging complete. In my experience, the screens were designed with a MESSAGE area on the last row (usually row24 as I recall) and I accumulated a full list of all messages and subsequently coded as required by the message.

It all starts with a knowledge of these factors.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

...and BTW the screen format table that I used is described here:
FAQ99-7950: Strategies for scraping in multiple screens

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Heya Skip!

PF8 is like the next or next page function while PF3 is like the back function.

The manual process of it is that you would enter an input (designated by PO in this case) then hit enter to see a list of all records for that value; if there are more records that that screen can show then it will have multiple pages/screens you can tab through using PF8. This list view gives high level information of each record which isn't very helpful for what is needed in the extract. However you can also look at each record in more detail by using the selection code (designated I in this case) which will take you into a different screen with more data fields that are helpful but the only way to get back to the complete listing of records is by using the PF3 key which unfortunately takes you back to the first page of records (so if there are multiple pages of records this is where I'm running into the issue). For this macro it can go into that more detailed screen for any of the records on the first page of the list and retrieve the necessary information for each while going back to the listing without any issue but pages 2, 3, 4, and so on have the issue.

As an example, let's say records/rows 1 & 8 on page 1 of the listing have the null value for 'S' so the macro is recognizing it needs to go into the more detailed screen of the record and retrieve that information - no issues. The macro continues through all the records/rows on page 1 then moves to page 2 where you might see rows 5 & 6 have null values for 'S' - it will go into the detailed view of record 5 and pull that data just fine but the PF3 key takes you back to page 1 of 2 in the records list where it will then continue with the logic and end up recapturing the details from record 7 again on page 1. I need it to recognize that it left off on page 2 and so when it uses that PF3/back feature it automatically uses the PF8 key the necessary number of times to get back to the page it left off on (i.e. PF8 x1 if on page 2, PF8x2 if on page 3, etc.).

I do see that Page 1 has a message on line 22 essentially stating more data is available, meaning that another page of records can be tabbed to using PF8 but I don't think that using this identifier would help much as there could be multiple pages of data for one input so I'm not sure how the macro would recognize exactly which page it had left off on. It's kind of like forgetting your place in a book and re-reading portions of it except it never gets past a certain point (sounds a little like me reading some of the textbooks in college).

RE: Help needed with syntax/logic in Attachmate macro

Do PA1, PA2 or PA3 key recover the previous page place?

Otherwise, you need to store a value the representing the Page Number and maybe even the row number that you last interrogated.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Unfortunately the PA1 key takes you back to a screen to select a session, the PA2 key would be to move to the next session and the PA3 key seems to have no effect on the screen.

PF3 is the only one I see notated as a return function but that starts you back off at the first page/entries of whatever the previous screen was.

RE: Help needed with syntax/logic in Attachmate macro

Then the Otherwise I posted might be appropriate, to be able to navigate back to the previous page and row last traversed.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
I agree that the alternative is the proper route if this is to be functional, I'm just not too skilled or versed in coding to know how to best write that. The values in the records/rows I'm looking for change between pages so I would assume I'd want to use a value there for the macro to reference in determining whether it's on the right page in addition to an If/Else statement early in the 'For r = 8 To 21' section of the code. For example, have it store maybe the value in location (8, 5, 4) and then reference prior to executing the search for 'S' being null in each row/record that way if the value matches or remains the same it can proceed onto the next record else it should PF8 and see if the value in that location on the next page matches what was stored.

RE: Help needed with syntax/logic in Attachmate macro

One of the first things you need to correctly define/design and correctly code screen navigation before you begin coding. That's got to work perfectly!

Then you can address screen-scraping design & code.

I can give you tips on specific questions, but I don't have the time to devote to examining your code and giving your feedback there. As I see things, I'll comment.

I'm hampered by the fact that I haven't coded a screen-scrape for close to a decade. I'm relying on what I can remember about 3270 terminal emulation. Your emulator might be different. And I have no access to Attachmate!Extra. Just what I can find by Googling. I'll do what I can for you.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Okay, so I tried adding in reference values PR/PG. I'm not sure if this would be the correct/best way to have a reference as it doesn't seem like the macro is seeing that the values match and is thus resorting to the Else '5 command sequence. I can see it's storing the correct value from Attachmate into Excel but not sure whether the PG = PR step is making sense or being accomplished. Ideas? I think the order of commands otherwise is all good and the screen scraping would be no issue...just need it to recognize the reference cell and actually compare it with the value in Attachmate. Apologies again as the code feature is giving me errors so it won't allow the below to be put in properly with all the indentations

Sub RecordLookUp()
Dim Sessions, System As Object, Sess0 As Object, PO As String, WB As Workbook, WS As Worksheet
Set System = CreateObject("EXTRA.System")
Set Sessions = System.Sessions
Set Sess0 = System.ActiveSession
Set WB = ActiveWorkbook
Set WS = Sheets("Look-up")

rw = 2
rw1 = 1
With Worksheets("Look-up")
Do '1
For X = rw To ActiveSheet.Rows.Count '2
PO = Left(Sheets("Look-up").Cells(X, 1).Value & "000000000", 9)
If PO = "000000000" Then Exit Sub

Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("BA")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("BA")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Sess0.Screen.MoveTo 5, 22
Sess0.Screen.SendKeys ("CD")
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
Sess0.Screen.MoveTo 3, 44
Sess0.Screen.SendKeys PO
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop


PG = Left(Sheets("Look-up").Cells(1, 2).Value & "0000", 4)
If PG = "0000" Then Exit Sub
PR = Sess0.Screen.GetString(8, 5, 4)
Sheets("Look-up").Cells(1, 2) = PR
Do '3
For r = 8 To 21 '4
dd = 1
S = Trim(Sess0.Screen.GetString(r, 33, 8))
I = Trim(Sess0.Screen.GetString(r, 2, 2))

If PG = PR Then '5

If S = "" And I = "" Then
Exit Do '6

ElseIf S = "" Then '6
Sess0.Screen.MoveTo 5, 18
Sess0.Screen.SendKeys I
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
ML = Trim(Sess0.Screen.GetString(2, 23, 27))
If ML = "INQUIRY" Then '7
CONT = Sess0.Screen.GetString(4, 28, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(8, 52, 8)
Date2 = Sess0.Screen.GetString(18, 73, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(8, 23, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 78, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

Else '7
CONT = Sess0.Screen.GetString(8, 18, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(20, 21, 8)
Date2 = Sess0.Screen.GetString(20, 31, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(9, 72, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 77, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

End If '7



Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
End If '6

Else '5
Sess0.Screen.SendKeys ("<Pf8>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

If S = "" And I = "" Then
Exit Do '8

ElseIf S = "" Then '8
Sess0.Screen.MoveTo 5, 18
Sess0.Screen.SendKeys I
Sess0.Screen.SendKeys ("<Enter>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
ML = Trim(Sess0.Screen.GetString(2, 23, 27))
If ML = "INQUIRY" Then '9
CONT = Sess0.Screen.GetString(4, 28, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(8, 52, 8)
Date2 = Sess0.Screen.GetString(18, 73, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(8, 23, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 78, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

Else '9
CONT = Sess0.Screen.GetString(8, 18, 4)
ID = Sess0.Screen.GetString(3, 44, 9)
ENTITY = Sess0.Screen.GetString(4, 9, 6)
PRDGRP = Sess0.Screen.GetString(4, 28, 6)
Date1 = Sess0.Screen.GetString(20, 21, 8)
Date2 = Sess0.Screen.GetString(20, 31, 8)
MCode = Sess0.Screen.GetString(4, 62, 2)
ISB = Sess0.Screen.GetString(9, 10, 8)
FR = Trim(Sess0.Screen.GetString(5, 51, 13))
MA = Sess0.Screen.GetString(5, 69, 1)
LA = Trim(Sess0.Screen.GetString(5, 12, 27))
SN = Sess0.Screen.GetString(9, 72, 9)
SR = Trim(Sess0.Screen.GetString(6, 14, 25))
CT = Trim(Sess0.Screen.GetString(6, 45, 25))
ST = Sess0.Screen.GetString(6, 77, 2)
ZP = Sess0.Screen.GetString(7, 6, 5)
rw1 = rw1 + 1
Sheets("Workbook").Cells(rw1, 1) = CONT
Sheets("Workbook").Cells(rw1, 2) = ID
Sheets("Workbook").Cells(rw1, 3) = ENTITY
Sheets("Workbook").Cells(rw1, 4) = PRDGRP
Sheets("Workbook").Cells(rw1, 5) = Date1
Sheets("Workbook").Cells(rw1, 6) = Date2
Sheets("Workbook").Cells(rw1, 7) = MCode
Sheets("Workbook").Cells(rw1, 8) = ISB
Sheets("Workbook").Cells(rw1, 9) = FR
Sheets("Workbook").Cells(rw1, 10) = MA
Sheets("Workbook").Cells(rw1, 11) = LA
Sheets("Workbook").Cells(rw1, 12) = SN
Sheets("Workbook").Cells(rw1, 13) = SR
Sheets("Workbook").Cells(rw1, 14) = CT
Sheets("Workbook").Cells(rw1, 15) = ST
Sheets("Workbook").Cells(rw1, 16) = ZP

End If '9

Sess0.Screen.SendKeys ("<Pf3>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop
End If '8
End If '5

Next r '4

Sess0.Screen.SendKeys ("<Pf8>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop

PR = Sess0.Screen.GetString(8, 5, 4)
Sheets("Look-up").Cells(1, 2) = PR

Loop '3

Sess0.Screen.SendKeys ("<Pf2>")
Do While Sess0.Screen.OIA.XStatus <> 0
DoEvents
Loop


Next X 'next row/group '2
Loop '1
End With





End Sub

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Also, based on your advice I tried without the screen scraping to see how it functions and only saw how the page navigation & other commands executed. It seems to be working great and recognizing the reference values then navigating to the correct screen based off that. However, when following the Else 5' command sequence it's not executing the S = "" Then command correctly and is just going through all the rows as well rather than just selecting the ones with a null value.

RE: Help needed with syntax/logic in Attachmate macro

Regarding PG/PR:

CODE

PG = Left(Sheets("Look-up").Cells(1, 2).Value & "0000", 4)
If PG = "0000" Then Exit Sub
PR = Sess0.Screen.GetString(8, 5, 4)
Sheets("Look-up").Cells(1, 2) = PR 
What is in Sheets("Look-up").Cells(1, 2).Value initially?
I can see that you're using this cell to keep track of where you are in the sheet. Maybe your code needs to grab the initial value from the sheet before you begin you loop through that screen.

Regarding the CODE feature here, I rarely use the buttons, but if you do, you must FIRST SELECT the text you want in a CODE Window and then hit the CODE button. Otherwise, as I mostly do, put the TGML tag in yourself...
[code]

[/code]
...and then paste your procedure in between.

Once you get your paging working, i'll give you a few more ideas.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Okay, made some changes which seemed to have fixed the macro recognizing and using the reference value correctly so that is no longer the issue. The PG value is null starting off in excel until stored with the PR value from Attachmate. This value will be overwritten when the macro goes through the For statement the first time and then pages to the second page of records

CODE

Sub RecordLookUp()
Dim Sessions, System As Object, Sess0 As Object, PO As String, WB As Workbook, WS As Worksheet
Set System = CreateObject("EXTRA.System")
Set Sessions = System.Sessions
Set Sess0 = System.ActiveSession
Set WB = ActiveWorkbook
Set WS = Sheets("Look-up")

        rw = 2
        rw1 = 1
        With Worksheets("Look-up")
            Do '1
             For X = rw To ActiveSheet.Rows.Count '2
                PO = Left(Sheets("Look-up").Cells(X, 1).Value & "000000000", 9)
                If PO = "000000000" Then Exit Sub
                
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("BA")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("BA")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("CD")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 3, 44
                Sess0.Screen.SendKeys PO
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                
                
                PG = Left(Sheets("Look-up").Cells(1, 2).Value & "000000000", 4)
                If PG = "000000000" Then Exit Sub
                PR = Sess0.Screen.GetString(8, 5, 4)
                Sheets("Look-up").Cells(1, 2) = PR
                
                If PG = PR Then
                
                    Do '3
                        For r = 8 To 21 '4
                        dd = 1
                        'S = Trim(Sess0.Screen.GetString(r, 33, 8))
                        'I = Trim(Sess0.Screen.GetString(r, 2, 2))
                        
                            If PG = PR Then '5
                                S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                
                                If S = "" And I = "" Then
                                Exit Do '6
                            
                                ElseIf S = "" Then '6
                                Sess0.Screen.MoveTo 5, 18
                                Sess0.Screen.SendKeys I
                                Sess0.Screen.SendKeys ("<Enter>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop

                                    'Screen Scrape coding will be placed here
                                

                                    Sess0.Screen.SendKeys ("<Pf3>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
                                End If '6
                            
                            Else '5
                            Sess0.Screen.SendKeys ("<Pf8>")
                            Do While Sess0.Screen.OIA.XStatus <> 0
                                DoEvents
                            Loop
                                'If PG = PR Then '5
        
                                S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                
                                If S = "" And I = "" Then
                                Exit Do '8
                            
                                ElseIf S = "" Then '8
                                Sess0.Screen.MoveTo 5, 18
                                Sess0.Screen.SendKeys I
                                Sess0.Screen.SendKeys ("<Enter>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                    
                                    'Screen Scrape coding will be placed here
                                    
                                    Sess0.Screen.SendKeys ("<Pf3>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
                                    End If '9
                                End If '8
                            'End If '5

                        Next r '4
    
                        Sess0.Screen.SendKeys ("<Pf8>")
                        Do While Sess0.Screen.OIA.XStatus <> 0
                            DoEvents
                        Loop
                        
                        PR = Sess0.Screen.GetString(8, 5, 4)
                        Sheets("Look-up").Cells(1, 2) = PR
                        Do While Sess0.Screen.OIA.XStatus <> 0
                            DoEvents
                        Loop
                        
                        Sess0.Screen.SendKeys ("<Pf3>")
                        Do While Sess0.Screen.OIA.XStatus <> 0
                            DoEvents
                        Loop
                        
                        Sess0.Screen.MoveTo 5, 22
                        Sess0.Screen.SendKeys ("CD")
                        Sess0.Screen.SendKeys ("<Enter>")
                        Do While Sess0.Screen.OIA.XStatus <> 0
                            DoEvents
                        Loop

                    Loop '3
                    
                Sess0.Screen.SendKeys ("<Pf2>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
               Else: End If
               
    
             Next X  'next row/group '2
            Loop '1
        
        End With





End Sub 

Now the issue is that the macro will use the reference value correctly, move to the Else '5 sequence correctly but will think that both S and I are null (when in fact they are not) which is causing it to exit the Do cycle. It shows that the r value is equal to the correct row it should read on that second page (r = 8) so I'm wondering if this needs to also be specified outside the loop.

CODE

Else '5
                            Sess0.Screen.SendKeys ("<Pf8>")
                            Do While Sess0.Screen.OIA.XStatus <> 0
                                DoEvents
                            Loop
                                'If PG = PR Then '5
        
                                S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                
                                If S = "" And I = "" Then
                                Exit Do '8 

RE: Help needed with syntax/logic in Attachmate macro

I notice that

CODE

'If PG = PR Then '5 
...is a COMMENT.

But what you can do is
1) UNCOMMENT that statement
2) Put a BREAK on that statement
3) Run to the BREAK, at which time, use the Watch Window to inspect the values stored in each if your variables.
FAQ707-4594: How to use the Watch Window as a Power Programming Tool

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Alrighty, it looks like the value is being scraped and stored into excel in the cell that PG is referencing however the comment of PG = PR is showing that the value for PG is 0000 so there is a problem. PR is storing the correct value. If I tried to run the macro a second time, while that value is already stored then the statement is true in that PG and PR are equal and showing the same value. How might I be able to have it store and return that same value without needing to restart the macro or creating another with statement?

If I continue running the macro, everything else runs fine until it gets to page 2 where it will successfully screen scrape the new value for PG from Attachmate but yet again will get to the PG = PR statement and will show PG as the old value rather than registering what it stored in excel just before.

Basically I think whatever the value is at the start of the macro in the cell PG is linked to is what that value remains throughout the macro until I restart the macro with a new value in that cell. For example, first run of the macro PG is initially 0000, it will screen scrape and store 1234 but still reads it as 0000 so the macro ends. Second run of the macro it will read the 1234 stored in that cell now so PG = 1234, it will then see that matches all records on page one and executes the commands for that page correctly, then tabs to the next page of records and stores the new value 5678 and tries to loop - during the loop it still sees PG as equal to 1234 so it's continuously looping the pulls from page 1.

RE: Help needed with syntax/logic in Attachmate macro

The problem is that you must store the PREVIOUS page number just before you PF8 to the next page.
Then PF8
Then store the CURRENT page number.

Notice that the FIRST page of any screen is a unique condition, that has no PREVIOUS value.

All this is part of screen navigation, including interrogating the message area after any codes that you send to the mainframe and performing appropriate steps. I notice that you are using a Do...Loop waiting for the mainframe to be ready to process any command. That's very astute.👍

Once you get your screen navigation code figured out, we might attempt modifying your code to simplify and streamline, if you care to do that.

Almost all of the screens that my users asked me to scrape were a one-screen process. So my paging was simpler. On very few occasions I encountered multiple screen situations and I can't remember any specifics from those encounters. I tend to think that the coding for those applications were all unique or COPY similar code and modify.

I kick myself for having lost my master workbook.

Here's a thought I just had.

In your process, you go to another screen based on values in the first screen. So what if you processed each screen separately, generating a list/table from the first screen and then processing that list/table to scrape the second screen?

Just a thought

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Alrighty Skip :) I've got it working now but had to break it apart into 2 macros - the first one to screen scrape the records pages and acquire some reference values and the second to go into the more detailed pages based off the reference values (I don't think there should ever be more than 5 pages of records per initial input ID from the first macro). For the second macro I had to introduce some PF7 syntax as I noticed sometimes it was not starting on the first page (odd but saw it occur quite a few times) so this is like the 'Last page' function and that ensures it's always at the true first page before continuing.

Macro 1:

CODE

Sub RecordLookUp1()
Dim Sessions, System As Object, Sess0 As Object, PO As String, WB As Workbook, WS As Worksheet
Set System = CreateObject("EXTRA.System")
Set Sessions = System.Sessions
Set Sess0 = System.ActiveSession
Set WB = ActiveWorkbook
Set WS = Sheets("Look-up")

        rw = 2
        rw1 = 1
        rw2 = 1
        With Worksheets("Look-up")
            Do
             For X = rw To ActiveSheet.Rows.Count
                PO = Left(Sheets("Look-up").Cells(X, 1).Value & "000000000", 9)
                If PO = "000000000" Then Exit Sub
                
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("BA")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("BA")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 5, 22
                Sess0.Screen.SendKeys ("CD")
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                Sess0.Screen.MoveTo 3, 44
                Sess0.Screen.SendKeys PO
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                
                    
                SB = Trim(Sess0.Screen.GetString(6, 2, 3))
                If SB = "SUB" Then
                
                    Do
                        For r = 8 To 21
                        dd = 1
                            S = Trim(Sess0.Screen.GetString(r, 33, 8))
                            I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                    
                                If S = "" And I = "" Then
                                Exit Do
                                
                                Else
                                    CONT = Sess0.Screen.GetString(r, 5, 4)
                                    ID = Sess0.Screen.GetString(3, 44, 9)
                                    ENTITY = Sess0.Screen.GetString(r, 10, 6)
                                    PRDGRP = Sess0.Screen.GetString(r, 17, 6)
                                    Date1 = Sess0.Screen.GetString(r, 24, 8)
                                    Date2 = Sess0.Screen.GetString(r, 33, 8)
                                    MCode = Sess0.Screen.GetString(r, 42, 2)
                                    ISB = Sess0.Screen.GetString(r, 45, 8)
                                    FR = Trim(Sess0.Screen.GetString(r, 54, 12))
                                    rw1 = rw1 + 1
                                    Sheets("Workbook").Cells(rw1, 1) = rw2
                                    Sheets("Workbook").Cells(rw1, 2) = CONT
                                    Sheets("Workbook").Cells(rw1, 3) = ID
                                    Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                    Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                    Sheets("Workbook").Cells(rw1, 6) = Date1
                                    Sheets("Workbook").Cells(rw1, 7) = Date2
                                    Sheets("Workbook").Cells(rw1, 8) = MCode
                                    Sheets("Workbook").Cells(rw1, 9) = ISB
                                    Sheets("Workbook").Cells(rw1, 10) = FR
                                        
                                End If
                                    
                            Next r
        
                            Sess0.Screen.SendKeys ("<Pf8>")
                            Do While Sess0.Screen.OIA.XStatus <> 0
                                DoEvents
                            Loop
                            
                            rw2 = rw2 + 1
                            
                    Loop
                        
                    Sess0.Screen.SendKeys ("<Pf2>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                    
                Else
                End If
                
                rw2 = 1
                
             Next X  'next row/group '
            
            Loop
        
        End With





End Sub 

Macro 2:

CODE

Sub RecordLookUp2()
Dim Sessions, System As Object, Sess0 As Object, PO As String, WB As Workbook, WS As Worksheet
Set System = CreateObject("EXTRA.System")
Set Sessions = System.Sessions
Set Sess0 = System.ActiveSession
Set WB = ActiveWorkbook
Set WS = Sheets("Workbook")

        rw = 2
        rw1 = 1
        rw2 = 2
        With Worksheets("Workbook")
            Do '1
            
                For X = rw To ActiveSheet.Rows.Count '2
                    PO = Left(Sheets("Workbook").Cells(X, 2).Value & "000000000", 9)
                    If PO = "000000000" Then Exit Sub
                    
                    Sess0.Screen.MoveTo 5, 22
                    Sess0.Screen.SendKeys ("BA")
                    Sess0.Screen.SendKeys ("<Enter>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                    Sess0.Screen.MoveTo 5, 22
                    Sess0.Screen.SendKeys ("BA")
                    Sess0.Screen.SendKeys ("<Enter>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                    Sess0.Screen.MoveTo 3, 44
                    Sess0.Screen.SendKeys PO
                    Sess0.Screen.SendKeys ("<Enter>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                    Sess0.Screen.MoveTo 5, 22
                    Sess0.Screen.SendKeys ("CD")
                    Sess0.Screen.SendKeys ("<Enter>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    
                    SB = Trim(Sess0.Screen.GetString(6, 2, 3))
                    If SB = "SUB" Then '3
                        
                        PG = Left(Sheets("Workbook").Cells(X, 1).Value & "00", 2)
                    
                        Do '4
                                For r = 8 To 21 '5
                                dd = 1
                    
                                    If PG = "01" Then '6
                                        rw2 = rw2 + 1
                                        
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                        I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                        
                                        If S = "" And I = "" Then '7
                                        Exit Do
                                    
                                        Else '7
                                        Sess0.Screen.MoveTo 5, 18
                                        Sess0.Screen.SendKeys I
                                        Sess0.Screen.SendKeys ("<Enter>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
        
                                            'Screen Scrape coding will be placed here
                                            ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                            If ML = "MEMBER LIFE PROFILE INQUIRY" Then '8
                                                CONT = Sess0.Screen.GetString(4, 28, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(8, 23, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CR = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 78, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                            Else '8
                                                CONT = Sess0.Screen.GetString(8, 18, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(9, 72, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CT = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 77, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                            
                                            End If '8
                                        
        
                                            Sess0.Screen.SendKeys ("<Pf3>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                            
                                        End If '7
                                    'End If '8
                                    
                                    ElseIf PG = "02" Then '6
                                        
                                        rw2 = rw2 + 1
                                    
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                
                                            S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                            I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                            
                                            If S = "" And I = "" Then '7
                                            Exit Do
                                        
                                            Else '7
                                            Sess0.Screen.MoveTo 5, 18
                                            Sess0.Screen.SendKeys I
                                            Sess0.Screen.SendKeys ("<Enter>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                                
                                            'Screen Scrape coding will be placed here
                                            ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                            If ML = "MEMBER LIFE PROFILE INQUIRY" Then '8
                                                CONT = Sess0.Screen.GetString(4, 28, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(8, 23, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CR = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 78, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                            Else '8
                                                CONT = Sess0.Screen.GetString(8, 18, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(9, 72, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CT = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 77, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                                End If '8
                                            Sess0.Screen.SendKeys ("<Pf3>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                            
                                            CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                                If CD = "CD" Then
                                                    Sess0.Screen.MoveTo 5, 22
                                                    Sess0.Screen.SendKeys ("CD")
                                                    Sess0.Screen.SendKeys ("<Enter>")
                                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                                        DoEvents
                                                    Loop
                                                
                                                Else
                                                End If
                                            End If '7
                                    
                                    ElseIf PG = "03" Then '6
                                    
                                        rw2 = rw2 + 1
                                    
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                
                                            S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                            I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                            
                                            If S = "" And I = "" Then '7
                                            Exit Do
                                        
                                            Else '7
                                            Sess0.Screen.MoveTo 5, 18
                                            Sess0.Screen.SendKeys I
                                            Sess0.Screen.SendKeys ("<Enter>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                                
                                            'Screen Scrape coding will be placed here
                                            ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                            If ML = "MEMBER LIFE PROFILE INQUIRY" Then '8
                                                CONT = Sess0.Screen.GetString(4, 28, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(8, 23, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CR = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 78, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                            Else '8
                                                CONT = Sess0.Screen.GetString(8, 18, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(9, 72, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CT = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 77, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                                End If '8
                                            Sess0.Screen.SendKeys ("<Pf3>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                            
                                            CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                                If CD = "CD" Then
                                                    Sess0.Screen.MoveTo 5, 22
                                                    Sess0.Screen.SendKeys ("CD")
                                                    Sess0.Screen.SendKeys ("<Enter>")
                                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                                        DoEvents
                                                    Loop
                                                
                                                Else
                                                End If
                                            End If '7
                                    
                                    ElseIf PG = "04" Then '6
                                        
                                        rw2 = rw2 + 1
                                    
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                
                                            S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                            I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                            
                                            If S = "" And I = "" Then '7
                                            Exit Do
                                        
                                            Else '7
                                            Sess0.Screen.MoveTo 5, 18
                                            Sess0.Screen.SendKeys I
                                            Sess0.Screen.SendKeys ("<Enter>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                                
                                            'Screen Scrape coding will be placed here
                                            ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                            If ML = "MEMBER LIFE PROFILE INQUIRY" Then '8
                                                CONT = Sess0.Screen.GetString(4, 28, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(8, 23, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CR = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 78, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                            Else '8
                                                CONT = Sess0.Screen.GetString(8, 18, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(9, 72, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CT = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 77, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                                End If '8
                                            Sess0.Screen.SendKeys ("<Pf3>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                            
                                            CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                                If CD = "CD" Then
                                                    Sess0.Screen.MoveTo 5, 22
                                                    Sess0.Screen.SendKeys ("CD")
                                                    Sess0.Screen.SendKeys ("<Enter>")
                                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                                        DoEvents
                                                    Loop
                                                
                                                Else
                                                End If
                                            End If '7
                                    
                                    ElseIf PG = "05" Then '6
                                    
                                        rw2 = rw2 + 1
                                    
                                        
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf8>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                            S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                            I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                            
                                            If S = "" And I = "" Then '7
                                            Exit Do
                                        
                                            Else '7
                                            Sess0.Screen.MoveTo 5, 18
                                            Sess0.Screen.SendKeys I
                                            Sess0.Screen.SendKeys ("<Enter>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                                
                                            'Screen Scrape coding will be placed here
                                            ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                            If ML = "MEMBER LIFE PROFILE INQUIRY" Then '8
                                                CONT = Sess0.Screen.GetString(4, 28, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(8, 23, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CR = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 78, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                            Else '8
                                                CONT = Sess0.Screen.GetString(8, 18, 4)
                                                ENTITY = Sess0.Screen.GetString(4, 9, 6)
                                                PRDGRP = Sess0.Screen.GetString(4, 28, 6)
                                                MA = Sess0.Screen.GetString(5, 69, 1)
                                                LA = Trim(Sess0.Screen.GetString(5, 12, 27))
                                                SN = Sess0.Screen.GetString(9, 72, 9)
                                                SR = Trim(Sess0.Screen.GetString(6, 14, 25))
                                                CT = Trim(Sess0.Screen.GetString(6, 45, 25))
                                                ST = Sess0.Screen.GetString(6, 77, 2)
                                                ZP = Sess0.Screen.GetString(7, 6, 5)
                                                rw1 = rw1 + 1
                                                Sheets("Workbook").Cells(rw1, 2) = CONT
                                                Sheets("Workbook").Cells(rw1, 4) = ENTITY
                                                Sheets("Workbook").Cells(rw1, 5) = PRDGRP
                                                Sheets("Workbook").Cells(rw1, 11) = MA
                                                Sheets("Workbook").Cells(rw1, 12) = LA
                                                Sheets("Workbook").Cells(rw1, 13) = SN
                                                Sheets("Workbook").Cells(rw1, 14) = SR
                                                Sheets("Workbook").Cells(rw1, 15) = CT
                                                Sheets("Workbook").Cells(rw1, 16) = ST
                                                Sheets("Workbook").Cells(rw1, 17) = ZP
                                                
                                                End If '8
                                            Sess0.Screen.SendKeys ("<Pf3>")
                                            Do While Sess0.Screen.OIA.XStatus <> 0
                                                DoEvents
                                            Loop
                                            
                                            CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                                If CD = "CD" Then
                                                    Sess0.Screen.MoveTo 5, 22
                                                    Sess0.Screen.SendKeys ("CD")
                                                    Sess0.Screen.SendKeys ("<Enter>")
                                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                                        DoEvents
                                                    Loop
                                                
                                                Else
                                                End If
                                            End If '7
                                    Else: End If '6
        
                                Next r '5
                                                            
                                Sess0.Screen.SendKeys ("<Pf8>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                
                                PR = Sess0.Screen.GetString(8, 5, 4)
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                
                                Sess0.Screen.SendKeys ("<Pf3>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                
                                CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                    If CD = "CD" Then
                                        Sess0.Screen.MoveTo 5, 22
                                        Sess0.Screen.SendKeys ("CD")
                                        Sess0.Screen.SendKeys ("<Enter>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                        
                                        Sess0.Screen.SendKeys ("<Pf7>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                    
                                    Else: End If
                                
                        Loop '4
                            
                    Else '3
                     
                        Sess0.Screen.SendKeys ("<Pf2>")
                        Do While Sess0.Screen.OIA.XStatus <> 0
                            DoEvents
                        Loop
                        
                    End If '3
                    
                                    
             Next X  'next row/group '2
            
            Loop '1
        
        End With

End Sub 

RE: Help needed with syntax/logic in Attachmate macro

So, bottom line, what's the result?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
It’s running perfectly for one input. I just recently tested it with multiple inputs (i.e. the PO values) and realized I’m missing some syntax to execute after the initial value so I’ll need to review it a bit more in break mode to see exactly where the disconnect(s) occur but should be relatively simple. Once I get that completed I’ll post it in case you can give me some pointers on cleaning it up :)

RE: Help needed with syntax/logic in Attachmate macro

Great! I'll be happy to review your progress.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Alrighty, I've been making some edits to both macros (I went ahead and just updated it within the last post since these are fairly long macros and a bit bothersome to repost with minor changes). I realized the PG1-PG5 values I had being referenced from my excel cells of the first screen scrape would not be consistently 14 rows apart every time when screen scraping the records of multiple inputs and that I would need more than 5 for multiple inputs I'm trying to collect data for so I've had to resort to indexing the page the data is pulled in from via the first macro (listed below). This isn't as great because it's not checking any data on the pages before proceeding into each record but I'll have to brainstorm on how to add a validation in once I get it all working.

CODE

Sheets("Workbook").Cells(rw1, 1) = rw2 

While amending the second macro I'm now getting a compile error of 'Next without For' on the last Next syntax. Any ideas what I may not have closed out and what I'm missing? I've tried another End If above this but that will give another compile error of 'End If without block If' and any I try to remove give errors up to those points in the coding.

CODE

Next X  'next row/group '2 

RE: Help needed with syntax/logic in Attachmate macro

I think that now is the time to look at streamlining your code.

I'm sure you've noticed that you have lots of repeated code. What sticks out the most is where you repeat PF8 one after the other. This is part of your screen navigation. These can probably be in a loop. Since I don't have access to your emulator, I don't know the logic of x repeats of PF8 or any other repeated command.

Then you have repeated use of scrape-and-store code. Those could be put in a separate procedure using module-level declarations for each variable used.

This will greatly simplify and minimize your code.

I'd like to see a pic of each screen and your "workbook" worksheet along with any explanation that would help me understand the screen navigation.

I didn't have the time today to scour your code to find the missing For.

Totally don't know what you mean by, "I realized the PG1-PG5 values..." and so on.

You have to reveal a LOT more about your screen navigation before I can be of specific help.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
I've attached a word document with a high level walk through/key strokes on each screen (redacted due to sensitive information and page 5 is not included as it was too much to redact and is really only needed for screen scraping).

I've set the variables and inserted a loop for the PF8 keys which I think will work but I'll need to wait to test it as I am still encountering the For error and I cannot figure out where I've messed up the syntax.

RE: Help needed with syntax/logic in Attachmate macro

Some comments on your code.

You have declared a workbook object and a worksheet object. But you fail to use them.

In this case, where you only reference ONE workbook and ONE worksheet, declaring object variables is really unnecessary. But lets go ahead and use them. I'll make a few minor changes to illustrate what I like to do. And I'm leaving out some code simply to emphasize these things.

CODE

Sub RecordLookUp2()
   Dim wbMain As Workbook, wsOutData As Worksheet
   Dim iScrn as Integer

   Set wbMain = ActiveWorkbook
   Set wsOutData = wbMain.WorkSheets("Workbook")

   rw = 2
   rw1 = 1
   rw2 = 2

   With wsOutData
      Do '1
            
         For X = rw To .Rows.Count '2
            PO = Left(.Cells(X, 2).Value & "000000000", 9)
            If PO = "000000000" Then Exit Sub
'Screens 1 & 2
            For iScrn = 1 To 2  
               Sess0.Screen.PutString "BA", 5, 22
               Sess0.Screen.SendKeys ("<Enter>")
               Do While Sess0.Screen.OIA.XStatus <> 0
                  DoEvents
               Loop
             Next
'Screen 3
             Sess0.Screen.PutString PO, 3, 44
             Sess0.Screen.PutString "CD", 5, 22
             Sess0.Screen.SendKeys ("<Enter>")   
             Do While Sess0.Screen.OIA.XStatus <> 0
                DoEvents
             Loop
'Screen 4
             SB = Trim(Sess0.Screen.GetString(6, 2, 3))
             If SB = "SUB" Then '3
'.....
             Else '3
                Sess0.Screen.SendKeys ("<Pf2>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                   DoEvents
                Loop
             End If '3
         Next X '2
      Loop '1
   End With
End Sub 

Now here, starting sheet 4 I do not see "SUB" @ 6,2???

NOTICE:
1) when you use With..End With, the Reference, in this case the worksheet object, wsOutData, is implied by each DOT Reference (.Rows.Count & .Cells(X, 2).Value) to wsOutData within the block.
2) I'm using PutString to place data on the screen. Neither is a command sending data to the mainframe UNTIL you hit SendKeys "ENTER"
3) I've put in ALL of the Block Format at the appropriate indent. I make a practice of doing this each time I add a block so I greatly minimize having a For without Next or If without End If.

CODE

Sub...End Sub
With...End With
Do...Loop
If...Else...End If
For...Next 
BTW, the Do...Loop followed by For...Next is unnecessary.
To exit a For...Next before the limit use an If test to Exit For.

CODE

For i = A To B
'....
   If expression Then Exit For
Next 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

(OP)
Skip, you saint! I got it working and tested it out with about 60+ initial inputs that scraped over 900 lines of data into excel - couldn't find any issues.

I did have to make quite a few amendments to the code though. At the start of each new input search, it's running a loop of PF7 10 times in the Attachmate session because for whatever reason if I used PF7 only once or twice sometimes it would go back to the list of records for the previous input while other times it would somewhat get lost in the paging of the current record. This loop ensures it always resets to the first page of that input's records (barring any records with 10+ pages it might previously pull up). There was no key(s) to reset the screen and remove that previous search history without having to restart the session itself so I figured this was the quicker option although I would have liked it to work off of a loop based on the PG2 variable to page back the appropriate number of times when necessary. I did in fact try this and ran into the same issue as stated earlier where it goes into the wrong record or gets lost in the paging of the correct record.

"SUB" at 6,2 is correct, I edited this area in the screenshot due to some sensitive information.

I removed the second Do...Loop to give that a shot but it backs out to the first screen every time and then got a bit lost after that so I figured I'd better not fix something that's not broke for me right now. tongue

Embedding the screen scrape macro in it's own sub and calling that into the main sub was a genius idea though so thanks so much for that! If you see any other places where I might be able to clean up feel free to let me know!

CODE

Option Explicit
    Dim Sessions, System As Object, Sess0 As Object
    Dim wbMain As Workbook, wsOutData As Worksheet
'Main Sub
    Dim X As Long, PG1 As Long
    Dim iScrn As Integer, rw As Integer, rw1 As Integer, SB As String, r As Integer, dd As Integer, PG2 As Integer, p As Integer, p2 As Integer
    Dim PO As String, PG As String, ID As String, S As String, I As String, ML As String, CD As String
'Scrub Subs
    Dim CONT As String, ENTITY As String, PRDGRP As String, MA As String, LA As String, SN As String, SR As String, CT As String, ST As String, ZP As String

Sub RecordLookUp2()
    Set System = CreateObject("EXTRA.System")
    Set Sessions = System.Sessions
    Set Sess0 = System.ActiveSession
    Set wbMain = ActiveWorkbook
    Set wsOutData = wbMain.Worksheets("Workbook")
    
    rw = 2
    rw1 = 1
        
    With wsOutData
        Do '1.1
            For X = rw To .Rows.Count '1.2
                PO = Left(.Cells(rw, 3).Value & "000000000", 9)
                If PO = "000000000" Then Exit Sub

'Screens 1 & 2
                For iScrn = 1 To 2 '2.1
                    Sess0.Screen.PutString "BA", 5, 22
                    Sess0.Screen.SendKeys ("<Enter>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                Next

'Screen 3
                Sess0.Screen.PutString PO, 3, 44
                Sess0.Screen.PutString "CD", 5, 22
                Sess0.Screen.SendKeys ("<Enter>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop

'Screen 4 Opt 1
                PG = Left("0" & .Cells(rw, 1).Value & "0", 2)
                ID = Sess0.Screen.GetString(3, 44, 9)
                p = 1
                
                For p = 1 To 10 '4.1
                    Sess0.Screen.SendKeys ("<Pf7>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                Next p '4.1
                
                SB = Trim(Sess0.Screen.GetString(6, 2, 3))
                If SB = "SUB" Then '4.2

                    Do '4.3
                        For r = 8 To 21 '4.4
                            dd = 1
                            PO = Left(.Cells(rw, 3).Value & "000000000", 9)
                            PG = Left("0" & .Cells(rw, 1).Value & "0", 2)
                            ID = Sess0.Screen.GetString(3, 44, 9)
                                    
                            If PG = "01" And PO = ID Then '4.5
                                S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                I = Trim(Sess0.Screen.GetString(r, 2, 2))
                                        
                                If S = "" And I = "" Then '4.6
                                Exit Do

'Screen 5
                                Else '4.6
                                    Sess0.Screen.PutString I, 5, 18
                                    Sess0.Screen.SendKeys ("<Enter>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
    
                                    ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                    If ML = "INQUIRY" Then '4.7
                                        RunScrape1
                                                
                                    Else '4.7
                                        RunScrape2

                                    End If '4.7

                                Sess0.Screen.SendKeys ("<Pf3>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                            
                                rw = rw + 1
                                
                                End If '4.6
                                    
'Screen 4 Opt2
                            ElseIf PG > "01" And PO = ID Then '4.5
                                PG1 = .Cells(rw, 1).Value
                                PG2 = PG1 - 1
                                p = 1

                                For p = 1 To PG2 '4.6
                                    Sess0.Screen.SendKeys ("<Pf7>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
                                Next p '4.6
                                        
                                For p2 = 1 To PG2 '4.6
                                    Sess0.Screen.SendKeys ("<Pf8>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
                                Next p2 '4.6
                
                                S = Trim(Sess0.Screen.GetString(r, 33, 8))
                                I = Trim(Sess0.Screen.GetString(r, 2, 2))
'Screen 5
                                If S = "" And I = "" Then '4.6
                                Exit Do
                                        
                                Else '4.6
                                    Sess0.Screen.PutString I, 5, 18
                                    Sess0.Screen.SendKeys ("<Enter>")
                                    Do While Sess0.Screen.OIA.XStatus <> 0
                                        DoEvents
                                    Loop
                                                
                                    ML = Trim(Sess0.Screen.GetString(2, 23, 27))
                                    If ML = "INQUIRY" Then '4.7
                                        RunScrape1

                                    Else '4.7
                                        RunScrape2

                                    End If '4.7

                                Sess0.Screen.SendKeys ("<Pf3>")
                                Do While Sess0.Screen.OIA.XStatus <> 0
                                    DoEvents
                                Loop
                                            
                                CD = Trim(Sess0.Screen.GetString(8, 56, 2))
                                    If CD = "CD" Then '4.7
                                        Sess0.Screen.PutString "CD", 5, 22
                                        Sess0.Screen.SendKeys ("<Enter>")
                                        Do While Sess0.Screen.OIA.XStatus <> 0
                                            DoEvents
                                        Loop
                                                
                                    Else '4.7
                                    End If
                                                
                                rw = rw + 1
                                                                                               
                                End If '4.6
                                    
                            Else '4.5
                            Exit Do
                                    
                            End If '4.5
        
                                
                        Next r '4.4
                                                               
                    Loop '4.3
                            
                Else '4.2
                    Sess0.Screen.SendKeys ("<Pf2>")
                    Do While Sess0.Screen.OIA.XStatus <> 0
                        DoEvents
                    Loop
                End If '4.2
                         
                Sess0.Screen.SendKeys ("<Pf2>")
                Do While Sess0.Screen.OIA.XStatus <> 0
                    DoEvents
                Loop
                    
            Next X  'next row/group '1.2
        Loop '1.1
    End With
End Sub

Public Sub RunScrape1()
    Set System = CreateObject("EXTRA.System")
    Set Sessions = System.Sessions
    Set Sess0 = System.ActiveSession
    Set wbMain = ActiveWorkbook
    Set wsOutData = wbMain.Worksheets("Workbook")
        
    With wsOutData
        CONT = Sess0.Screen.GetString(4, 28, 4)
        ENTITY = Sess0.Screen.GetString(4, 9, 6)
        PRDGRP = Sess0.Screen.GetString(4, 28, 6)
        MA = Sess0.Screen.GetString(5, 69, 1)
        LA = Trim(Sess0.Screen.GetString(5, 12, 27))
        SN = Sess0.Screen.GetString(8, 23, 9)
        SR = Trim(Sess0.Screen.GetString(6, 14, 25))
        CT = Trim(Sess0.Screen.GetString(6, 45, 25))
        ST = Sess0.Screen.GetString(6, 78, 2)
        ZP = Sess0.Screen.GetString(7, 6, 5)
        rw1 = rw1 + 1
        .Cells(rw1, 2) = CONT
        .Cells(rw1, 4) = ENTITY
        .Cells(rw1, 5) = PRDGRP
        .Cells(rw1, 11) = MA
        .Cells(rw1, 12) = LA
        .Cells(rw1, 13) = SN
        .Cells(rw1, 14) = SR
        .Cells(rw1, 15) = CT
        .Cells(rw1, 16) = ST
        .Cells(rw1, 17) = ZP
    End With
End Sub

Public Sub RunScrape2()
    Set System = CreateObject("EXTRA.System")
    Set Sessions = System.Sessions
    Set Sess0 = System.ActiveSession
    Set wbMain = ActiveWorkbook
    Set wsOutData = wbMain.Worksheets("Workbook")
        
    With wsOutData
        CONT = Sess0.Screen.GetString(8, 18, 4)
        ENTITY = Sess0.Screen.GetString(4, 9, 6)
        PRDGRP = Sess0.Screen.GetString(4, 28, 6)
        MA = Sess0.Screen.GetString(5, 69, 1)
        LA = Trim(Sess0.Screen.GetString(5, 12, 27))
        SN = Sess0.Screen.GetString(9, 72, 9)
        SR = Trim(Sess0.Screen.GetString(6, 14, 25))
        CT = Trim(Sess0.Screen.GetString(6, 45, 25))
        ST = Sess0.Screen.GetString(6, 77, 2)
        ZP = Sess0.Screen.GetString(7, 6, 5)
        rw1 = rw1 + 1
        .Cells(rw1, 2) = CONT
        .Cells(rw1, 4) = ENTITY
        .Cells(rw1, 5) = PRDGRP
        .Cells(rw1, 11) = MA
        .Cells(rw1, 12) = LA
        .Cells(rw1, 13) = SN
        .Cells(rw1, 14) = SR
        .Cells(rw1, 15) = CT
        .Cells(rw1, 16) = ST
        .Cells(rw1, 17) = ZP
    End With
End Sub 

RE: Help needed with syntax/logic in Attachmate macro

Good for you! Like a wild horse, you have taken the bit in your teeth and run like the wind!

Here's another tip, like polishing an apple and tuning the string to a perfect harmonic. It could be a constant labor of love.

Look for places where an object variable can save you multiple dot levels of reference. Here oScreen can replace Sess0.Screen.

CODE

Option Explicit
    Dim Sessions, System As Object, Sess0 As Object, oScreen As Object
'...
Sub RecordLookUp2()
    Set oScreen = Sess0.Screen
'...
    With oScreen
       .PutString "BA", 5, 22
'...
    End With
'...
End Sub

Public Sub RunScrape2()
'These are Module level variables that were Set in the calling procedure.
    Set System = CreateObject("EXTRA.System")
    Set Sessions = System.Sessions
    Set Sess0 = System.ActiveSession
    Set wbMain = ActiveWorkbook
    Set wsOutData = wbMain.Worksheets("Workbook")
        
    With wsOutData
        With oScreen
           CONT = .GetString(8, 18, 4)
           ENTITY = .GetString(4, 9, 6)
           PRDGRP = .GetString(4, 28, 6)
           MA = .GetString(5, 69, 1)
           LA = Trim(.GetString(5, 12, 27))
           SN = .GetString(9, 72, 9)
           SR = Trim(.GetString(6, 14, 25))
           CT = Trim(.GetString(6, 45, 25))
           ST = .GetString(6, 77, 2)
           ZP = .GetString(7, 6, 5)
        End With
        rw1 = rw1 + 1
        .Cells(rw1, 2) = CONT
        .Cells(rw1, 4) = ENTITY
        .Cells(rw1, 5) = PRDGRP
        .Cells(rw1, 11) = MA
        .Cells(rw1, 12) = LA
        .Cells(rw1, 13) = SN
        .Cells(rw1, 14) = SR
        .Cells(rw1, 15) = CT
        .Cells(rw1, 16) = ST
        .Cells(rw1, 17) = ZP
    End With
End Sub 

These two FAQs ought to be helpful.
FAQ707-4105: How Can I Make My Code Run Faster?
FAQ707-4594: How to use the Watch Window as a Power Programming Tool

FYI: Most of my posting regarding the use of VBA code is in forum707: VBA Visual Basic for Applications (Microsoft).

It is just the honest truth that I found VBA and the Editor, were so much more superior to Extra VB and that Editor that I never wasted my time trying to learn the lesser of the two when I was ALWAYS using an Excel list to drive any process.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

RE: Help needed with syntax/logic in Attachmate macro

Here's a possible next step in the evolution of your procedure and your programming progress.

The stated goal is to remove as much DATA embedded in your code as possible and rather embed the data in one or more tables. Managing dozens of sets of values in a table used by one set of code is much easier than managing dozens of sets of code.

So the specific goal is to place the screen data in a table with headings like:
Screen
Field
Start Row
End Row
Column
Length
Type (Num or Chr)
 

So in this case, making some assumptions...
Screen  Field  Start  End  COL  Length  Type
Screen4 SEL    8      21   2    2       NUM
Screen4 CONT   8      21   6    4       CHR
Screen4 ENTITY 8      21   11   6       CHR
Screen4 PRDGRP 8      21   18   6       CHR
Screen4 DATE1  8      21   25   8       CHR
Screen4 DATE2  8      21   34   8       CHR
Screen4 MB     8      21   43   2       CHR
Screen4 ISB    8      21   46   8       CHR
Screen4 FA     8      21   55   3       CHR
Screen4 MGS    22     22   2    79      CHR
 

Now I did this for every every single field on the screen and I had dozens of screens so defined in my table. And my corresponding worksheet had the same headings in the same order. So I would read down the table for the given screen and do a GetString according to the values in the table then a PutString in my worksheet.

On screens where there were a range of rows, all the single-row fields would be repeated for each row. The Type field was to either make the Excel column GENERAL for NUM or TEXT for CHR. If the leading ZERO is necessary, then TEXT/CHR.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close