受欢迎的博客标签

如何始终在Excel中的工作表上始终浮动命令按钮?

Published

在某些情况下,无论如何上下滚动工作表,您都可能需要一个浮动的命令按钮(ActiveX控件)才能与工作表一起移动。

s 1:

1.在包含要使其浮动的ActiveX控件命令按钮的工作表中,右键单击工作表选项卡,然后单击“确定”。 查看代码 从上下文菜单。

2.在弹出 Microsoft Visual Basic应用程序 窗口,将下面的VBA代码复制并粘贴到“代码”窗口中。

VBA代码:使命令按钮始终浮在工作表中

CommandButton1是您希望始终保持可见的按钮名称

此VBA仅适用于ActiveX受控命令按钮

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
        On Error GoTo 0
        With Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn)
            CommandButton1.Top = .Top + 100
            CommandButton1.Left = .Left + 300
        End With
End Sub

s2:

在较早的日子(Excel 2003及更早版本)中,有工具栏,您可以“浮动”那些工具栏,并将其放置在工作表上的任何位置。 您还可以创建自定义工具栏,并向其中添加一些命令和宏。 它们也可以在工作表中移动。

In the newer versions of Excel, those floating toolbars aren't available, but you can create something similar, based on a UserForm. Build your own, or download my example, and customize it.

在较新版本的Excel中,这些浮动工具栏不可用。

s3:在工作表顶部的冻结窗格中添加按钮。

In Excel 2007 and later, to make it easy for people to run macros, or move around in a workbook, you can add buttons in a frozen pane, at the top of the worksheet. Or, create a custom tab on the Ribbon, and put the commands there.

在Excel 2007和更高版本中,为了使人们能够轻松运行宏或在工作簿中移动,可以在工作表顶部的冻结窗格中添加按钮。

s4:创建一个用户表单 (Create a UserForm)

To create a floating form, you can insert a UserForm in the workbook, and have it open automatically when the file opens. There are instructions for creating a UserForm on my website, if you haven't built one before.

若要创建浮动表单,可以在工作簿中插入一个用户窗体,并在文件打开时自动将其打开。 如果您以前没有构建过UserForm ,则有在我的网站上创建UserForm的说明 。

To create a floating form:

创建浮动表格:

Insert a UserForm in the workbook 

在工作簿中插入一个用户窗体
Change the form’s ShowModal setting to False, so you’ll be able to use the worksheet while the form is open. 

将窗体的ShowModal设置更改为False,以便在窗体打开时可以使用工作表。
Then, add a few command buttons, and set those to run navigation macros, or other types of macros. 

然后,添加一些命令按钮,并将其设置为运行导航宏或其他类型的宏。
Here is a screen shot of the small and simple form that I built.

这是我构建的小型简单表单的屏幕截图。

(Open the Form Automatically)
In the workbook module, use the Workbook_Open event to show the UserForm, so it appears as soon as the workbook opens.

在工作簿模块中,使用Workbook_Open事件显示UserForm,以便在工作簿打开后立即显示。

Here is the code that I added to the ThisWorkbook module in my example – the UserForm is named frmButtons.

这是我在示例中添加到ThisWorkbook模块中的代码– UserForm名为frmButtons。

Private Sub Workbook_Open()
  frmButtons.Show
End Sub

After the form opens automatically, it can be positioned anywhere on the screen, for easy access.

表单自动打开后,可以将其放置在屏幕上的任何位置,以便于访问。

 

https://blog.csdn.net/culiao2169/article/details/107988693