richh
Board Regular
- Joined
- Jun 24, 2007
- Messages
- 245
- Office Version
- 365
- 2016
I am using Excel 2016. I am attempting to loop through a set of controls within a frame in a specific order and writing their content to a worksheet. I've tried defining the tab indexes, which seems to do what I need it to, for the most part, but a few controls are not looping in the correct order. I could force the program to write to specific columns, but that's not really all that dynamic; plus, I'd have to write a unique loop for each frame as their controls all differ.
The tab indexes of the controls in one of the frame are as follows:
The tab indexes of the controls in one of the frame are as follows:
- Assignment - combobox (CB)
- Type - CB
- year - CB
- Version - CB
- Desc - textbox (TB)
- Status - CB
- ProgNum - TB
- award - CB
- scale - CB
- resources - TB
- Assignment
- Type
- Year
- Version
- Desc
- Status
- Award (out of order)
- Scale (out of order)
- Resources (out of order)
- ProgNum (out of order)
VBA Code:
Private Sub sNodeButton_Click()
Dim lRow As Long
Dim ws As Worksheet
Dim thisNode As String
Dim startCol As Integer
Dim frameName As String
startCol = 10
Set ws = Application.ThisWorkbook.Worksheets(1)
lRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
For Each tNode In Me.TreeView1.Nodes 'Find the selected node and write data to its row
If tNode.Selected = True Then
For i = 2 To lRow
If ws.Cells(i, 2) = tNode.Key Then
ws.Cells(i, 5) = "Saved"
Select Case Left(tNode.Key, 2)
Case "PR"
frameName = "programFrame"
Case "PC"
frameName = "progCertFrame"
Case "CC"
frameName = "courseCertFrame"
Case "CO"
frameName = "courseFrame"
Case "MO"
frameName = "moduleFrame"
Case "CP"
frameName = "compentencyFrame"
End Select
For Each ctrl In Me.Controls(frameName).Controls
If TypeName(ctrl) = "ComboBox" Or TypeName(ctrl) = "TextBox" Then
ws.Cells(i, startCol) = ctrl.Value
startCol = startCol + 1
End If
Next ctrl
Exit For
End If
Next i
Exit For
End If
Next tNode
Me.TreeView1.Enabled = True
Call ResetTree
Application.ThisWorkbook.Save
Set ws = Nothing
End Sub