Excel - VBA : pass variable from Sub to Userform

前端 未结 2 814
轮回少年
轮回少年 2021-01-11 16:59

I have read and applied solution I found on similar topics but nothing seem to work in my case.

So, I want to pass a variable from one sub of my Module1 to a userfo

2条回答
  •  渐次进展
    2021-01-11 17:41

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Dim selectColumn
    selectColumn= Split(Target.Address(1, 0), "$")(0)
    
    Call UserFormStart(selectColumn)
    End Sub
    

    Inside Main Module

    Public columnSelection As String
    ...
    Public Sub UserFormStart(ByVal columnRef As String)
        'MsgBox "Debug columnRef=" & columnRef
        columnSelection = columnRef
        UserForm1.Show
    End Sub
    

    Inside UserForm

    Private Sub UserForm_Initialize()
    
    'MsgBox "Debug UserForm_Initialize =" & columnSelection
    ...
    
    End Sub
    

    Worksheet_SelectionChange calls a sub on the module where columnSelection is declared as public and visable from the UserForm. I used three different variables for the Column Reference to show that there is where the UserForm has access to the Module. The above all works and took ages to find and work out hence the submission. Happy hunting folks

提交回复
热议问题