我正在尝试在打开工作簿时在工作表上放置按钮。我已经完成了那么多,但似乎无法让按钮以可靠的方式放置。我的意思是我试图将按钮放在活动工作表的顶部和右侧,定义为" Landing。"但是,当我尝试设置参数时,空格似乎不一致。我的代码是:
Dim Landing As Worksheet
Dim ImportBankTransButton As Button
Dim FinancialStatementsButton As Button
Dim LogInButton As Button
Dim NewBankAcctButton As Button
Dim ChangeBankAcctButton As Button
Dim NewExpenseButton As Button
Dim ChangeExpenseButton As Button
Dim ChangeLogInButton As Button
Dim PullUpLogInButton As Button
Dim PastStatementsButton As Button
Dim AcctMaintButton As Button
Dim trial As Button
Set Landing = Activeworkbook.Sheets("Welcome")
ChDir ("temp")
Landing.Buttons.Delete 'deletes all current buttons to reset the page
'Adding buttons back to page
'Buttons.add(left parameter, top parameter, width parameter, height parameter)
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 640, ActiveWindow.VisibleRange.Height - 425, 120, 45).Select
Set ImportBankTransButton = Selection
With ImportBankTransButton
.Text = "Import Banking" & vbNewLine & "Transactions"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 12
.OnAction = "ImportBankingTransactions"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 455, ActiveWindow.VisibleRange.Height - 425, 120, 45).Select
Set FinancialStatementsButton = Selection
With FinancialStatementsButton
.Text = "Generate Financial" & vbNewLine & "Statements"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 12
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 290, ActiveWindow.VisibleRange.Height - 425, 120, 45).Select
Set LogInButton = Selection
With LogInButton
.Text = "New Log-In" & vbNewLine & "Information"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 12
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 25, 80, 35).Select
Set NewBankAcctButton = Selection
With NewBankAcctButton
.Text = "Add New Bank" & vbNewLine & "Account"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "CreateNewAccountPreForm"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 70, 80, 35).Select
Set ChangeBankAcctButton = Selection
With ChangeBankAcctButton
.Text = "Change Bank" & vbNewLine & "Account Info"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 115, 80, 35).Select
Set NewExpenseButton = Selection
With NewExpenseButton
.Text = "Create New Expense" & vbNewLine & "(Sub-Account)"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 160, 80, 35).Select
Set ChangeExpenseButton = Selection
With ChangeExpenseButton
.Text = "Change Expense" & vbNewLine & "(Sub-Account)"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 205, 80, 35).Select
Set ChangeLogInButton = Selection
With ChangeLogInButton
.Text = "Change Existing" & vbNewLine & "Log-In Info"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 250, 80, 35).Select
Set PullUpLogInButton = Selection
With PullUpLogInButton
.Text = "Go To Log-In" & vbNewLine & "Information"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "ShowLogIn"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 295, 80, 35).Select
Set PastStatementsButton = Selection
With PastStatementsButton
.Text = "Open Past" & vbNewLine & "Statements"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "UnderConstruction"
End With
Landing.Buttons.Add(ActiveWindow.VisibleRange.Width - 125, 340, 80, 35).Select
Set AcctMaintButton = Selection
With AcctMaintButton
.Text = "MyLife File" & vbNewLine & "Maintenance"
.Font.Name = "Times New Roman"
.Font.Bold = False
.Font.Size = 10
.OnAction = "AccountMaintenance"
End With
一切都显示出来,按钮功能齐全。按钮4-11显示在按钮的右边缘和窗口的右边缘之间有一个小的(~1.4cm)空间,这等于" 45"屏幕宽度之差小于125(activewindow.visiblerange.width - 125)和按钮宽度(80)。但是,当我尝试将下一个按钮设置在另一个45(上面的按钮1 - 3)内部(刚刚放置的按钮的左侧)时,它会留下一个更大的间隙(~2.5cm)?我认为使用添加45和新按钮的宽度到最右边的按钮的左边缘将产生相同的间隙(activewindow.visible range.width - (125 + 45 + 120))。我缺少的是单位不一致吗?
此外,作为VBA的新手,欢迎任何提高代码效率的意见/建议!
谢谢大家。
编辑:谢谢大家称重!为了回应许多评论,我试图使这张表格看起来相同,具体取决于屏幕的大小,因此使用动态放置。它将触发"打开"并且"调整大小。"此外,除了激活页面和退出子目录外,没有任何代码。