Userform VBA to fill Dynamically TextBoxes

ricafonyat

New Member
Joined
May 1, 2017
Messages
9
Office Version
  1. 365
Platform
  1. Windows
My userform has 10 textboxes with the following names:

TB_Year1
TB_Year2
TB_Year3
TB_Year4
TB_Year5
TB_Year6
TB_Year7
TB_Year8
TB_Year9
TB_Year10

And I have two named ranges in my worksheet("Sheet1")

Range1 --- > firstYear example: 2022
Range2 --- > totalYears example: 5

My goal is to fill the TB_Year1 with the value of the FirstYear and the rest adding 1 year until totalYears

Any help would be appreciated
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long
  If Range("Range2").Value < 11 Then
    For i = 1 To Range("Range2").Value
      Controls("TB_Year" & i) = Range("Range1").Value + i - 1
    Next
  End If
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Here is my final code and it works perfecty!

VBA Code:
Private Sub UserForm_Activate()
  
  TotalYears = shDataBase.Range("TotalYears")
  OPeningYear = shDataBase.Range("OpeningYear")
  
  Dim i As Long
    For i = 1 To TotalYears
      Controls("TBRoomsSegmentYear" & i) = OPeningYear + i - 1
    Next

  Dim j As Long
    For j = 1 To TotalYears
      Controls("LBYear" & j) = OPeningYear + j - 1
    Next

End Sub

Best Regards
 
Upvote 0
It can be simplified to this:

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long, TotalYears As Long, OPeningYear As Long
  
  TotalYears = shDataBase.Range("TotalYears")
  OPeningYear = shDataBase.Range("OpeningYear")
  
  For i = 1 To TotalYears
    Controls("TBRoomsSegmentYear" & i) = OPeningYear + i - 1
    Controls("LBYear" & i) = OPeningYear + i - 1
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,219
Messages
6,123,689
Members
449,117
Latest member
Aaagu

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