How do I align a UserForm next to the active cell?

后端 未结 2 425
离开以前
离开以前 2020-12-16 02:16

I have a UserForm of a MonthView that opens when I click in the specified range of cells. This SO thread gave me the basic script. It doesn\'t put the UserForm where I expec

2条回答
  •  别那么骄傲
    2020-12-16 03:05

    Question 1: I have the UserForm StartUpPosition property set to 0 - Manual - is this correct? Yes, it's correct. In the code below, I am setting this property in the code.

    Question 2: When I click any cell in the specified range, for the first time after opening the workbook, the UserForm always opens in the far top left corner of the screen. Why? I think the answer to this is somewhat related to question #3. That would seem to be a default location for the form to open in. The way you have the code now, trying to set the form top and left coordinates in the Worksheet_SelectionChange event is not working because the coordinates are never actually getting set. The setting of the coordinates needs to be moved to the userform initialization event.

    Question 3: When I click any cell in the specified range, for any clicks after the first, the UserForm opens relative to the previous cell that was active, instead of the one I just clicked. How do I get it to open relative to the cell just clicked, instead of relative to the previous active cell? This problem is also related to the code being in the wrong place. As noted above, the coordination setting needs to take place in the userform initialization event. As to why it's referencing the previous active cell, my guess would be that the active cell doesn't actually get changed until after the worksheet selection change event completes. So since you are trying to set the coordinates within this event (i.e. - before the event finishes), you are getting the previously active cell. Again, moving the code to the correct location fixes this issue.

    Question 4: Why does it appear to align the bottom of the UserForm instead of the top? There appears to be a difference between the definition of "top" when it comes to cells (ranges) vs userforms. The top of the cell is measured from the first row whereas the top of the userform seems to be measured from the top of the Excel application. So in over words, if activecell.top and userform.top are both equal to 144, they will be different locations on the screen. This is because the top of activecell is 144 points down from the first row in the Excel spreadsheet while the top of the userform is 144 points down from the top of the Excel application (i.e. - the top of the Excel window), which is higher on the screen because the starting point (top of the Excel window) is higher than the starting point for activecell.top (first row in the spreadsheet). We can adjust for that by adding the height of the userform plus the height of the active cell to the top coordinate.

    Sheet module code

    Private Sub Worksheet_SelectionChange(ByVal target As Range)
    
        Dim oRange As Range
    
        Set oRange = Range("B3:C2000")
        If Not Intersect(target, oRange) Is Nothing Then
            frmCalendar.Show
        End If
    
    End Sub
    

    Userform code

    Private Sub UserForm_Initialize()
    
        With Me
            .StartUpPosition = 0
            .Top = ActiveCell.Top + ActiveCell.Height + .Height
            .Left = ActiveCell.Offset(0, 1).Left
        End With
    
    End Sub
    

提交回复
热议问题