vba 在 Excel 中创建删除线宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11829914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:11:04  来源:igfitidea点击:

Creating Strikethrough Macro in Excel

excelvbaexcel-vba

提问by STANGMMX

I'm a novice to VBA and I'm trying to make a simple macro where one can highlight a set of cells, click a button, and strikethrough the selected sells. After, you can select the cell again, click the same button, and remove the strikethrough.

我是 VBA 的新手,我正在尝试制作一个简单的宏,可以突出显示一组单元格,单击一个按钮,然后删除选定的销售。之后,您可以再次选择单元格,单击相同的按钮,并删除删除线。

I have been looking for decent documentation but, have yet to find anything.

我一直在寻找像样的文档,但是还没有找到任何东西。

Here's some code.

这是一些代码。

Also, I would love to know where the best documentation is on VBA.

另外,我很想知道 VBA 上最好的文档在哪里。

Sub strikeOut()

Selection.Font.Strikethrough = True

End Sub

I also need help with the command button.

我还需要有关命令按钮的帮助。

Thank you.

谢谢你。

回答by Gaffi

It looks like you're on the right path. Based on your code, I'm assuming you already have a command button created. If so try this:

看起来你走在正确的道路上。根据您的代码,我假设您已经创建了一个命令按钮。如果是这样试试这个:

Sub strikeOut()
    With Selection.Font
        .Strikethrough = Not .Strikethrough
    End With
End Sub

To create a command button:

创建命令按钮:

  • Excel 2003 and earlier:
    • Open up the Visual Basictoolbar and activate the Control Toolboxbutton. Another box/toolbar should appear with different control options.
    • Select the Buttonoption and place it in the desired location.
  • Excel 2007 and later:
    • Click on the Developertab/ribbon.
    • Select Insertand select Buttonand place it in the desired location.
  • *The steps below apply to all versions from this point forward.
    • Right-click on your new button and select Propertiesto give your button a name/caption.
  • Right-click again and select View Code.
  • In the ButtonName_Click()sub, add the strikeOut()call using either:
    • Call strikeOut()
  • or simply
    • strikeOut
  • Excel 2003 及更早版本
    • 打开Visual Basic工具栏并激活Control Toolbox按钮。另一个框/工具栏应该显示不同的控制选项。
    • 选择该Button选项并将其放置在所需位置。
  • Excel 2007 及更高版本
    • 单击Developer选项卡/功能区。
    • 选择Insert并选择Button并将其放置在所需位置。
  • *以下步骤适用于从现在开始的所有版本。
    • 右键单击您的新按钮并选择Properties为您的按钮命名/标题。
  • 再次右键单击并选择View Code
  • ButtonName_Click()子中,strikeOut()使用以下任一方法添加调用:
    • Call strikeOut()
  • 或者干脆
    • strikeOut

To answer the second part of your question, it's hard to say what is the 'best' but here are some links that may help:

要回答问题的第二部分,很难说什么是“最好的”,但这里有一些链接可能会有所帮助:

Chip Pearson's site
MSDN
OZgrid

Chip Pearson 的网站
MSDN
OZgrid