Giordano Bruno
Well-known Member
- Joined
- Jan 7, 2007
- Messages
- 1,350
I have the following code which lives in a UserForm in Workbook 1 and is called from button in Workbook2. I have redirected the button that launches the UserForm to code on Workbook 3. When I try to run it now, it fails on the line indicated and highlights the word tvwChild. The message is Compile Error: Variable not defined. I have searched the code in the working version and there is no other reference to this word.
If anyone can offer an explanation or workaround, I would be most grateful
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> LoadTreeView()
<SPAN style="color:#00007F">Dim</SPAN> rNodeNames <SPAN style="color:#00007F">As</SPAN> Range, rNodeName <SPAN style="color:#00007F">As</SPAN> Range, nodX <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> bNodeNames <SPAN style="color:#00007F">As</SPAN> Range <SPAN style="color:#007F00">'extra</SPAN>
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
.Clear
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
cS1 = Sheet1.Range("Codes").Column <SPAN style="color:#007F00">'sets Codes column number</SPAN>
rR1 = Sheet1.Range("Codes").Row + 1 <SPAN style="color:#007F00">'sets top row</SPAN>
<SPAN style="color:#00007F">Set</SPAN> bNodeNames = Sheet1.Cells(rR1, cS1) <SPAN style="color:#007F00">'first node name</SPAN>
<SPAN style="color:#00007F">Set</SPAN> rNodeNames = Range("CodeList") <SPAN style="color:#007F00">'subsequent node names</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rNodeName <SPAN style="color:#00007F">In</SPAN> bNodeNames
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
<SPAN style="color:#00007F">Set</SPAN> nodX = .Add(, , "R", rNodeName.Offset(, -cS1 + 1).Value) <SPAN style="color:#007F00">'adds nodes to the tree</SPAN>
nodX.Expanded = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Next</SPAN> rNodeName
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rNodeName <SPAN style="color:#00007F">In</SPAN> rNodeNames
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
<SPAN style="color:#007F00">'Fails at this line</SPAN>
<SPAN style="color:#00007F">Set</SPAN> nodX = .Add(Left(rNodeName.Value, -1 + InStrRev(rNodeName.Value, "_")), _
tvwChild, rNodeName, rNodeName.Offset(, -cS1 + 1).Value) <SPAN style="color:#007F00">' adds terminals</SPAN>
nodX.Expanded = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Next</SPAN> rNodeName
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
If anyone can offer an explanation or workaround, I would be most grateful
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> LoadTreeView()
<SPAN style="color:#00007F">Dim</SPAN> rNodeNames <SPAN style="color:#00007F">As</SPAN> Range, rNodeName <SPAN style="color:#00007F">As</SPAN> Range, nodX <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> bNodeNames <SPAN style="color:#00007F">As</SPAN> Range <SPAN style="color:#007F00">'extra</SPAN>
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
.Clear
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
cS1 = Sheet1.Range("Codes").Column <SPAN style="color:#007F00">'sets Codes column number</SPAN>
rR1 = Sheet1.Range("Codes").Row + 1 <SPAN style="color:#007F00">'sets top row</SPAN>
<SPAN style="color:#00007F">Set</SPAN> bNodeNames = Sheet1.Cells(rR1, cS1) <SPAN style="color:#007F00">'first node name</SPAN>
<SPAN style="color:#00007F">Set</SPAN> rNodeNames = Range("CodeList") <SPAN style="color:#007F00">'subsequent node names</SPAN>
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rNodeName <SPAN style="color:#00007F">In</SPAN> bNodeNames
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
<SPAN style="color:#00007F">Set</SPAN> nodX = .Add(, , "R", rNodeName.Offset(, -cS1 + 1).Value) <SPAN style="color:#007F00">'adds nodes to the tree</SPAN>
nodX.Expanded = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Next</SPAN> rNodeName
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rNodeName <SPAN style="color:#00007F">In</SPAN> rNodeNames
<SPAN style="color:#00007F">With</SPAN> TreeView1.Nodes
<SPAN style="color:#007F00">'Fails at this line</SPAN>
<SPAN style="color:#00007F">Set</SPAN> nodX = .Add(Left(rNodeName.Value, -1 + InStrRev(rNodeName.Value, "_")), _
tvwChild, rNodeName, rNodeName.Offset(, -cS1 + 1).Value) <SPAN style="color:#007F00">' adds terminals</SPAN>
nodX.Expanded = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Next</SPAN> rNodeName
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>