Gap between 65 textboxes and labels is huge. Require the perfect gap between eachother

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hello

What i want is to display 65 textboxes and 65 labels adjacent to each other in 3 or 5 columnar way on the userform at run time.

Textbox and Label Top Position starts from 144

so when used the following in below code for both controls textbox and label .Top = 144 * i * 1 the gap between each Label and Textbox is huge and looks bad.
How can i get the perfect gap between each label and Textbox respectively below each other and also perfect gap for columnar representation on userform.

Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
Label TextBox Label TextBox Label TextBox
......so on


Code:
Private Sub UserForm_Initialize()
Dim i As Integer
Dim txtbxTopPos As Integer
Dim txtbxGap As Integer


Dim txtB1 As Control
Dim labl As Control


For i = 1 To 65
Set txtB1 = Controls.Add("Forms.TextBox.1")
Set labl = Controls.Add("Forms.Label.1")
With txtB1
.Name = "txtBox" & i
.Height = 15.75 
.Width = 126    
.Left = 102     
.Top = 144 * i * 1
End With


With labl
.Name = "lbl" & i
.Height = 15.75 
.Width = 126    
.Left = 10     
.Top = 144 * i * 1
.BackStyle = 0
.Caption = Sheet1.Cells(1, i).Value
End With

Next i
End Sub

Thankx NimishK
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I would have thought that this was creating an issue

Code:
.Top = 144 * i * 1
When i is greater than 1 it is multipying i by 144....Have you tried replacing it with

Code:
.Top = 144 +.width + 10
 
Upvote 0
Thankx Michael for quicker response just tried your code. but all textboxes are at one place. it should have been displayed one below the other in vertical mannner or horizontal way
 
Upvote 0
Ok, don't have Excel at the moment, but try adding the line in red...

Code:
With textBox & i
.Name = "textBox" & i
.Height = 15.75
.Width = 126
.Left = 102
.Top = 144
End With


With labl
.Name = "lbl" & i
.Height = 15.75
.Width = 126
.Left = 10
.Top = 144
.BackStyle = 0
.Caption = Sheet1.Cells(1, i).Value
End With
[color=red]txtB1.Top = txtB1.Top + txtB1.Width + 10[/color]
[color=red]labl.Top = labl.Top + labl.Width + 10[/color]
Next i
 
Last edited:
Upvote 0
No Dear. Still not working. still shows the last text box at 144 rather than showing one below another
Will request you to try when Excel is in front of you.:)
 
Upvote 0
Some Success with following
txtB1.Top = txtB1.Top + 25 * i
labl.Top = labl.Top + 25 * i

but not through with horizontal presentation. by the way 10 was almost touching all the textboxes together i.e why 25
 
Upvote 0
Some Success with following
txtB1.Top = txtB1.Top + 25 * i
labl.Top = labl.Top + 25 * i


but not through with horizontal presentation. by the way 10 was almost touching all the textboxes together i.e why 25
in above atleast displayed one after another and also Observed that the gap increases if the number of text box increases.
How can these be fixed
 
Upvote 0
Remove the *1 from your equation.
the * i will give you a lerger gap as i increases ??
Do the same with .Left = 102

txtB1.left = txtB1.left + 25 * i

You can use any figure 10, 25, etc....whatever suits your needs !
 
Last edited:
Upvote 0
Remove the *1 from your equation.
the * i will give you a lerger gap as i increases ??
Do the same with .Left = 102

txtB1.left = txtB1.left + 25 * i

You can use any figure 10, 25, etc....whatever suits your needs !
Your above suggestion with .left led to following
Label TextBox
Label TextBox

Label TextBox
in slanting manner and also not in one particular line
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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