Your response was EXCELLENT! Thank you so much for taking your time to help me! I had to add an Exit For at the end to make it stop but it's totally working now! I am so excited! THANK YOU!
Here's the full code:
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
ThisWorkbook.Activate
J = Cells(Rows.Count, "A").End(xlUp).Row
'For I = 2 To J
I = 2
FromPeriod = Cells(I, "A").Value
ToPeriod = Cells(I, "B").Value
FiscalYear = Cells(I, "C").Value
ProfitCenterGroup = Cells(I, "D").Value
session.findById("wnd[0]").resizeWorkingPane 128, 41, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzco30"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PERIOF").Text = FromPeriod
session.findById("wnd[0]/usr/ctxtP_PERIOT").Text = ToPeriod
session.findById("wnd[0]/usr/ctxtP_PROFIT").Text = ProfitCenterGroup
session.findById("wnd[0]/usr/ctxtS_VALUES-LOW").Text = ""
session.findById("wnd[0]/usr/chkCB_REIM").SetFocus
session.findById("wnd[0]/usr/chkCB_REIM").Selected = True
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").selectItem " 1", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem " 1", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").doubleClickItem " 1", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").expandNode " 3"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = " 3"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").expandNode " 4"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = " 3"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").expandNode " 5"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = " 3"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").selectItem " 5", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem " 5", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").doubleClickItem " 5", "C 2"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").expandNode " 1"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = " 1"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").selectItem " 2", "C 1"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem " 2", "C 1"
Set Tree = session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell/shellcont[1]/shell")
'Column 2 is Profit Center, but the index starts with 0
'To get the correct columnt 2 minus 1
colName = Tree.GetColumnNames.Item("1")
Set col = Tree.GetColumnCol(colName)
'Get top node
topNode = CStr(Tree.topNode): Key = topNode
'-Counter to get correct index of node------------------------------
cnt = -7 + Tree.GetNodeChildrenCountByPath(topNode)
'-Scan nodes on the first level-------------------------------------
For I = 1 To Tree.GetNodeChildrenCountByPath(topNode)
cnt = cnt + 1
nodeName = Tree.GetNodeTextByKey(Key)
nodePath = Tree.GetNodePathByKey(Key)
If nodeName = "Pre close Tie Out Hi" Then
'-Scan nodes on the second level--------------------------------
For J = 1 To Tree.GetNodeChildrenCount(Key)
cnt = cnt + 1
subNode = Tree.GetNodeKeyByPath(nodePath & "/" & CStr(J))
subNodeName = Tree.GetNodeTextByKey(subNode)
Select Case subNodeName
Case "Communications, Medi"
ProfitCenter = col.Item(cnt)
Cells(I, "F").Value = ProfitCenter
Case "Financial Services"
ProfitCenter = col.Item(cnt)
Cells(1 + I, "F").Value = ProfitCenter
Case "Health & Public Serv"
ProfitCenter = col.Item(cnt)
Cells(2 + I, "F").Value = ProfitCenter
Case "Products"
ProfitCenter = col.Item(cnt)
Cells(3 + I, "F").Value = ProfitCenter
Case "Resources"
ProfitCenter = col.Item(cnt)
Cells(4 + I, "F").Value = ProfitCenter
Case "Other"
ProfitCenter = col.Item(cnt)
Cells(5 + I, "F").Value = ProfitCenter
End Select
Next
End If
If I < Tree.GetNodeChildrenCountByPath(topNode) Then
Exit For
Key = Tree.GetNextNodeKey(Key)
End If
Next
MsgBox "Done"
End Sub