Using visual basic to change the formula in a cell

Angelo Dundee

Board Regular
Joined
Nov 18, 2002
Messages
167
This is a very simple question :-
How do I use visual basic to change the formula in a cell.
For example I want 2 option buttons that change the contents of a cell from mm to inches.
Therefore I require 2 routines that are triggered by option buttons.
These routines will do the following :-
When the mm button is clicked I want Cell A1 :-
format = 1 decimal place
contents = C1
When the inches is clicked I want Cell A1 :-
format = 2 decimal place
contents = C1/25.4


Private Sub OptionButton2_Click()
'American - inches click
Range("A1").Select
Selection.NumberFormat = "0.00"

End Sub

What is the line missing above, which will change the formula in the cell, to C1/25.4 ?

Thanks, Angelo Dundee

:rolleyes: :pray: :oops: o_O

Ali, shall return !!!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

R_Stephens

Board Regular
Joined
Dec 23, 2004
Messages
143
Hi there,

To put a formula in your selected cell add the following line

Code:
Selection.Value = "=C1/25.4"

Basically type the formula that you would put into the excel cell within "" marks (including the = at the beginning)

Hope this helps

Richard
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Hi,

try
Code:
Private Sub OptionButton1_Click()
With Me
        If .OptionButton1 = True Then
                .OptionButton2 = False
                With .Range("a1")
                    .NumberFormat = "0.0"
                    .Value = .Range("c1").Value / 25.4
                End With
        End If
End With
End Sub

Private Sub OptionButton2_Click()
With Me
        If .OptionButton2 = True Then
                .OptionButton1 = False
                With .Range("a1")
                    .NumberFormat = "0.00"
                    .Value = .Range("c1").Value
                End With
        End If
End With
End Sub

hope this helps

jindon
 

Forum statistics

Threads
1,147,507
Messages
5,741,566
Members
423,667
Latest member
Kai_357

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Top