VBA Cell Formula to Name Range

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions for which I will give feedback.

I am trying to set cell to a formula involving name ranges. Where, for example cell D53 [Cell(53,4)] formula would be = V1.Total.NPV10 + V2.Total.NPV10 + V3.Total.NPV10 + V4.Total.NPV10 + V5.Total.NPV10

since the value in cell D1 [cell(1, 4)] = NPV10


Why does the following line of code turn red? Also, have I done the quotes done correctly?

Code:
 ActiveSheet.Cells.(53, j).Formula "= V1.Total." & Cells(1, j).Value & "+" _
                    "V2.Total." & Cells(1, j).Value & "+" _
                    "V3.Total." & Cells(1, j).Value & "+" _
                    "V4.Total." & Cells(1, j).Value & "+" _
                    "V5.Total." & Cells(1, j).Value"


Entire code is as follows:
Code:
Sub FormulawithNameRanges()


    Dim j As Integer
    
    Worksheets("1").Activate


        For j = 4 To 26
            
                ActiveSheet.Cells.(53, j).Formula "= V1.Total." & Cells(1, j).Value & "+" _
                    "V2.Total." & Cells(1, j).Value & "+" _
                    "V3.TOTAL." & Cells(1, j).Value & "+" _
                    "V4.Total." & Cells(1, j).Value & "+" _
                    "V5.Total." & Cells(1, j).Value"
                
         End If
         
         Next j
         


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Is red because is incorrectly 'split' in rows.
Try this:

Code:
Sub FormulawithNameRanges()
    Dim j As Integer
    Worksheets("1").Activate
        For j = 4 To 26
            ActiveSheet.Cells(53, j).Formula [B]=[/B] "= V1.Total." & Cells(1, 4).Value & _
                "+ V2.Total." & Cells(1, 4).Value & "+ V3.TOTAL." & Cells(1, 4).Value & _
                "+ V4.Total." & Cells(1, 4).Value & "+V5.Total." & Cells(1, 4).Value
         Next j
End Sub
Also you've got End if without If
Missing = after formula
 
Last edited:
Upvote 0
Solution
Thanks KOKOSEK that fixed the issue and apologies for the late response.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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