VBA adding Formula to Cell

jamesmev

Board Regular
Joined
Apr 9, 2015
Messages
233
Office Version
  1. 365
Platform
  1. Windows
I have a sheet that I need to reference another cell.
If the cell is blank I need it not to have a result.
If the cell has a result I need it to have a result.

I am currently using a formula - however, with the formulas it is causing the workbook to be too large.

I am trying to build the vba and getting an error.

the formula i'm using is:
=IF(D3<>"",F2,IF(C3<>"",F2,""))"

this currently starts on F3.
I need this to loop column C until Row is blank
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:
VBA Code:
Sub Test()
Dim i As Long, Lr As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
With Range("F3:F" & Lr)
    .Formula = "=IF(D3<>"""",F2,IF(C3<>"""",F2,""""))"
    .Value = .Value
End With
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub Test()
Dim i As Long, Lr As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
With Range("F3:F" & Lr)
    .Formula = "=IF(D3<>"""",F2,IF(C3<>"""",F2,""""))"
    .Value = .Value
End With
End Sub
This is almost working.
If C="" and D has a value . A zero is being placed on F2.

Unsure why that is happening
 
Upvote 0
I don't fully understand what you mean. If you have some sample data, it would be helpful for helpers.

A blind guess

VBA Code:
Sub jecx()
 With Range("F3:F" & Range("C" & Rows.Count).End(xlUp).Row)
   .Value = Evaluate(Replace(Replace(Replace("if(len(##&@@),$$,"""")", "##", .Offset(, -3).Address), "@@", .Offset(, -2).Address), "$$", .Address))
 End With
End Sub
 
Last edited:
Upvote 0
Or a little bit different

VBA Code:
Sub jecxx()
 With Range("F2:F" & Range("C" & Rows.Count).End(xlUp).Row)
   .Offset(1).Value = Evaluate(Replace(Replace(Replace("if(len(##&@@),$$,"""")", "##", .Offset(1, -3).Address), "@@", .Offset(1, -2).Address), "$$", .Address))
 End With
End Sub
 
Upvote 0
I don't fully understand what you mean. If you have some sample data, it would be helpful for helpers.

A blind guess

VBA Code:
Sub jecx()
 With Range("F3:F" & Range("C" & Rows.Count).End(xlUp).Row)
   .Value = Evaluate(Replace(Replace(Replace("if(len(##&@@),$$,"""")", "##", .Offset(, -3).Address), "@@", .Offset(, -2).Address), "$$", .Address))
 End With
End Sub
The previous code worked well from maabadi

I was unsure why if Something is in column D - F2 went to 0 when it should have copied the text of F2 and pasted down the column when a row was filled in. .
However if something was in Column C - It worked perfectly.
They appear to be the same. But one doesn't seem to work.
 
Upvote 0
What you want exactly from formula?
For example for F3
1. If Cell D3 or C3 Not Blank equal F2
OR
2. If D3 Not Blank, equal F2, After That Check C3 Not Blank (Your formula now do this. Then if D3 empty, ignore Criteria at C3 and result is Blank.)
Also if one Cell at column F equal Blank the afterwards Cell also will be Blank.
 
Upvote 0
What you want exactly from formula?
For example for F3
1. If Cell D3 or C3 Not Blank equal F2
OR
2. If D3 Not Blank, equal F2, After That Check C3 Not Blank (Your formula now do this. Then if D3 empty, ignore Criteria at C3 and result is Blank.)
Also if one Cell at column F equal Blank the afterwards Cell also will be Blank.
If Cell D3 or C3 Not Blank Equal F2.
 
Upvote 0
Then try this:
VBA Code:
Sub Test()
Dim i As Long, Lr As Long 
Lr = Range("C" & Rows.Count).End(xlUp).Row 
With Range("F3:F" & Lr)
    .Formula = "=IF(Or(D3<>"""",C3<>""""),F2,"""")"
   .Value = .Value 
End With 
End Sub
 
Upvote 0
Then try this:
VBA Code:
Sub Test()
Dim i As Long, Lr As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
With Range("F3:F" & Lr)
    .Formula = "=IF(Or(D3<>"""",C3<>""""),F2,"""")"
   .Value = .Value
End With
End Sub
I am still getting the same results.
When C has a value F2 is carried down
However, when D has a value is it zeroing out F2.

1632509655173.png
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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