Macros

Ideal Percent Style

VBA: Ideal Percent Style

Sub Ideal_Percent()
'This macro changes the Percent style to that preferred for the presentation of your excel workings; obtained from bgrconsulting.co.uk/macros where help and support can be found.
Dim ShConstant As Range, ShFormulas As Range, Rng As Range

Begin:
dps = InputBox("How many decimal places would you like the Percent style to contain?" _
& vbNewLine & vbNewLine & "Note: Please input in '0.00' format.", "Decimal Places?")
If dps = "" Then Exit Sub

On Error GoTo -1
On Error GoTo Leave
x = Int(dps)

Idnt = MsgBox("Would you like force the alignment to the right of the cell?", vbYesNo, "Force Right?")
Rd = MsgBox("Would you like negative figures to appear in red text?", vbYesNo, "Red Negatives?")

If Idnt = vbYes Then
    If Rd = vbYes Then
        ActiveWorkbook.Styles("Percent").NumberFormat = "_-* #,##" & dps & "%_);[RED]_-* (#,##" & dps & "%);_-* ""-""??%_);_-@_)"
    Else
        ActiveWorkbook.Styles("Percent").NumberFormat = "_-* #,##" & dps & "%_);_-* (#,##" & dps & "%);_-* ""-""??%_);_-@_)"
    End If
Else
    If Rd = vbYes Then
        ActiveWorkbook.Styles("Percent").NumberFormat = "_-#,##" & dps & "%_);[RED]_-(#,##" & dps & "%);_-""-""??%_);_-@_)"
    Else
        ActiveWorkbook.Styles("Percent").NumberFormat = "_-#,##" & dps & "%_);_-(#,##" & dps & "%);_-""-""??%_);_-@_)"
    End If
End If

Reformat = MsgBox("Would you like to reformat all the cells in this workbook presenting a percentage to align with this new style?", vbQuestion + vbYesNo, "Reformat Cells:")
If Reformat = vbNo Then Exit Sub

Call TurnStuffOff
Set Rtn = ActiveCell
For Each Sh In ActiveWorkbook.Sheets
    On Error Resume Next
    Set ShConstant = Sh.UsedRange.SpecialCells(xlCellTypeConstants, 1)
    Set ShFormulas = Sh.UsedRange.SpecialCells(xlCellTypeFormulas, 1)
    If ShConstant Is Nothing And ShFormulas Is Nothing Then GoTo NxtSh
    If ShConstant Is Nothing Then Set Rng = ShFormulas
    If ShFormulas Is Nothing Then Set Rng = ShConstant
    'On Error GoTo -1: On Error GoTo NxtSh
    If Rng Is Nothing Then Set Rng = Union(ShConstant, ShFormulas)
    For Each Cell In Rng
        If Cell.NumberFormat Like "*%*" Then Cell.Style = "Percent"
    Next Cell
    Set ShConstant = Nothing: Set ShFormulas = Nothing: Set Rng = Nothing
NxtSh:
Next Sh
Rtn.Select
Call TurnStuffOn
Exit Sub

Leave:
    Retry = MsgBox("The style must be input in '0.00' format." & vbNewLine & vbNewLine & "Would you like to try again?", vbCritical + vbYesNo, "Retry?")
    If Retry = vbYes Then GoTo Begin
End Sub