Writing formulas with VBA better methods?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a report which is generated with VBA referencing data from another sheet with formulas then turned into values

Example
Code:
Range("D2:D1000").FormulaR1C1 = "=R[-1]C[-1]*10"
Range("D2:D1000").value = Range("D2:D1000").value

Which works fine but the problem is if I ever need to modify formulas due to new columns inserted etc... It's a pain having to modify the VBA everytime

Is there a better way? Like readable VBA instead of R1C1 notation would help

Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have a report which is generated with VBA referencing data from another sheet with formulas then turned into values
Your example does not fit this description.
readable VBA instead of R1C1 notation
Instead of the first line of code you posted :
Range("D2:D1000").Formula = "=C1*10"

Or you could replace both lines of code with :
[D2:D1000] = [C2:C1000*10]
 
Last edited:
Upvote 0
Amendment:

Or you could replace both lines of code with :

[D2:D1000] = [C1:C999*10]
 
Upvote 0
Amendment:

Or you could replace both lines of code with :

[D2:D1000] = [C1:C999*10]

Thank you this is what I meant. Usually I put formula in the cell, then record macro to get the code.

[D2:D1000] = [C1:C999*10]

Can you do all formula like this? IF statement / fill down etc?
 
Upvote 0
Can you do all formula like this? IF statement / fill down etc?

A qualified yes, depending upon what you actually want to do and the actual formulas.
Note : The code posted does not write a formula to the sheet - it places the calculated values directly into D2:D1000

Code:
[COLOR=#333333]Usually I put formula in the cell, then record macro to get the code. [/COLOR]

To avoid R1C1 notation, type the formula in the macro like : Range("D2:D1000").Formula = "=C1*10"
 
Upvote 0
For the OP code, I would use

Code:
With Range("D2:D1000")
    .FormulaR1C1 = "=R[-1]C[-1]*10"
    .Value = .Value
End With

That way, there is one location in the VB code to be changed if the cells are different. Its also easier to convert the hard-coded range to a variable.
 
Upvote 0
Thank you both, so I can combine both your solution like:

Code:
With Range("E2:E1000")
    .Formula = "=IF(D2>0,D2/60,0)"
    .Value = .Value
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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