Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Want to set ControlSource for ComboBox to different sheet from where I started the userform

0

I have a set of comboboxes and want to store the data entered in them to a different sheet, same workbook, from where I started the userform the comboboxes lives in.

Code is like this:

For x = 1001 To 1198

frmFasteOpplysninger.Controls("Combobox" & x).ControlSource = Sheets("Sjekkliste 2022").Cells(ActiveCell.Row, x).Address

Next x

The values entered in the comboboxes stills stores in the sheet I started the userforms from. I need a different sheet as the data belongs to yerars. I tried to reset the values for ControlSource as it could "remember" the old ones that where stored in the same sheet I started from. 

Answer
Discuss

Discussion

Bjotr. Did my revised Answer and second file help? 
John_Ru (rep: 6142) Jun 8, '22 at 3:43 pm
Add to Discussion

Answers

0
Selected Answer

Bjotr

You could just write values to the other sheet directly, using this (changes in bold):- I used it for the UserForm_Click event:

Private Sub UserForm_Click()

Dim CellAdd As String
Rw = ActiveCell.Row

For x = 1001 To 1198
    CellAdd = Cells(Rw, x).Address
    Sheets("Sjekkliste 2022").Range(CellAdd) = frmFasteOpplysninger.Controls("ComboBox" & x).Value
Next x

End Sub

I found it much trickier to set the ControlSource property successfully to a cell on the other sheet but found a way....

In the FIRST attached file, Sheet1 has a button to show a form with just 3 comboboxes. The code behind it is just:

Private Sub CommandButton1_Click()
frmFasteOpplysninger.Show
End Sub

(using your version of userform, frmFasteOpplysninger ).

If you pick a cell in Sheet1 (and note the row number), type values in the three boxes then click the lilac button "Set controlsource/ store values", this code will set the ControlSource per combobox so the values appear in the other sheet. I've added comments for you to follow (and delete if you like)

Private Sub CommandButton1_Click()

Dim CellAdd As String, FirstCB As Long

' store the CB number for start of loop
FirstCB = 1

Rw = ActiveCell.Row

For x = FirstCB To 3
    'create address string
    CellAdd = Cells(Rw, x).Address
    'get CB contents
    Temp = frmFasteOpplysninger.Controls("ComboBox" & x).Text
    'select this control
    frmFasteOpplysninger.Controls("ComboBox" & x).SetFocus
    ' set the controlsource
    frmFasteOpplysninger.Controls("ComboBox" & x).ControlSource = "Sjekkliste_2022!" & CellAdd
    ' rewrite the value
    frmFasteOpplysninger.Controls("ComboBox" & x).Text = Temp
Next x
' select the first control (so last is stored)
frmFasteOpplysninger.Controls("ComboBox" & FirstCB).SetFocus


' show stored values
Worksheets("Sjekkliste_2022").Activate
Cells(Rw, 1).Select
End Sub

It will show the sheet Sjekkliste_2022 (notice NO space in the name) and you can change values in the form and see them change in the sheet. Once you close the form however, the ControlSources reset to the default "".

Note that I found it was necessary to move between comboboxes (using .SetFocus) to get the value into the sheet. Likewise to move to the first CB (after the loop) to get the last value stored.

REVISION:

In the SECOND attached file, I guessed that you wanted to take some existing data and edit that/ store in sheet Sjekkliste_2022 so...

Sheet1 also has 4 rows of data (plus the button to show the form). In this file, the ComboBox values are taken from Sheet1 and the ControlSource set on the sheet Sjekkliste_2022. Pick one of the four data rows in Sheet1 (click Show...) and the row to be editted in that sheet then appears with a yellow fill (which is removed once the "Save..." button is clicked).

The variables are declared outside the individual codes (so useable between them) and you need to change the items in bold below:

Dim CellAdd As String, FirstCB As Long, LastCB As Long, wsOut As Worksheet, Rw As Long

Private Sub CommandButton1_Click()

'remove highlight from row
wsOut.Rows(Rw).Interior.Color = xlNone
MsgBox "Any changes saved to " & wsOut.Name & " (only)"
'close form
Unload Me
End Sub



Private Sub UserForm_Initialize()

' store the CB number for start and end of loop
FirstCB = 1
LastCB = 3
' state destination worksheet
Set wsOut = Worksheets("Sjekkliste_2022")

Rw = ActiveCell.Row
' loop in current sheet
For x = FirstCB To LastCB
    'get the value from the current row
    Me.Controls("ComboBox" & x).Value = Cells(Rw, x)

Next x

' show stored values
wsOut.Activate



For x = FirstCB To LastCB
    'create address string
    CellAdd = Cells(Rw, x).Address
    'get CB contents
    Temp = frmFasteOpplysninger.Controls("ComboBox" & x).Text
    'select this control
    frmFasteOpplysninger.Controls("ComboBox" & x).SetFocus
    ' set the controlsource
    frmFasteOpplysninger.Controls("ComboBox" & x).ControlSource = "Sjekkliste_2022!" & CellAdd
    ' rewrite the value
    frmFasteOpplysninger.Controls("ComboBox" & x).Text = Temp
Next x
' select the first control (so last is stored)
frmFasteOpplysninger.Controls("ComboBox" & FirstCB).SetFocus

'add highlight to row
wsOut.Rows(Rw).Interior.Color = vbYellow
MsgBox "You can now change values in " & wsOut.Name & vbCr & "(Press enter to change an item)"

End Sub

I think this should work with FirstCB=1001 and For x = FirstCB To To 1198 but I leave you to try.

Hope this helps

Discuss

Discussion

Yes, sure one could do it like that, but there will be about 350 comboboxes, and ten users at the same time. Will be a lot of saving for each change taking place. Continueing saving in the same sheet until I find out how to save to another sheet. 
Bjotr (rep: 6) Jun 7, '22 at 1:59 pm
Bjotr. Please see my revised answer/ file
John_Ru (rep: 6142) Jun 7, '22 at 3:43 pm
Bjotr. There's now a better, second file (...with form fill v0_c.xlsm) in the latest REVISION to my answer.
John_Ru (rep: 6142) Jun 8, '22 at 4:22 am
Hi Bjotr. Hope you fixed your problem but if not please see my revised answer and file- I think it does what you asked.
John_Ru (rep: 6142) Jun 11, '22 at 12:05 pm
Thanks for selecting my answer, Bjotr. Guess you've been very busy (I'd forgotten about giving that reply in June!) 
John_Ru (rep: 6142) Sep 14, '22 at 4:59 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login