Apply formulas to cells in range with VBA with increasing row values to match?

LucidCheetah

New Member
Joined
Dec 11, 2017
Messages
15
I need to assign a formula to a range of cells in VBA to fix an issue I'm having. In the range of A1:A300 I have the formula =D1&"_"&E1, =D2&"_"&E2, =D3&"_"&E3, etc etc increasing to match the row number through the column range. How can I apply this formula to each cell in the range with the E & D row values increasing to match the row value of the cells in range?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this

VBA Code:
Sub InsertFormulas()

    Dim Cel As Range
    
    For Each Cel In Range("A1:A300")
        With Cel
            .Formula = "=D" & .Row & "&""_""&E" & .Row
        End With
    Next Cel

End Sub
 
Upvote 0
You can do what you want without using a loop...
VBA Code:
Sub InsertFormulas()
  Range("A1:A300").Formula = "=D1&""_""&E1"
End Sub
 
Upvote 0
You can do what you want without using a loop...
VBA Code:
Sub InsertFormulas()
  Range("A1:A300").Formula = "=D1&""_""&E1"
End Sub

This seems to fit into my macro a little easier and works for 2 out of 3 on the formulas I'm trying to apply it to. The third formula doesn't seem to want to work for me though. When i put the formulas in manually it gives me all the data I want, however trying to apply this in VBA gives me a Syntax error? Am i missing something, or is there an easier way to achieve what I'm trying to do here?


VBA Code:
Worksheets("Sheet2").Range("C2:C300").Formula = "=IF(L2="DOWN","D",IF(L2="TOP","T",IF(L2="SIDE","S",IF(L2="HIGH","H",""))))"
 
Last edited:
Upvote 0
This seems to fit into my macro a little easier and works for 2 out of 3 on the formulas I'm trying to apply it to. The third formula doesn't seem to want to work for me though. When i put the formulas in manually it gives me all the data I want, however trying to apply this in VBA gives me a Syntax error? Am i missing something, or is there an easier way to achieve what I'm trying to do here?
VBA Code:
Worksheets("Sheet2").Range("C2:C300").Formula = "=IF(L2="DOWN","D",IF(L2="TOP","T",IF(L2="SIDE","S",IF(L2="HIGH","H",""))))"
Text constants (what is being assigned to the Formula property) have quote marks on the outside ends. So that VB can tell what is a quote mark delineating a text constant and what is an actual quote mark character located inside the quote marks delineating a text constant, VB requires those actual (internal) quote marks to be doubled up. Here is your code line with that doubling up of internal quote marks applied....
VBA Code:
Worksheets("Sheet2").Range("C2:C300").Formula = "=IF(L2=""DOWN"",""D"",IF(L2=""TOP"",""T"",IF(L2=""SIDE"",""S"",IF(L2=""HIGH"",""H"",""""))))"
 
Upvote 0
When you put quotes inside of quotes you need to double up

VBA Code:
Worksheets("Sheet2").Range("C2:C300").Formula = "=IF(L2=""DOWN"",""D"",IF(L2=""TOP"",""T"",IF(L2=""SIDE"",""S"",IF(L2=""HIGH"",""H"",""""))))"
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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