Link to home
Start Free TrialLog in
Avatar of yuppydu
yuppyduFlag for Italy

asked on

Organize tabindex in vba form with vba code

Hi, I have a multipage form with a large number of textboxes and comboboxes. I'd like to assign a specific Tabindex to each one of them so that pressing Tab or Enter the cursor moves from left to right. Presently it moves from top to botton, the way I've copied the boxes in the form.
I've written a vba code which read from a table in a sheet the Textbox name and assigns a Tabindex value, but it does not work. I do not get any error messages but the value are not saved in the tabindex property.
The table starts in a cell named "starttab" and the name of the boxes are written in that column (20 per column). The tabindex value is written 2 columns to the right and columns with the textbox name are 3 columns apart one another.
Again no error messages, debug works fine, but the tabindex value are not upgraded.
Option Explicit
Dim sBoxName As String
Dim iColumnOffset As Integer
Dim iRowoffset As Integer
Dim iTabOrder As Integer

Sub taborder()
For iColumnOffset = 0 To 33 Step 3
For iRowoffset = 1 To 20
With wksMenuSheet.Range("starttab").Offset(iRowoffset, iColumnOffset)
sBoxName = .Value
iTabOrder = .Offset(0, 2)
 FPortfolioInput.Controls(sBoxName).TabIndex = iTabOrder
End With
Next iRowoffset
Next iColumnOffset
MsgBox ("Done")
End Sub

Open in new window

Avatar of Jan Karel Pieterse
Jan Karel Pieterse
Flag of Netherlands image

You are setting the tabindexes during runtime, to an instance of your userform in memory. Instead, you should be setting the tabindex at design time, using the "Microsoft Visual Basic For Applications Extensibility" library.

Dim oCtl as Control
For Each oCtl in ActiveWorkbook.VBProject.VBComponents(YourFormName).Designer.Controls
oCtl.TabIndex=GetIndexFromSomewhere
Next
Avatar of yuppydu

ASKER

I've tried something like that but I get an error at the for each.... command.
Do I have to attach a library?
Avatar of yuppydu

ASKER

My table for textbox names and tabindex value is organized like this. How do i make the cursor go down the first column to read the texbox name and then move right 2 columns to read the tabindex value? Is my with and for routine valid?
excel-table.gif
Tools, references, check this one: Microsoft Visual Basic For Applications Extensibility

Sub Tabs()
    Dim oCell As Range
    For Each oCell In Range("A2:A21")
        With ActiveWorkbook.VBProject.VBComponents(YourFormName).Designer
            .Controls(oCell.Value).TabIndex = oCell.Offset(, 2).Value
        End With
    Next
End Sub

Open in new window

Avatar of yuppydu

ASKER

it's giving me Type mismatch in  With ActiveWorkbook.VBProject.VBComponents(fportfolioinput).Designer
Avatar of yuppydu

ASKER

this is just one of the columns. The full table for textboxes looks like this, and I have 9 more pages in the form. This is why I'm looking for a code to do it. Manually would be a nightmare
excel-table.gif
ASKER CERTIFIED SOLUTION
Avatar of Jan Karel Pieterse
Jan Karel Pieterse
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yuppydu

ASKER

Thanks, it seems to work fine, few mistakes in my table of values but that's my issue. Your code it perfect. That's going to save me a lot of time, I have more than 500 textboxes tabindex