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
Creating Strikethrough Macro in Excel
提问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 Basic
toolbar and activate theControl Toolbox
button. Another box/toolbar should appear with different control options. - Select the
Button
option and place it in the desired location.
- Open up the
- Excel 2007 and later:
- Click on the
Developer
tab/ribbon. - Select
Insert
and selectButton
and place it in the desired location.
- Click on the
- *The steps below apply to all versions from this point forward.
- Right-click on your new button and select
Properties
to give your button a name/caption.
- Right-click on your new button and select
- Right-click again and select
View Code
. - In the
ButtonName_Click()
sub, add thestrikeOut()
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:
要回答问题的第二部分,很难说什么是“最好的”,但这里有一些链接可能会有所帮助: