new to userforms..question about populating a lable box

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,604
Office Version
  1. 365
Platform
  1. Windows
Hi all

assuming that
Code:
Sub Dispay_userform()
rows.Show
End sub
will display my rows userform
how, when it is displayed can I get the Lable4 box to be populated with the value in cell T2 in the data worksheet?

thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,

Assuming your worksheet is named 'Data', try:
Code:
Private Sub UserForm_Initialize()
Label4.Caption = CStr(Sheets("Data").Range("T2").Value)
End Sub
 
Upvote 0
Try like this (I wouldn't call your form rows - that is a VBA keyword)

Code:
Sub Dispay_userform()
With UserForm1
    .Label4.Caption = Sheets("Data").Range("T2").Value
    .Show
End With
End Sub
 
Upvote 0
Hi, yep thats works - thanks

I have a follow up question which is more tricky and well beyond my simple vba knowledge !

This code asks the user (via input box) to input the number of rows to be inserted....
Code:
Sub InsertRows2()
    Dim Rws As Long, LastRw As Long
    
    Do
        Rws = Application.InputBox( _
            Title:="Insert how many rows?", _
            Prompt:="Enter the number of rows to be inserted", _
            Type:=1)
    Loop While Rws < 0
    If Rws > 0 Then
        LastRw = Range("B" & rows.Count).End(xlUp).Row
        If LastRw < 7 Then
            MsgBox "Unable to add rows in correct location"
        Else
            rows(LastRw - 6).Resize(Rws).Insert
        End If
    End If
End Sub

when my userform (now called 'insertrows') is displayed stating that xx number of rows need to be added,once the user clicks on the yes command button can the code above be added to ur previous code to insert the number of rows stated..

Code:
Sub Dispay_userform()
With Insertrows
    .Label4.Caption = Sheets("Data").Range("T2").Value
    .Show
End With
End Sub

so above code will state the number of rows to be added is xx (xx being the value of cell T2 from data tab)
when user selects the 'yes' command button the code from the very top of this post runs to insert xx rows

hope that makes sense and many thanks
 
Upvote 0
Maybe like this

Code:
Sub Dispay_userform()
With InsertRows
    .Label4.Caption = Sheets("Data").Range("T2").Value
    .Show
    ActiveCell.Resize(.Label4.Caption).EntireRow.Insert
End With
End Sub
 
Upvote 0
VoG

The xx rows are inserted at the very top of the sheet
as per the code to insert the given number of rows, these are at the bottom of the sheet column B with an ofset of 6 rows....this code works great

Code:
Sub InsertRows2()
    Dim Rws As Long, LastRw As Long
    
    Do
        Rws = Application.InputBox( _
            Title:="Insert how many rows?", _
            Prompt:="Enter the number of rows to be inserted", _
            Type:=1)
    Loop While Rws < 0
    If Rws > 0 Then
        LastRw = Range("B" & rows.Count).End(xlUp).Row
        If LastRw < 7 Then
            MsgBox "Unable to add rows in correct location"
        Else
            rows(LastRw - 6).Resize(Rws).Insert
        End If
    End If
End Sub
but instead of using the value input into the input box I want the value used from cell ref T2 in data worksheet, ie that shown in my userform (Lable4)

:eeek:

thanks again
 
Upvote 0
Like this maybe

Code:
Sub Dispay_userform()
Dim Rws As Long, LastRw As Long
Rws = Sheets("Data").Range("T2").Value
With InsertRows
    .Label4.Caption = Rws
    .Show
End With
If Rws > 0 Then
    LastRw = Range("B" & Rows.Count).End(xlUp).Row
    If LastRw < 7 Then
        MsgBox "Unable to add rows in correct location"
    Else
        Rows(LastRw - 6).Resize(Rws).Insert
    End If
End If
End Sub
 
Upvote 0
almost.....

Just want the insert action linked to the 'Yes' command button on my userform, at the moment it iserts on closing the form by clicking on the X button

cheers
 
Upvote 0
Maybe like this then

Code:
Private Sub CommandButton1_Click()
Dim Rws As Long, LastRw As Long
Rws = Sheets("Data").Range("T2").Value
With InsertRows
    .Label4.Caption = Rws
    .Show
End With
If Rws > 0 Then
    LastRw = Range("B" & Rows.Count).End(xlUp).Row
    If LastRw < 7 Then
        MsgBox "Unable to add rows in correct location"
    Else
        Rows(LastRw - 6).Resize(Rws).Insert
    End If
End If
End Sub
 
Upvote 0
Hi,

wouldnt a simple MsgBox (instead of a userform) suit, e.g.
Code:
Sub xxx()
Dim Rws As Long, LastRw As Long

Rws = Sheets("Data").Range("T2").Value

If MsgBox(prompt:="do you want to insert " & Sheets("Data").Range("T2").Value & " rows?", _
          Buttons:=vbQuestion + vbYesNo) = vbYes Then
    If Rws > 0 Then
        LastRw = Range("B" & Rows.Count).End(xlUp).Row
        If LastRw < 7 Then
            MsgBox "Unable to add rows in correct location"
        Else
            Rows(LastRw - 6).Resize(Rws).Insert
        End If
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

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