VBA Code Required For Conversion

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,502
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I need a VBA code for the following, if possible.


CELL A1 90.00 ------> centimeters
CELL A2 35.43 -------> Inches
CELL A3 00.90 --------> Meter
CELL A4 02.95 ---------> Feet


What i require is if i change the value in cell A1 then all the rest 3 cells should show the converted results based on that manually entered cell. And If i change the value in cell A2 then all the rest 3 cells should show the converted results based on that manually entered cell and so on. hope anyone can provide a solution.

Thanks,

Humayun
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
you cannot have A2 dependent on A1 AND A1 dependent on A2

what you can do is start with all cells blank, put a value in one of them and use a simple macro to populate the otherr 3 - is this acceptable
 
Upvote 0
You could use the CONVERT function

=CONVERT(A1,"cm","in")
=CONVERT(A1,"cm","m")
=CONVERT(A1,"cm","ft")

Edit: never mind, you can't use Excel functions to set up a circular reference situation like this.

You'd have to use VBA event code in the sheet's code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Cells.Count = 1 And .Column = 1 And .Row < 5 Then
            Application.EnableEvents = False
            Select Case .Row
                Case 1
                    Me.Cells(2, 1) = WorksheetFunction.Convert(.Value, "cm", "in")
                    Me.Cells(3, 1) = WorksheetFunction.Convert(.Value, "cm", "m")
                    Me.Cells(4, 1) = WorksheetFunction.Convert(.Value, "cm", "ft")
                Case 2
                    Me.Cells(1, 1) = WorksheetFunction.Convert(.Value, "in", "cm")
                    Me.Cells(3, 1) = WorksheetFunction.Convert(.Value, "in", "m")
                    Me.Cells(4, 1) = WorksheetFunction.Convert(.Value, "in", "ft")
                Case 3
                    Me.Cells(1, 1) = WorksheetFunction.Convert(.Value, "m", "cm")
                    Me.Cells(2, 1) = WorksheetFunction.Convert(.Value, "m", "in")
                    Me.Cells(4, 1) = WorksheetFunction.Convert(.Value, "m", "ft")
                Case 4
                    Me.Cells(2, 1) = WorksheetFunction.Convert(.Value, "ft", "cm")
                    Me.Cells(3, 1) = WorksheetFunction.Convert(.Value, "ft", "in")
                    Me.Cells(4, 1) = WorksheetFunction.Convert(.Value, "ft", "m")
            End Select
        End If
    End With
    Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
thanks mikerickson

i am already working on the formula you provided... thanks once again. but everytime i dont have the data in centimeters. sometimes i have it in inches, sometime in meters and so on. so, if i will enter the formula in cells A2 to A3 then i would not be able to enter the data in it.
 
Upvote 0
Here's a corrected version that does the conversion directly and has the cell references corrected.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const cm2in As Double = 0.393700787401575
    With Target
        If .Cells.Count = 1 And .Column = 1 And .Row < 5 Then
            Application.EnableEvents = False
            Select Case .Row
                Case 1
                    Me.Cells(2, 1) = .Value * cm2in
                    Me.Cells(3, 1) = .Value / 100
                    Me.Cells(4, 1) = .Value * cm2in / 12
                Case 2
                    Me.Cells(1, 1) = .Value / cm2in
                    Me.Cells(3, 1) = .Value / cm2in / 100
                    Me.Cells(4, 1) = .Value / 12
                Case 3
                    Me.Cells(1, 1) = .Value * 100
                    Me.Cells(2, 1) = .Value * 100 * cm2in
                    Me.Cells(4, 1) = .Value * 100 * cm2in / 12
                Case 4
                    Me.Cells(1, 1) = .Value * 12 / cm2in
                    Me.Cells(2, 1) = .Value * 12
                    Me.Cells(3, 1) = .Value * 12 / cm2in / 100
            End Select
        End If
    End With
    Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
dear mike, well i was not sure where to enter the code.... i mean in the sheet code or module....

well i entered in both ..... but nothing is happening... i mean i entered 100 in cell A1 but cell A2 to A4 remains blank

any Idea ???
 
Upvote 0
i also tried the second code which you provided.. problem remains the same... nothing is happening..
 
Upvote 0
sorry, i made some mistake.... its working just perfect

thanks
 
Upvote 0
dear mike can you just ammend the coded a bit... i need feet conversion in the third cell and Meter conversion in the fourth cell... at the moment its vice versa
 
Upvote 0
or this is another way

in
89.9922cm
35.43in35.43< < < < start valueCELL A1 90.00 ------> centimeters
0.899922metresCELL A2 35.43 -------> Inches
2.9525feetCELL A3 00.90 --------> Meter
CELL A4 02.95 ---------> Feet
col J
row 16incmmetresfeet
in 2.540.02540.083333
cm0.393701 0.010.032808
metres39.37008100 3.28084
feet1230.480.3048
when you enter a number in C2:C5
C1 automatically detects the units to its left
the formula in col A uses the conversion table
to calculate the required numbers
formula in A2
=IF(C2="",LARGE($C$2:$C$5,1)*OFFSET($J$16,MATCH($C$1,$J$17:$J$20,0),MATCH(B2,$K$16:$N$16,0)),C2)

<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,824
Members
449,470
Latest member
Subhash Chand

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