excel宏 拆分工作表_使用工作表按钮运行Excel宏

excel宏 拆分工作表

In my workbooks, I sometimes add buttons to run macros. Usually, they're for navigation to the next or previous sheet, or to run a macro that's specific to the contents of the worksheet.

在工作簿中,有时会添加按钮以运行宏。 通常,它们用于导航到下一个或上一个工作表,或运行特定于工作表内容的宏。

In this example, I've got two macros in the workbook – one that takes you to the next sheet in the workbook, and one that takes you to the previous sheet.

在此示例中,我在工作簿中有两个宏–一个将您带到工作簿中的下一个工作表,另一个将您带到上一个工作表。

So, if you're on the second sheet, you can click the Next button to go to the third sheet. Or, click the Back button to go to the first sheet.

因此,如果您在第二张纸上,则可以单击“下一步”按钮转到第三张纸。 或者,单击“后退”按钮转到第一页。

添加一个按钮 (Add a Button)

    • Note: If the Developer tab isn't visible, click the Office Button, then click Excel Options. Click the Popular category, then add a check mark to Show Developer tab in the Ribbon.

      注意:如果“开发人员”选项卡不可见,请单击“ Office按钮”,然后单击“ Excel选项”。 单击“流行”类别,然后在功能区中的“显示开发人员”选项卡上添加一个复选标记。
  • In the Controls group, click Insert, to see the controls from the Form toolbar and the Control Toolbox.

    在“控件”组中,单击“插入”,以从“表单”工具栏和“控件工具箱”中查看控件。
  • In the Form Controls section, click Button.

    在“表单控件”部分中,单击“按钮”。
FormButton01
  • Click on the worksheet to add a button, or drag on the worksheet to add a button of a specific size.

    单击工作表上以添加按钮,或在工作表上拖动以添加特定大小的按钮。
  • In the Assign Macro dialog box that opens automatically, click on the name of a macro to select it, then click OK

    在自动打开的“分配宏”对话框中,单击一个宏的名称以将其选中,然后单击“确定”。
AssignMacro01

格式化按钮 (Format the Button)

While the button is still selected, you can change its caption and add some formatting.

当按钮仍处于选中状态时,您可以更改其标题并添加一些格式。

  • To change the button's caption, select the existing text, and type a caption to replace it. The border around the button will have diagonal lines when you're editing the text

    要更改按钮的标题,请选择现有文本,然后键入标题以替换它。 编辑文字时,按钮周围的边框将带有对角线
ButtonBack01
  • Click on the border of the button, to select it. The outline should change to a dotted pattern, as you can see in the next screenshot.

    单击按钮的边框以将其选中。 轮廓应更改为虚线,如您在下一个屏幕截图中所见。
ButtonBack02
  • On the Ribbon's Home tab, use the font formatting buttons, such as Bold and Font Size, to change the appearance of the button.

    在“功能区”的“主页”选项卡上,使用字体格式化按钮(如“粗体”和“字体大小”)来更改按钮的外观。

使用按钮 (Use the Buttons)

After you've added the Back button, repeat the steps to create a Next button. Then, copy the two buttons to any worksheets or chart sheets in the workbook.

添加“后退”按钮后,重复上述步骤以创建“下一步”按钮。 然后,将两个按钮复制到工作簿中的任何工作表或图表表。

To move through the sheets, click either the Back or Next button on any sheet.

要遍历工作表,请在任何工作表上单击“上一步”或“下一步”按钮。

ButtonBack03

导航代码 (The Navigation Code)

Here's the code that I used to make the buttons select the next or previous sheet.

这是我用来使按钮选择下一页或上一页的代码。

Sub GoSheetNext()
Dim wb As Workbook
Dim lSheet As Long
Set wb = ActiveWorkbook
lSheet = ActiveSheet.Index
With wb
  If lSheet = .Sheets.Count Then
    .Sheets(1).Select
  Else
    .Sheets(lSheet + 1).Select
  End If
End With
End Sub
'=================================
Sub GoSheetBack()
Dim wb As Workbook
Dim lSheet As Long
Set wb = ActiveWorkbook
lSheet = ActiveSheet.Index
With wb
  If lSheet = 1 Then
    .Sheets(.Sheets.Count).Select
  Else
    .Sheets(lSheet - 1).Select
  End If
End With
End Sub

翻译自: https://contexturesblog.com/archives/2009/07/08/run-an-excel-macro-with-a-worksheet-button/

excel宏 拆分工作表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤使用VBA按某列汇总拆分工作: 1. 打开你想要拆分工作。 2. 在工作上方的菜单栏中选择“开发人员”选项卡。如果你没有看到该选项卡,则需要启用它。可以通过下列步骤:文件->选项->自定义功能区->勾选“开发人员”选项卡。 3. 点击“Visual Basic”按钮,打开Visual Basic编辑器。 4. 在编辑器中,依次展开“项目-VBAProject(VBA项目)”、“Microsoft Excel 对象”、“Sheet1(Sheet1)”,双击“Sheet1(Sheet1)”。 5. 在工作的代码窗口中输入以下代码: ``` Sub SplitData() Dim lastRow As Long Dim currentRow As Long Dim i As Long Dim currentName As String Dim ws As Worksheet Dim newWs As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row currentRow = 2 For i = 2 To lastRow If ws.Range("A" & i).Value <> currentName Then currentName = ws.Range("A" & i).Value Set newWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) newWs.Name = currentName ws.Range("A1:D1").Copy newWs.Range("A1") currentRow = 2 End If ws.Range("A" & i & ":D" & i).Copy newWs.Range("A" & currentRow) currentRow = currentRow + 1 Next i End Sub ``` 6. 保存并关闭Visual Basic编辑器。 7. 回到工作并点击“开发人员”选项卡上的“按钮。 8. 在弹出的对话框中选择“SplitData”,并点击“运行”。 这将按列A中的值拆分工作,并将每个唯一值的数据放入一个新的工作中。每个新工作的名称将是该值。请注意,此代码假设您的数据以列A开始,并且您的数据没有标题行。如果您的数据的列不同,请在代码中进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值