Solved

Pasting in these complicated formulas (part II)

Posted on 2014-04-13
7
343 Views
Last Modified: 2014-04-14
Hi All,

I asked how to paste in these formulas before in part I of the code and thought I had the answer:

(1)  "= IIf(Evaluate(Worksheets(""Tickets"").Cells(r1, ""A"").Value * Worksheets(""Tickets"").Cells(r1, ""B"").Value) < Evaluate(Worksheets(""OtherSheets"").Cells(r1, ""D"")), 1, 0)"

(2)"a = IIf(Evaluate(Worksheets(""Tickets"").Cells(r1, ""A"").Value * Worksheets(""OtherSheet"").Range(""$d$1"").Value) < Evaluate(Worksheets(""thisSheet"").Range(""$e$1"")), 1, 0)"

...and this works except I get the "answer" of 1 or 0 right away.  

The next step is to actually have these formulas pasted in so that they are evaluating.

In other words, after the operation is done I don't want to just to see a "1" or "0" in the cell.  I would like to see the whole formula.

In other, other words, I would like it in this form:

"= if(constants!AO" & i & ">constants!AM" & i & ", 1, 0)"


I ordinarily can't even do the simplest of these kinds of "paste ins" so I have zero percent chance right now with these things.

thanks for your help!
0
Comment
Question by:BostonBob
7 Comments
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Try putting a SINGLE QUOTE in front of the formula

' "= IIf(Evaluate(Worksheets(""Tickets"").Cells(r1, ""A"").Value * Worksheets(""Tickets"").Cells(r1, ""B"").Value) < Evaluate(Worksheets(""OtherSheets"").Cells(r1, ""D"")), 1, 0)"


mlmcc
0
 
LVL 35

Expert Comment

by:Bembi
Comment Utility
You want to see the formula as text, or you want to see the formala as it is in the cell?
First question is answered by mimcc, second is just to switch from the results view to the formula view (either via File - Options) or you add the command to your ribbon bar to switch via button....
0
 

Author Comment

by:BostonBob
Comment Utility
I would like to see the formula in the cell...ready to go.

In other words if i have it in this form to begin with:

(1)  =if(constantsAO10>ConstantsAM10, 1, 0)  for row 10
       =if(constantsAO11>ConstantsAM11, 1, 0) for row 11
       =if(ConstantsAO252>ConstantsAM252, 1, 0) for row 252

Will become when pasting in the formula (when triggered to do so):

(2) = "= if(constants!AO" & i & ">constants!AM" & i & ", 1, 0)"


So I would like my equation above to be in the form of (2).


Side note:  Either I did not ask the question properly above (probable) or there is another way to do what you (mlmcc) did above as what I am trying to do.  Please inform either what the correct way to "ask" the question or if there is indeed another way of doing the same formula as in my example in (2).

Thanks!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
Assuming that you want the formula in a worksheet cell, you might do it like shown in the snippet below. I show two ways. In the first, the worksheet names are as given. In the second, the worksheet names are in variables.
Sub Formulator()
Dim r1 As Long
Dim frmla1 As String, frmla2 As String, sTickets As String, sOtherSheet As String, sThisSheet As String
r1 = 10
frmla1 = "=IF(Tickets!A" & r1 & "*Tickets!B" & r1 & "<OtherSheet!D" & r1 & ",1,0)"
frmla2 = "=IF(Tickets!A" & r1 & "*OtherSheet!$D$1<thisSheet!$E$1,1,0)"
Range("A10").Formula = frmla1
Range("B10").Formula = frmla2

sTickets = "'" & Worksheets("Tickets").Name & "'!"
sOtherSheet = "'" & Worksheets("OtherSheet").Name & "'!"
sThisSheet = "'" & Worksheets("thisSheet").Name & "'!"
frmla1 = "=IF(" & sTickets & "A" & r1 & "*" & sTickets & "B" & r1 & "<" & sOtherSheet & "D" & r1 & ",1,0)"
frmla2 = "=IF(" & sTickets & "A" & r1 & "*" & sOtherSheet & "$D$1<" & sThisSheet & "$E$1,1,0)"
Range("A10").Formula = frmla1
Range("B10").Formula = frmla2
End Sub

Open in new window

0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
If you don't know which row you'll be pasting the formula in, then you could use R1C1 notation instead of A1 when building the formula. With R1C1 addressing, RC1 is equivalent to $Axxx where xxx automatically updates to the number of the row containing the formula. This eliminates the need for variable r1 when building your formula.

Note that I needed to make the reference to column A absolute in the above example because I don't know which column will receive the formula. If I knew that column F would receive the formula then I could use RC[-5] to refer to cell Axxx with 100% relative addressing.

The distinction between relative and absolute column references is only important if you need to copy a formula across.

Assuming that the absolute references to the columns aren't a problem, then you could use the following snippet:
Sub Formulator()
Dim frmla1 As String, frmla2 As String, sTickets As String, sOtherSheet As String, sThisSheet As String
frmla1 = "=IF(Tickets!RC1*Tickets!RC2<OtherSheet!RC4,1,0)"
frmla2 = "=IF(Tickets!RC1*OtherSheet!R1C4<thisSheet!R1C5,1,0)"
Range("A10").FormulaR1C1 = frmla1
Range("B10").FormulaR1C1 = frmla2

sTickets = "'" & Worksheets("Tickets").Name & "'!"
sOtherSheet = "'" & Worksheets("OtherSheet").Name & "'!"
sThisSheet = "'" & Worksheets("thisSheet").Name & "'!"
frmla1 = "=IF(" & sTickets & "RC1*" & sTickets & "RC2<" & sOtherSheet & "RC4,1,0)"
frmla2 = "=IF(" & sTickets & "RC1*" & sOtherSheet & "R1C4<" & sThisSheet & "R1C5,1,0)"
Range("A10").FormulaR1C1 = frmla1
Range("B10").FormulaR1C1 = frmla2
End Sub

Open in new window

0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
BostonBob,
The reason that you and mlmcc weren't on the same page is because you posted the question in the VB Script TA instead of the Excel TA. Though derived from the same parent as VBA, VB Script is a different language. It is commonly used in scripting tasks, much like the old .bat files in DOS 4.0.

Even though I couldn't write the simplest bit of code in VB Script, I often tackle Excel questions that are miscategorized in the VB Script TA.

I've added the Excel TA to the question, acting in my capacity as Topic Advisor.

Brad
0
 

Author Comment

by:BostonBob
Comment Utility
As usual Mr.  Byundt, your answers are "over-the-top," eye-poppingly, awesome!  Not only did you give me the answer, you gave me the answer two different ways while at the same time opening up a whole new branch of programming for vba!

I have printed these answers out and I am just staring at it thinking to myself that I have not even "scratched the surface" of what is possible in VBA when one of you (Experts on Expert's Exchange) divine something like this for me.

You're teaching me so much; I am so grateful!

Thanks doesn't say it all.  It couldn't even begin to.

Can I award you 50000 points?  

As for the VB Script comment....I had no idea!!! All this time I thought that "VB Script" was the "right choice" and here I find that isn't the case at all.  Excel TA all the way from here on in!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now