Excel Formulas in VBA Editor

harrisont9

New Member
Joined
Oct 21, 2013
Messages
7
Hello,

I'm trying to automatize a report. I have several huge formulas that I would like to embed into the VBA editor. Can anyone help?

This is the formula I want the Macro to enter:

[TABLE="width: 64"]
<tbody>[TR]
[TD="class: xl109, width: 64"]IFERROR(IF(IF(ISNA(VLOOKUP(A:A,'Q1186 Warehouse Order Roundup'!A:S,MATCH("SHIP_STATUS",'Q1186 Warehouse Order Roundup'!$A$1:$S$1,0),0)),VLOOKUP(A:A,'Q1188 Staged not shipped'!A:S,MATCH("SHIPPING_STATUS",'Q1188 Staged not shipped'!$A$1:$S$1,0),0),VLOOKUP(A:A,'Q1186 Warehouse Order Roundup'!A:S,MATCH("SHIP_STATUS",'Q1186 Warehouse Order Roundup'!$A$1:$S$1,0),0))=0,"",IF(ISNA(VLOOKUP(A:A,'Q1186 Warehouse Order Roundup'!A:S,MATCH("SHIP_STATUS",'Q1186 Warehouse Order Roundup'!$A$1:$S$1,0),0)),VLOOKUP(A:A,'Q1188 Staged not shipped'!A:S,MATCH("SHIPPING_STATUS",'Q1188 Staged not shipped'!$A$1:$S$1,0),0),VLOOKUP(A:A,'Q1186 Warehouse Order Roundup'!A:S,MATCH("SHIP_STATUS",'Q1186 Warehouse Order Roundup'!$A$1:$S$1,0),0))),"")[/TD]
[/TR]
</tbody>[/TABLE]


This is how it looks in the VBA editor window. Everything works fine, except the section in bold.

Sheets("Formulas").Select
Range("C2").Select
Selection.Copy
Sheets("DataConsolidation").Select
Range("D2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(IF(ISNA(VLOOKUP(C[-3],'Q1186 Warehouse Order Roundup'!C[-3]:C[15],MATCH(""SHIP_STATUS"",'Q1186 Warehouse Order Roundup'!R1C1:R1C19,0),0)),VLOOKUP(C[-3],'Q1188 Staged not shipped'!C[-3]:C[15],MATCH(""SHIPPING_STATUS"",'Q1188 Staged not shipped'!R1C1:R1C19,0),0),VLOOKUP(C[-3],'Q1186 Warehouse Order Roundup'!C[-3]:C[15],MATCH(""SHIP_STATUS"",'Q1186 Warehous" & _
"Roundup'!R1C1:R1C19,0),0))=0,"""",IF(ISNA(VLOOKUP(C[-3],'Q1186 Warehouse Order Roundup'!C[-3]:C[15],MATCH(""SHIP_STATUS"",'Q1186 Warehouse Order Roundup'!R1C1:R1C19,0),0)),VLOOKUP(C[-3],'Q1188 Staged not shipped'!C[-3]:C[15],MATCH(""SHIPPING_STATUS"",'Q1188 Staged not shipped'!R1C1:R1C19,0),0),VLOOKUP(C[-3],'Q1186 Warehouse Order Roundup'!C[-3]:C[15],MATCH(""SHIP_ST" & _
"1186 Warehouse Order Roundup'!R1C1:R1C19,0),0))),"""")"
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hello

You can do far better (IMHO) by not doing this in VBA.
Put the formula in a (hidden) cell and use VBA to just grab the formula:

Code:
 Range("D2").Formula = Range("H1").Formula

For example.
 
Upvote 0
Hey Wigi,

The sheet where the formulas need to go is first created by a macro. What would the code be to tell VBA to grab the formula from a hidden cell in a different worksheet?

Thanks,
 
Upvote 0
You can add the reference to the sheet in front:

Code:
Sheets("nameofthesheet").Range("H1").Formula
 
Upvote 0
Wigi,

What if the formulas I need come from an entire separate workbook? For example, the formula from a cell in sheet X in workbook X needs to go into a cell in sheet Y from workbook Y.
 
Upvote 0
What I showed you before, was adding the sheet name in front of the Range.
By the same token, add the workbook reference in front, again, so that you can refer to a workbook other than the Active workbook.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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