2025年4月30日 星期三

VBA:控制表單的位置(APPLICATION應用)

 沒寫控制,表單顯示時都出現在螢幕正中央,實在很煩,小編就改了一下,當作磨練。

VBA:


使用,做一個表單,放進去後,調整LEFT跟TOP,勘你想要放那 XD。


Private Sub UserForm_Activate()

    Call PositionUserFormRelativeToExcel

End Sub


Sub PositionUserFormRelativeToExcel()

    Dim excelLeft As Single

    Dim excelTop As Single

    Dim excelWidth As Single

    Dim excelHeight As Single


    ' 獲取 Excel 視窗的位置和大小

    excelLeft = Application.Left

    excelTop = Application.Top

    excelWidth = Application.Width

    excelHeight = Application.Height

    ' 設定 UserForm1 的位置,使其位於 Excel 視窗的右側

    With Me

        .StartUpPosition = 0 ' 設定為手動位置

        .Left = excelLeft + excelWidth - .Width - 10 ' 設定左邊位置,使其位於 Excel 視窗的右側

        .Top = excelTop + (excelHeight - .Height) / 2 ' 設定頂部位置,使其在垂直方向居中

                    If Me.Visible = True Then             

            Else                

               .Show 0

            End If        

    End With

End Sub





沒有留言:

張貼留言

指數變化(2026.01.23) 開始透過AI做整理

  指數變化(2026.01.23) 開始透過AI做整理 上周焦點: 1/13消費者物價相關 美國消費者物價指數(CPI)年增率:2.7%(符合預期),月增率:0.3%。 美國核心CPI年增率:2.6%。 1/14企業庫存、房市、能源及房貸相關 美國企業庫存月增率:0.3%(10...