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
Option Explicitto the top of the module and rerun. Does it flag a possible typo?