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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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
Back
Top