Consulting

Results 1 to 3 of 3

Thread: Automating Userform Mapping to worksheet

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Automating Userform Mapping to worksheet

    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

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.
    [VBA]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[/VBA]

    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.

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •