Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA line

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I can't seem to compile the following line, as I have "" within the formula - does any one know how to get round this?

Range("L5").Formula = "=IF(G6="KO","*",IF(AND(G6="RKO",F6="C"),ABS(I6-E6)*(K6/I6),IF(G6="OT",K6,IF(AND(G6="RKO",F6="P"),ABS(H6-E6)*(K6/H6),IF(AND(G6="RKI",F6="C"),ABS(I6-E6)*(K6/I6),IF(G6="OT",L6,IF(AND(G6="RKI",F6="P"),ABS(H6-E6)*(K6/H6),0)))))))"
 
Just use double quotes for those that are required in the forumula.
Code:
Dim sFormula As String

'Forumla: =IF(A1>0,A1,"")
sFormula = "=IF(A1>0,A1,"""")"
    
Range("B2").Formula = sFormula
[\code]
Hope this helps...
 
Try replacing the double quotes in the formula (") with single quotes ('). I haven't tried it but I'd imagine that would do the trick.

Ed Metcalfe.
 
You could spend the time building the string using Chr(34).

'Forumla: =IF(A1>0,A1,"")
sFormula = "=IF(A1>0,A1," & Chr(34) & Chr(34) & ")"

A little more effort, but it will work.
 
DSI, I think the problem lies with using " rather than spaces. Because I define certain words in the formula it thinks that it is the end of defining the formula.
 
Chr(34) in my last sample adds quotes to the string literal. You should utilize a string variable to build the string before setting the Forumula property.
Code:
Dim sEqn As String

'---------------------------------
'Forumla for Cell: =IF(A1>0,A1,"")
'---------------------------------
sEqn = "=IF(A1>0,A1," & Chr(34) & Chr(34) & ")"

Range("B2").Formula = sEqn
You can also test this by displaying the equation. In the next example, you would build your string and run TestEqn until the displayed value matched the desired forumula.
Code:
Sub TestEqn()
  Dim sEqn As String
  sEqn = "=IF(A1>0,A1," & Chr(34) & Chr(34) & ")"
  MsgBox sEqn
End Sub
 

BenCD... I love to answer questions by not answering them. Without trying to be judgemental, your equation could use some help, is there an easier way to accomplish what you're trying to do?

Or - another thing you might try is to create the equation in Excel on the spreadsheet. Excel's color coding will help you make sure you have your parenthesis right. Then you can record a quick macro adding one small thing like "+1" to the end of the equation and take a look at the VBA that Excel creates.
 
I think that dsi is on the right track. The following code put "=IF(G6="KO","*",IF(AND(G6="RKO",F6="C"),ABS(I6-E6)*(K6/I6),IF(G6="OT",K6,IF(AND(G6="RKO",F6="P"),ABS(H6-E6)*(K6/H6),IF(AND(G6="RKI",F6="C"),ABS(I6-E6)*(K6/I6),IF(G6="OT",L6,IF(AND(G6="RKI",F6="P"),ABS(H6-E6)*(K6/H6),0)))))))" in cell L5:

Sub Sample()
Dim lQuoteChar As String

lQuoteChar = Strings.Chr(34)

Range("L5").Formula = "=IF(G6=" + lQuoteChar + "KO" + _
lQuoteChar + "," + lQuoteChar + "*" + lQuoteChar + _
",IF(AND(G6=" + lQuoteChar + "RKO" + lQuoteChar + _
",F6=" + lQuoteChar + "C" + lQuoteChar + _
"),ABS(I6-E6)*(K6/I6),IF(G6=" + lQuoteChar + "OT" + _
lQuoteChar + ",K6,IF(AND(G6=" + lQuoteChar + "RKO" + _
lQuoteChar + ",F6=" + lQuoteChar + "P" + lQuoteChar + _
"),ABS(H6-E6)*(K6/H6),IF(AND(G6=" + lQuoteChar + "RKI" _
+ lQuoteChar + ",F6=" + lQuoteChar + "C" + lQuoteChar + _
"),ABS(I6-E6)*(K6/I6),IF(G6=" + lQuoteChar + "OT" + _
lQuoteChar + ",L6,IF(AND(G6=" + lQuoteChar + "RKI" + _
lQuoteChar + ",F6=" + lQuoteChar + "P" + lQuoteChar + _
"),ABS(H6-E6)*(K6/H6),0)))))))"

End Sub

Hope this helps,
Pete
 
I HAVE IT!!!

Do it like this.... drop the equation you want into you cell, replacing the initial "=" with "XXX" and replacing your double quotes with single quotes. Then do a quick find and replace to change the ' to " and the XXX to =. Example below (works!)


Code:
Sub workaround()

    Range("C7").Select
    ActiveCell.Value = "xxxIF(G6='KO','*',IF(AND(G6='RKO',F6='C'),ABS(I6-E6)*(K6/I6),IF(G6='OT',K6,IF(AND(G6='RKO',F6='P'),ABS(H6-E6)*(K6/H6),IF(AND(G6='RKI',F6='C'),ABS(I6-E6)*(K6/I6),IF(G6='OT',L6,IF(AND(G6='RKI',F6='P'),ABS(H6-E6)*(K6/H6),0)))))))"

    Cells.Replace What:="'", Replacement:="""", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False
    Cells.Replace What:="XXX", Replacement:="=", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top