# Increase/Decrease Decimals

#### BuddieB

##### Board Regular
I know that there is a button that already does this, but is there a way to assign this command to a keystroke?

Or better yet, does anyone have some VBA that mimics the same action of the increase/decrease decimal buttons?

I'm just not a mouse guy and I wanted to assign the command to a couple key strokes.

### Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

#### Joe4

Here is a macro that will increase the decimal position by one place. You can then attach the macro to a keystroke.

Code:
``````Sub IncreaseDecimal()

Dim myFormat As String
myFormat = Selection.NumberFormat
If (Len(myFormat) > 1) And (Mid(myFormat, 2, 1) = ".") Then
myFormat = myFormat & "0"
Else
myFormat = "0.0"
End If
Selection.NumberFormat = myFormat

End Sub``````

From this, see if you can also build one to reduce the decimal places (hint: think Left(myFormat,Len(myFormat)-1):

#### BuddieB

##### Board Regular
I actually took a couple stabs at it and was able to come up with one that works for a typical number, but it doesn't work for cases when I am dealing with % or \$ formatted cells. The buttons DO handle this, any chance that someone can come up with some code that will match the behavior of the buttons for all cases?

Here's what I have already for normal decimal values:

Code:
``````Sub DecimalIncrease()
'
' DecimalIncrease Macro
' Increases the number of digits after the decimal

Curformat = ActiveCell.NumberFormat

If Curformat = "General" Then Selection.NumberFormat = "0"
If Curformat = "0" Then Selection.NumberFormat = "0.0"

If Mid(Curformat, 2, 1) = "." Then
Selection.NumberFormat = Curformat & "0"
End If

End Sub``````

#### Joe4

As you can see, this can get quite complicated, as there are many different formats to consider (which I failed to do in my first post).

Rather then re-create the wheel, if you don't like the mouse, then how about menu shortcuts (O, E, Tab, etc.) to go into the Format menu?

#### BuddieB

##### Board Regular

oh I know all about the ALT + letters to get to the menus. I was just hoping that there might be an easy way to link an existing XL button to a keystroke.

#### Joe4

I am sure it can be done, but I don't know about the easy part!

#### harvey_v

##### New Member

I would also be very interested in an answer to this question. I saw in another thread that there is a way to enable and disable the command bar buttons on the toolbars. In the same vein, is there a way to execute them using a VBA macro?

##### New Member
Easy.

to increase decimals:

sub Increase_Decimal()
Application.CommandBars.FindControl(ID:=398).Execute
end sub

To decrease decimals:

Sub Decrease_Decimal()
Application.CommandBars.FindControl(ID:=399).Execute
End Sub

#### level3ninja

##### New Member
Easy.

to increase decimals:

sub Increase_Decimal()
Application.CommandBars.FindControl(ID:=398).Execute
end sub

To decrease decimals:

Sub Decrease_Decimal()
Application.CommandBars.FindControl(ID:=399).Execute
End Sub

LIFE SAVER!!!!

#### BobSundquist

##### New Member
Here is an improved version that works with a field with "General" format initially. This used to work with Excel 2003; Microsoft broke it sometime between then and Excel 2013:

Sub IncreaseDecimal()
If InStr(Selection.NumberFormat, "General") Then
If InStr(ActiveCell.Text, ".") Then
n = Len(ActiveCell.Text) - InStr(ActiveCell.Text, ".")
Else
n = 0
End If
n = n + 1
FormatString = "0."
While n > 0
FormatString = FormatString & "0"
n = n - 1
Wend
Selection.NumberFormat = FormatString
Else
Application.CommandBars("Formatting").Controls("Increase Decimal").Execute
End If
End Sub

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,168,068
Messages
5,857,146
Members
431,858
Latest member
mucut

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back