VBA code to divide a range of cells by a variable with an option to select each time

Nravota

New Member
Joined
Apr 2, 2015
Messages
7
Hi everyone,

First, thank you for any information and ideas shared. I need a vba code which is able to divide range of numbers with two decimals (prices) with decimal coefficient. For instance, I have values in A1:A25 and I need to divide them with a different value each time (0.9 or 0.92 or 0.9205). Then I would like to have the results with two decimals pasted in another column. :eeek: So, I need to have an option in the macro to enter the variable I need, before it gave the results.

Can someone help with this?

Thank you in advance for the support!
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to the Board!

Maybe something like this?
Code:
Sub MyDivide()


    Dim myRange As Range
    Dim cell As Range
    Dim myDivisor As Double
    Dim myOffset As Integer
    
    Application.ScreenUpdating = False
    
'   Determine range to apply to
    Set myRange = Range("A1:A25")
    
'   Indicate how many columns to offset the results by (i.e. "B" would be 1 columns for "A")
    myOffset = 1
    
'   Prompt to enter divisor
    myDivisor = InputBox("Enter the value you wish to divide by")
    
'   Divide each cell and round to 2 decimals
    For Each cell In myRange
        cell.Offset(0, 1) = Round(cell / myDivisor, 2)
    Next cell
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Great! Many thanks, this function works perfectly. I was also wondering if there is a way to select the range with the cursor each time instead of fixing it from A1 to A25? Thanks once again for sharing this.
 
Upvote 0
Great! Many thanks, this function works perfectly. I was also wondering if there is a way to select the range with the cursor each time instead of fixing it from A1 to A25? Thanks once again for sharing this.

Hi, I found the solution, it can't be more simple, but if someone new to vba like me ever wonders about it:

Set myRange = Selection
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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