Calculate % and value vice versa

Jobsan

New Member
Joined
Nov 30, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello guys,

Sorry to bother you I was wondering if there VBA that can calculate value just by percentage and percentage base on value. So basically being able to to calculate vice versa. If I overwrite the value of one the other will update. Ive attached a screenshot. I would like to be able to do that for column E and Column H on the whole workbook. Thank you!!

1733027325734.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Right click on tab with sheet name, Use "View Code". In VBA Editor window which shall show up, paste the code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, curr_row As Long
If Not Intersect(Target, Range("E5:E1000,H5:H1000")) Is Nothing Then
  For Each rng In Intersect(Target, Range("E5:E1000,H5:H1000"))
    curr_row = rng.Row
    If IsNumeric(Cells(curr_row, "C").Value) And IsNumeric(rng.Value) And Cells(curr_row, "C").Value > 0 Then
      Application.EnableEvents = False
        If rng.Column = 5 Then 'change in E
          Cells(curr_row, "H") = rng.Value / Cells(curr_row, "C").Value
        Else 'it is H column
          Cells(curr_row, "E") = rng.Value * Cells(curr_row, "C").Value
        End If
      Application.EnableEvents = True
    End If
  Next rng
End If
End Sub

close VBA editor window, Make sure that column H is formatted as percents, and file is saved in macro-enabled format (like xlsm) and test it.

Book1
ABCDEFGH
1
2
3
4
5
650%
712001200100%
8a100
9150090060%
102000100050%
Sheet1
 
Upvote 1
Thank you so much!!! ive been scratching my head all this time. one more thing, lets say i have thew same information but 10 more taps. What can i do do make it do the same in all the following workbook

1733086316017.png
 
Upvote 0
And all sheets has the same layout?

Then you have 2 main options:
First:
simple, but not elegant - copy the code to every sheet which shall work that way.

Second:
View the code again, delete it, dont close VBA editor window..
Doubleclick on Thisworkbook element which shall be visible in a window on the left. And in a code window on the right paste such code.
In a code include a list of all names of sheets, where code shall not be executed. If it is only SOV sheet, then leave it as is. The list shall have at least 2 elements to work properly, and it does not hurt if you don't have sheet named Name_of_any_other_Sheet_not_working_that_way in your workbook

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim rng As Range, curr_row As Long, lista As Variant
lista = Split("SOV,Name_of_any_other_Sheet_not_working_that_way", ",") ' make a comma separated list of sheets excluded from such procedure
If Not IsError(Application.Match(Sh.Name, lista, 0)) Then Exit Sub 'do nothing/exit if current sheet is on that list
'here the same code as previously used
If Not Intersect(Target, Range("E5:E1000,H5:H1000")) Is Nothing Then
  For Each rng In Intersect(Target, Range("E5:E1000,H5:H1000"))
    curr_row = rng.Row
    If IsNumeric(Cells(curr_row, "C").Value) And IsNumeric(rng.Value) And Cells(curr_row, "C").Value > 0 Then
      Application.EnableEvents = False
        If rng.Column = 5 Then 'change in E
          Cells(curr_row, "H") = rng.Value / Cells(curr_row, "C").Value
        Else 'it is H column
          Cells(curr_row, "E") = rng.Value * Cells(curr_row, "C").Value
        End If
      Application.EnableEvents = True
    End If
  Next rng
End If
End Sub


1733142447093.png
 
Upvote 1
Solution

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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