Loop text value for each combobox

akaseto

New Member
Joined
Oct 10, 2021
Messages
18
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
hello,
i've an example 4 combobox will linked to 4 cells, for example : combobox1 linked with cell B6 and so on.

Sub tryout()
Dim i As Integer, j As Integer
Dim ws As Worksheet

Set ws = ActiveSheet

For i = 1 To 4
For j = 6 To 9
With ws.OLEObjects("Combobox" & i)
.Value = range("B" & j).Value
End With
Next
Next
End Sub
i know my code is funny, and yes its only add the last value (9) into evey combobox from 1 - 4.
what i want is
-combobox1 is linked with B6 text value
-combobox2 with B7 text value
-combobox3 with B8 text value
-combobox4 with B9 text value

pls help me, thanks before
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:
VBA Code:
Sub tryout()
    Dim i As Integer, j As Integer
    Dim ws As Worksheet
    Dim vCombo As Variant
    Dim vRange As Variant
    Dim sCombo As String
    Dim sRange As String
    
    sCombo = "1,2,3,4"
    sRange = "6,7,8,9"
    
    vCombo = Split(sCombo, ",")
    vRange = Split(sRange, ",")
    
    Set ws = ActiveSheet
    
    For i = LBound(vCombo) To UBound(vCombo)
        With ws.OLEObjects("Combobox" & vCombo(i))
            .Value = Range("B" & vRange(i)).Value
        End With
    Next
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub tryout()
    Dim i As Integer, j As Integer
    Dim ws As Worksheet
    Dim vCombo As Variant
    Dim vRange As Variant
    Dim sCombo As String
    Dim sRange As String
  
    sCombo = "1,2,3,4"
    sRange = "6,7,8,9"
  
    vCombo = Split(sCombo, ",")
    vRange = Split(sRange, ",")
  
    Set ws = ActiveSheet
  
    For i = LBound(vCombo) To UBound(vCombo)
        With ws.OLEObjects("Combobox" & vCombo(i))
            .Value = Range("B" & vRange(i)).Value
        End With
    Next
End Sub

thanks for the help, i really appreciated it
its work but when i merged to my main code it crashed with another code, i found the simplest way for me is renaming combobox1 start from combobox6 and using this code :

VBA Code:
Sub tryout()

    Dim i As Integer

    Dim ws As Worksheet

  

    Set ws = ActiveSheet

  

    For i = 6 To 20

        With ws.OLEObjects("Combobox" & i)

            Object.Value = ws.Range("B" & i).Value

        End With

    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top