Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Strikethrough Macro in Excel

Tags:

excel

vba

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.

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.

Sub strikeOut()

Selection.Font.Strikethrough = True

End Sub

I also need help with the command button.

Thank you.

like image 623
STANGMMX Avatar asked Aug 06 '12 14:08

STANGMMX


1 Answers

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 the Control Toolbox button. Another box/toolbar should appear with different control options.
    • Select the Button option and place it in the desired location.
  • Excel 2007 and later:
    • Click on the Developer tab/ribbon.
    • Select Insert and select Button and 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 Properties to 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

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

like image 99
Gaffi Avatar answered Sep 24 '22 10:09

Gaffi