PDA

View Full Version : Automating Userform Mapping to worksheet



simora
01-13-2011, 05:46 PM
Is there a way or some code to map Userform Textboxes and ComboBoxes to worksheet cells.
My userform has 32 components, Textboxes & Comboboxes. If there's any code or software that helps to map each component to a worksheet cell, I'll like to know.
Currently I'm doing this by individual component. Cell by Cell.
If no software, any ideas or suggestions welcome.

Thanks

mikerickson
01-14-2011, 04:26 PM
At some point, you'll have to (via code) say that Control X goes with Cell Y, for each of the controls. The best you can do, is do it once and not have to do it again.

This pseudo-code shows one approach.
Private Sub UserForm_Initialize()
TextBox1.Tag = Sheet1.Range("B10").Address(,,,True)
TextBox2.Tag = Sheet1.Range("C10").Address(,,,True)
TextBox3.Tag = Sheet2.Range("G10").Address(,,,True)
' ...
End Sub

Sub ReadFromSheet()
Dim oneControl as Variant

For Each oneControl in Array(TextBox1, TextBox2, TextBox3)
oneControl.Value = Range(oneControl.Tag).Value
Next oneControl
End Sub

Sub WriteToSheet()
Dim oneControl As Variant

For Each oneControl in Array(TextBox1, TextBox2, TextBox3)
Range(oneControl.Tag).Value = oneControl.Value
Next oneControl
End Sub

The assignment of the control to its cell is done once, in the Intialize code.
All interactions between those controls and the worksheet are done through that assignment, there is no need for hardcode values after the .Tag properties hold the .Addresses of the cells.

simora
01-15-2011, 06:11 AM
mikerickson:

RE: The best you can do, is do it once and not have to do it again.
Got it.
Liked the idea of doing it in the initialize code.
Thanks.