• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

User forms: populate option boxes from sheet|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / User forms: populate option boxes from sheet|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosUser forms: populate option boxes f…
sp_PrintTopic sp_TopicIcon
User forms: populate option boxes from sheet
Avatar
kary smithers
south africa

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
May 5, 2020
sp_UserOfflineSmall Offline
1
May 5, 2020 - 7:26 pm
sp_Permalink sp_Print

Hi, thank you for taking my question.

I have an Excel userform which has multiple controls including option boxes consisting of up to 8 choices.  When a new record is added I find the first empty row in my spreadsheet and use the offset command to place the data in the correct column (eg: ActiveCell.Offset(0, 1) = txtInputDate.Value).  For option boxes, I use the Caption from the option box selected and place it in the spreadsheet using a function as shown below:

Public Function FillOptionBoxes(stframe, stOffset)
Dim Ctrl As MSForms.Control

For Each Ctrl In frmNewRecords.Controls(stframe).Controls
 If TypeName(Ctrl) = "OptionButton" And Ctrl = True Then
  ActiveCell.Offset(0, stOffset) = Ctrl.Caption
 End If
Next

End Function

Now I want to be able to read this information back into the form in order to edit records.  Is there a similar method I could use?

Thank you

 

  

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
May 5, 2020 - 11:48 pm
sp_Permalink sp_Print

Hi,

It looks to me like you just need to alter the logic slightly:

 

For Each Ctrl In frmNewRecords.Controls(stframe).Controls
If TypeName(Ctrl) = "OptionButton" And Ctrl.Caption = ActiveCell.Offset(0, stOffset).Value Then
Ctrl = True

Exit For
End If
Next

sp_AnswersTopicAnswer
Answers Post
Avatar
kary smithers
south africa

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
May 5, 2020
sp_UserOfflineSmall Offline
3
May 6, 2020 - 5:30 pm
sp_Permalink sp_Print

Thank you Velouria for your reply - it works perfectly! I appreciate your help. 

I have put this code into a function called ReadOptionBoxes which I call like this: 

stoffset = 35
stframe = "FrameGender"
Call ReadOptionBoxes(stframe, stoffset)

However, following on to this, since I have around 10-15 sets of option boxes (eg: Gender, Socio-economic status, marital status, etc), the code to fill the variables and call the function is quite repetitive and seems wasteful.  I would like to set up a table with all  the offsets and frame names (eg: FrameGender, offset 10; FrameSocioEcon, offset 11) and loop through these to read all the option groups. I just can't get my head around the best way to do this.  I'd be grateful for any advice.  Many thanks 🙂

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
May 6, 2020 - 8:13 pm
sp_Permalink sp_Print

You could just put them into a 2 column table/range, then simply process each row using the first column as the frame name and the second as the offset value.

Avatar
kary smithers
south africa

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
May 5, 2020
sp_UserOfflineSmall Offline
5
May 7, 2020 - 1:01 am
sp_Permalink sp_Print

Thanks, I will try that.  I've become a bit bogged down by a logic problem related to my last query.  When I move up/down my records I use the UpdateDisplay Sub to select the row and populate the form.  However, the info from the optionboxes doesn't reflect the correct info - it is one "move" behind i.e. it lags behind by one row when I spin up/down. I'm not sure if this makes sense but I'd be grateful if you could put me out of my misery!  Many thanks, 🙂   

Private Sub UpdateDisplay()
'called when record added/deleted/moved

 With VMPTable
    RecordPosition.Caption = CurrentRow & " of " & .ListRows.Count
    Populate .ListRows(CurrentRow).Range
                 .ListRows(CurrentRow).Range.Select
 End With
End Sub

Private Sub Populate(SelectedRow As Range)
...declare variables, etc
  With SelectedRow
     txtRefNo.Value      =  .Cells(1, 1).Value
     txtInputDate.Value = .Cells(1, 2).Value
    'Frame3 - Case Type (GBV/DV/Other)
      stoffset = 34
      stframe = "Frame3"
        Call ReadOptionBoxes(stframe, stoffset)
  End With
End Sub

Public Function ReadOptionBoxes(stframe, stoffset)
Dim Ctrl As MSForms.Control
For Each Ctrl In frmNewRecords.Controls(stframe).Controls
  If TypeName(Ctrl) = "OptionButton" And Ctrl.Caption = ActiveCell.Offset(, stoffset).Value Then
    Ctrl = True
   Exit For
  ElseIf TypeName(Ctrl) = "OptionButton" And Ctrl.Caption <> ActiveCell.Offset(, stoffset).Value Then
    Ctrl = False
  End If
Next
End Function

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
6
May 7, 2020 - 7:04 pm
sp_Permalink sp_Print

Your ReadOptionBoxes routine uses the active cell, but your UpdateDisplay routine doesn't move the selection until after it's populated everything. If I were you, I'd pass the relevant range (or a row number) to the ReadOptionBoxes routine rather than relying on the active cell.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Jan pedersen, Francis Drouillard
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
wahab tunde
Cong Le Duc
Faisal Bashir
Ivica Cvetkovski
Blaine Cox
Shankar Srinivasan
riyepa fdgf
Hannah Cave
Len Matthews
Kristine Arthy
Forum Stats:
Groups: 3
Forums: 24
Topics: 6205
Posts: 27209

 

Member Stats:
Guest Posters: 49
Members: 31878
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.