Populate Multiple Textboxes from a Range of Cells

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
185
Office Version
  1. 365
Platform
  1. Windows
I appraciate that the syntax in the below code is probably incorrect, however, I was wondering if there is a simpler way to create code to popualte multiple cells with a value from a range oc cells.

VBA Code:
If cboTest1.Value = "Test 1" Then
    texttBox1.Value - Sheets("Sheet1").Range(B2)
    texttBox2.Value - Sheets("Sheet1").Range(B3)
    texttBox3.Value - Sheets("Sheet1").Range(B4)
    texttBox4.Value - Sheets("Sheet1").Range(B5)
    texttBox5.Value - Sheets("Sheet1").Range(B6)
    
    End If

Typically I will be pulling values from different ranges for different values in CBOTest1

Example

VBA Code:
If cboTest1.Value = "Test 2" Then
    texttBox1.Value - Sheets("Sheet1").Range(D2)
    texttBox2.Value - Sheets("Sheet1").Range(D3)
    texttBox3.Value - Sheets("Sheet1").Range(D4)
    texttBox4.Value - Sheets("Sheet1").Range(D5)
    texttBox5.Value - Sheets("Sheet1").Range(D6)
    
    End If

As there will be multiple values in cboTest1 I am looking for to reduce the number of lines of code
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Just an example looking at the values and ranges in your code. Can be modified in more than one way, i.e., use Cells() and column numbers etc.

VBA Code:
Dim i as Long
Dim col as String

Select Case cboTest1.Value
    Case "Test 1"
        col = "B"
    Case "Test 2"
        col = "D"
End Select

For i = 1 to 5
    Me.Controls("texttBox" & i).Text = Sheets("Sheet1").Range(col & i + 1).Value
Next i

Edit: I assumed controls are on a userform. May not follow through due to time constraints.
 
Upvote 0
Just an example looking at the values and ranges in your code. Can be modified in more than one way, i.e., use Cells() and column numbers etc.

VBA Code:
Dim i as Long
Dim col as String

Select Case cboTest1.Value
    Case "Test 1"
        col = "B"
    Case "Test 2"
        col = "D"
End Select

For i = 1 to 5
    Me.Controls("texttBox" & i).Text = Sheets("Sheet1").Range(col & i + 1).Value
Next i

Edit: I assumed controls are on a userform. May not follow through due to time constraints.
Thank you I will apply this to a file I am working on and revert
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,560
Members
449,089
Latest member
Motoracer88

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