Search code examples
excellabeluserform

Using active cell value in userform label


I open a userform when double-clicking on a cell;

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Range("B5:K20"), Target) Is Nothing Then

    Cancel = True
    UserForm1.Show

    End If

End Sub

I would like to add the cell value from the cell I double clicked to Label1 on the userform.

I tried UserForm1.Label1.Caption = ActiveCell.Value after UserForm1.Show. On the first double click, the userform shows no value in Label1, the second time I double click a cell, Label1 shows the cell value of the first cell I double clicked.

How can I show the current (double-clicked) cell value when the userform opens?


Solution

  • Possible tell excel that the label is = to teh taget before showing the form.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim s As String
        If Not Intersect(Range("B5:K20"), Target) Is Nothing Then
            s = Target.Value
    
            UserForm1.Label1.Caption = s
            UserForm1.Show
        End If
        Cancel = True
    
    End Sub