vba help - pass variable in R1CI formula not working

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Need help in passing variable in below Recorded Code.

Recorded code :=> ActiveCell.FormulaR1C1 = "=SUM(R[-38]C+R[-36]C+R[-34]C)"


My Variable Range as follows. I have found Range via find method. need to pass these variable in above recorded formula.
rng_TelReimbursement
rng_DvrSalary
Rng_Fuel


Attempted Code

VBA Code:
With Sheet1
        With .Range(.Cells(50, 4), .Cells(50, lc + 1))
                    .FormulaR1C1 = "=SUM(R[" & rng_TelReimbursement.row & " ]C+R[" & rng_DvrSalary.row & "]C+R[" & Rng_Fuel.row & "]C)"
            End With
End With


Thanks
mg
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Note that in your formula it is -38, -36, and -34.
You have left the negatives out of your formula. .Row will only return positive values.
And note in R1C1 format, it is all relative to the cell you are putting the formula in. So using ".Row" may not be correct, without adding some offset to that.

If you have issues figuring it out, please let us know that all those ".Row" references return, and explain to us how the exact logic of the formula, especially as it relates to the row the formula is in.
 
Upvote 0
I suspect what you want is to remove the square brackets altogether:

Code:
.FormulaR1C1 = "=SUM(R" & rng_TelReimbursement.row & "C,R" & rng_DvrSalary.row & "C,R" & Rng_Fuel.row & "C)"

Note that there is no point in using both SUM and + for that.
 
Upvote 0
How about
VBA Code:
   With Sheet1
      With .Range(.Cells(50, 4), .Cells(50, lc + 1))
         .FormulaR1C1 = "=SUM(R" & rng_TelReimbursement.Row & " C,R" & rng_DvrSalary.Row & "C,R" & Rng_Fuel.Row & "C)"
      End With
   End With
 
Upvote 0
Hi Joe,Rory and Fluff.

Thanks for your help. tried your code its working.
Rory's answer select specific cells,
Fluffs answer also gives correct it select entire column.

I will use Rorys code, But thanks for sharing multiple Option.


Below is my Code gives correct output.
But in Future if expendiature Items increase I have to Change my code.

Can we Make Dynamic here using loops......Answer I want in sum formula

Like----->'=SUM(E6,E8,E10)




VBA Code:
Sub Sum_Help()


    Dim rng_DriverSalary As Range
    Dim rng_Fuel As Range
    Dim rng_Telephone As Range
    Dim rng_Reimbursement_Payble As Range
       
    Set rng_Telephone = Sheet1.Range("D:D").Find("Telephone Reimbursement", LookIn:=xlValues, lookat:=xlPart)
    Set rng_DriverSalary = Sheet1.Range("D:D").Find("Driver's Salary", LookIn:=xlValues, lookat:=xlPart)
    Set rng_Fuel = Sheet1.Range("D:D").Find("Fuel & Maintenance", LookIn:=xlValues, lookat:=xlPart)
    Set rng_Reimbursement_Payble = Sheet1.Range("D:D").Find("Reimbursement Payble", LookIn:=xlValues, lookat:=xlPart)
    
    
    
    With Sheet1
        With .Range(.Cells(rng_Reimbursement_Payble.Row, 5), .Cells(rng_Reimbursement_Payble.Row, 6))
                    .FormulaR1C1 = "=SUM(R" & rng_Telephone.Row & "C,R" & rng_DriverSalary.Row & "C,R" & rng_Fuel.Row & "C)"
            End With
    End With

End Sub


Below is a Table, K column is the criteria. how to create below formula using loops.
Actual my Criteria is in closed workbook.

.FormulaR1C1 = "=SUM(R" & rng_Telephone.Row & "C,R" & rng_DriverSalary.Row & "C,R" & rng_Fuel.Row & "C)"


Book3
ABCDEFGHIJK
1
2Cost Head100020003000Expendiature List
3Basic220223271735091Telephone Reimbursement
4DA388862022731564Fuel & Maintenance
5HRA230442148336847Driver's Salary
6Telephone Reimbursement100200300
7Bonus242033203320467
8Fuel & Maintenance300500800
9Car Allowance264322053129890
10Driver's Salary4003001200
11
12
13=SUM(E6,E8,E10)
14
15
16Reimbursement Payble80010002300
17
Sheet1
Cell Formulas
RangeFormula
E16:F16E16=SUM(E$6,E$10,E$8)
G16G16=SUM(G6,G8,G10)



Thanks
mg
 
Upvote 0
As this is now a totally different question, you will need to start a new thread. Thanks
 
Upvote 0
Hi Fluf and Rory.

yes will post new thread for new request.

And Thanks for your help in passing variable in formula's.?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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