1

enter image description here

I'm only getting this error when I try the macro across multiple boxes that do not all have the same selection style to start.

Sub Remove_Decimals()
'
' Remove_Decimals Macro
'
' Keyboard Shortcut: Ctrl+Shift+V
'
    If Selection.Style = "Percent" Then
    Else:
    Selection.NumberFormat = "_($* #,##0.0_);_($* (#,##0.0);_($* ""-""??_);_(@_)"
    Selection.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"
    End If
End Sub
4
  • Can you share a sample screenshot of the data that causes the error? Commented Jun 4, 2019 at 18:55
  • yup I just added it to the question. Selecting all those cells before applying the macro gives me an error. (wouldn't let me post the picture as a screenshot not enough reputation points) Commented Jun 4, 2019 at 19:01
  • 1
    Can't reproduce the error the screenshot and code you provided. Add Option Explicit to the top of the module and rerun. Does it flag a possible typo? Commented Jun 4, 2019 at 19:06
  • no typos are being flagged. The marco works sometimes but not always and im not sure what exactly the parameters are that cause the error. Commented Jun 4, 2019 at 19:09

2 Answers 2

1

Working off Selection assumes that there is a selection. If nothing is selected, Selection is Nothing, and accessing its members throws error 91 like you're getting.

Working off Selection.Style requires that the current selection has a Style property, but Selection can be a Shape or a Chart, not necessarily a Range of cells.

The only way to make sure of that is to validate the data type of the Selection - add this guard clause to the beginning of your code, should fix it:

If Not TypeOf Selection Is Excel.Range Then Exit Sub

Now that we know we're looking at a Range, it's safe to cast it to that interface and do away with the late-bound code that's only resolved at run-time (and risk error 438 for any typo... which Option Explicit won't be able to pick up):

Dim selectedCells As Range
Set selectedCells = Selection

Now, I know we usually prefer positive conditionals, but making an empty conditional block with the whole logic in the Else part makes no sense. Invert that condition and remove the empty block - and if you're going to write a value only to overwrite it the next statement, skip straight to the second assignment:

If selectedCells.Style <> "Percent" Then
    selectedCells.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"
End If

That said, since the macro is named "remove decimals" it might be a good idea to first validate that we're looking at a numeric value - but we can't do this for the selectedCells range, we need to validate each individual cell separately (because selectedCells.Value is a 2D variant array given any multiple-cells range).

So, recap:

Public Sub RemoveDecimals()
    'bail out if no cells are selected:
    If Not TypeOf Selection Is Excel.Range Then Exit Sub 

    Dim cell As Range
    For Each cell In selectedCells
        'ignore non-numeric and percentage cells:
        If IsNumeric(cell.Value) And cell.Style <> "Percent" Then
            cell.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"
        End If
    Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Try this (just reusing your code):

Sub Remove_Decimals()
'
' Remove_Decimals Macro
'
' Keyboard Shortcut: Ctrl+Shift+V
'
Dim rngCell As Range
    For Each rngCell In Selection

        If rngCell.Style <> "Percent" Then
            rngCell.NumberFormat = "_($* #,##0.0_);_($* (#,##0.0);_($* ""-""??_);_(@_)"
            rngCell.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"
        End If
    Next rngCell
End Sub

Not sure why you are applying the formatting twice though?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.