How to use array values from userform as a variable in worksheet

nsook

New Member
Joined
Mar 10, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I have 4 separate user forms that have a variable number of textboxes and labels based on a user defined parameter. For this question I will be looking at one in particular, however let me first list my global variables:
Option Explicit
Public BuildingNumber As Long
Public UnitNumber As Long
Public FixtureNumber As Long
Public array1() As String
Public array2() As String
Public array3() As String
Public array4() As String

At the very bottom is my current code for my first userform. The rest of my userforms are fairly similar in terms of code they just operate for different constraints. I am trying to set the value entered into txtBox 1 through however many I have get saved into an array that then is used when i click on a commandbutton on the last userform which has the following code:
Private Sub CommandButton1_Click()

Dim i As Long
Dim j As Long
Dim s As Long
Dim iFirst As String
Dim iLast As String
Dim iRange As Range

s = 0
Cells(5, 1).Value = "Units"
For i = 1 To BuildingNumber
Cells(4, 1 + i + s).Value = array1(i)
iFirst = Selection.Offset(4, 1 + i + s).Address
iLast = Selection.Offset(4, 1 + i + s + array2(i)).Address
Set iRange = Range(iFirst & ":" & iLast)
iRange.Merge
For j = 1 To array2(i)
Cells(5, 1 + j + s).Value = "Floor " & j
s = s + j
Next j
Next i
End Sub

Array1 in the above code is my building name and array2 is the number of floors that building has. Essentially I am trying to make it look like the below table except the building name gets merged based on the number of floors that building has. So Building 1 would be merged with the next 3 cells and building 2 would merge with the next 2 cells. However the number of floors changes from projects to project so I want it to be user defined.

Building 1Building 2
UnitsFloor 1Floor 2Floor 3Floor 4Floor 1Floor 2Floor 3

I apologize if this is confusing, if it is please let me know and will try to make my question more concise. Thank you for helping with this!

Option Explicit


Private Sub UserForm_Initialize()
Dim i As Long


BuildingNumber = InputBox("How Many Buildings Have Units?", "Enter Building Number")
UnitNumber = InputBox("How Many Unit Types Are There on This Job?", "Enter Unit Number")
FixtureNumber = InputBox("How Many Fixture Types Are There in Units?", "Enter Fixture Number")

Dim txtB1 As Control
ReDim array1(1 To BuildingNumber)
For i = 1 To BuildingNumber
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "txtBox" & i
If i <= 25 Then
.Height = 20
.Width = 75
.Left = 100
.Top = 20 * i * 1
ElseIf 25 < i And i <= 50 Then
.Height = 20
.Width = 75
.Left = 300
.Top = 20 * (i - 25) * 1
ElseIf 50 < i And i <= 75 Then
.Height = 20
.Width = 75
.Left = 500
.Top = 20 * (i - 50) * 1
Else
End If
array1(i) = "txtBox" & i
End With

Next i

Dim lblL1 As Control
For i = 1 To BuildingNumber
Set lblL1 = Controls.Add("Forms.Label.1")
With lblL1
.Caption = "Building " & i & " Name"
.Name = "lbl" & i
If i <= 25 Then
.Height = 20
.Width = 75
.Left = 20
.Top = 20 * i * 1
ElseIf 25 < i And i <= 50 Then
.Height = 20
.Width = 75
.Left = 220
.Top = 20 * (i - 25) * 1
ElseIf 50 < i And i <= 75 Then
.Height = 20
.Width = 75
.Left = 420
.Top = 20 * (i - 50) * 1
Else
End If
End With
Next i
If i <= 25 Then
UserForm1.Height = 640
UserForm1.Width = 220
ElseIf 25 < i And i <= 50 Then
UserForm1.Height = 640
UserForm1.Width = 420
ElseIf 50 < i And i <= 75 Then
UserForm1.Height = 640
UserForm1.Width = 620
Else
End If
End Sub

Private Sub CommandButton1_Click()

