easy formula = HARD with ".Formula" property in VBA

naznorb

Board Regular
Joined
Nov 24, 2009
Messages
51
Here's a formula I'm using:

Code:
=IF(tree.1!$A1<>"",tree.1!$A1,"")
Basically, I'm copying data from one sheet to another (please, don't ask me how I got myself into this situation!), and trying to avoid 0s for blank cells.

Problem: I have to do this to 10,000s of cells, so I had a handy on-open macro:

Code:
FinalRow = Worksheets("tree.1").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("tree").Activate
For q = 1 To FinalRow
ActiveSheet.Cells(q, 1).Formula = "=tree.1!A" & q
Next
Worked great -- but I desperately need the blank-cell trap in there. How do I deal with the double-quotes (which presumably themselves have to be inside quotes)?

Have a feeling it's something dopey or simple, but haven't been able to find anything with search of Forum or FAQs. Any help much appreciated.​
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Here's a formula I'm using:

Code:
=IF(tree.1!$A1<>"",tree.1!$A1,"")

Translates to this as a VBA string:
Code:
"=IF(tree.1!$A1<>"""",tree.1!$A1,"""")"

I'll let you work out how to concatenate it together with variables in your code.
 
Upvote 0
If you just want the values, you could use
Code:
With Worksheets("tree.1")
    With Range(.Cells(1,1), .Cells(Rows.Count, 1).End(xlUp))
        ActiveSheet.Range(.Address).Value = .Value
    End With
End With
If you want a formula in the cells, to react to changes in tree.1, you can eliminate the 0's by concatinating a "". (Caution, this will turn all the numbers in the tree.1 range into text on the active sheet)

Code:
With Worksheets("tree.1")
    With Range(.Cells(1,1), .Cells(Rows.Count, 1).End(xlUp))
        ActiveSheet.Range(.Address).FormulaR1C1 = "=tree.1!RC1 & """" "
    End With
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

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