UserForm2.Show

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I have 4 separate user forms that have a variable number of textboxes and labels based on a user defined parameter. For this question I will be looking at one in particular, however let me first list my global variables:
Option Explicit
Public BuildingNumber As Long
Public UnitNumber As Long
Public FixtureNumber As Long
Public array1() As String
Public array2() As String
Public array3() As String
Public array4() As String

At the very bottom is my current code for my first userform. The rest of my userforms are fairly similar in terms of code they just operate for different constraints. I am trying to set the value entered into txtBox 1 through however many I have get saved into an array that then is used when i click on a commandbutton on the last userform which has the following code:
Private Sub CommandButton1_Click()

Dim i As Long
Dim j As Long
Dim s As Long
Dim iFirst As String
Dim iLast As String
Dim iRange As Range

s = 0
Cells(5, 1).Value = "Units"
For i = 1 To BuildingNumber
Cells(4, 1 + i + s).Value = array1(i)
iFirst = Selection.Offset(4, 1 + i + s).Address
iLast = Selection.Offset(4, 1 + i + s + array2(i)).Address
Set iRange = Range(iFirst & ":" & iLast)
iRange.Merge
For j = 1 To array2(i)
Cells(5, 1 + j + s).Value = "Floor " & j
s = s + j
Next j
Next i
End Sub

Array1 in the above code is my building name and array2 is the number of floors that building has. Essentially I am trying to make it look like the below table except the building name gets merged based on the number of floors that building has. So Building 1 would be merged with the next 3 cells and building 2 would merge with the next 2 cells. However the number of floors changes from projects to project so I want it to be user defined.

Building 1Building 2
UnitsFloor 1Floor 2Floor 3Floor 4Floor 1Floor 2Floor 3

I apologize if this is confusing, if it is please let me know and will try to make my question more concise. Thank you for helping with this!

Option Explicit


Private Sub UserForm_Initialize()
Dim i As Long


BuildingNumber = InputBox("How Many Buildings Have Units?", "Enter Building Number")
UnitNumber = InputBox("How Many Unit Types Are There on This Job?", "Enter Unit Number")
FixtureNumber = InputBox("How Many Fixture Types Are There in Units?", "Enter Fixture Number")

Dim txtB1 As Control
ReDim array1(1 To BuildingNumber)
For i = 1 To BuildingNumber
Set txtB1 = Controls.Add("Forms.TextBox.1")
With txtB1
.Name = "txtBox" & i
If i <= 25 Then
.Height = 20
.Width = 75
.Left = 100
.Top = 20 * i * 1
ElseIf 25 < i And i <= 50 Then
.Height = 20
.Width = 75
.Left = 300
.Top = 20 * (i - 25) * 1
ElseIf 50 < i And i <= 75 Then
.Height = 20
.Width = 75
.Left = 500
.Top = 20 * (i - 50) * 1
Else
End If
array1(i) = "txtBox" & i
End With

Next i

Dim lblL1 As Control
For i = 1 To BuildingNumber
Set lblL1 = Controls.Add("Forms.Label.1")
With lblL1
.Caption = "Building " & i & " Name"
.Name = "lbl" & i
If i <= 25 Then
.Height = 20
.Width = 75
.Left = 20
.Top = 20 * i * 1
ElseIf 25 < i And i <= 50 Then
.Height = 20
.Width = 75
.Left = 220
.Top = 20 * (i - 25) * 1
ElseIf 50 < i And i <= 75 Then
.Height = 20
.Width = 75
.Left = 420
.Top = 20 * (i - 50) * 1
Else
End If
End With
Next i
If i <= 25 Then
UserForm1.Height = 640
UserForm1.Width = 220
ElseIf 25 < i And i <= 50 Then
UserForm1.Height = 640
UserForm1.Width = 420
ElseIf 50 < i And i <= 75 Then
UserForm1.Height = 640
UserForm1.Width = 620
Else
End If
End Sub

Private Sub CommandButton1_Click()

UserForm2.Show

End Sub
I've been looking into this and started wondering should I be making a class module instead of a normal module for this? If so would it be easier to consolidate my 4 userforms into one that just has multiple tabs?
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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