Consulting

Results 1 to 13 of 13

Thread: Runtime error '1004' Application-defined or object-defined error regarding if formula

  1. #1

    Question Runtime error '1004' Application-defined or object-defined error regarding if formula

    I am having problems using the below test macro, just to see if I can get the first stage of this if statement working for a formula.

    Here's the macro part of it:

    [VBA]Dim margin As Double
    Dim cost As Double


    Cells(1, 4).Select
    Selection.EntireColumn.Select
    Selection.Insert Shift:=xlToLeft

    Cells(1, 4).Select

    Dim LastRowPriceMargin As String

    LastRowPriceMargin = Cells(Rows.Count, "A").End(xlUp).Row

    'Range("D1").Resize(LastRowPriceMargin).Formula = "=$E1" & "*" & margin & "+" & cost
    Range("D1").Resize(LastRowPriceMargin).Formula = "=IF($E1=""LAS"",""IS LASER"",""IS INK"")"""

    Cells.Select

    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False[/VBA]

    What it basically does is go to column D, inputs a value (or is supposed to), that tests if column E has value LAS (if this is true, D column brings up IS LASER) then likewise for the other value.

    Can someone help me with this as to why it saying object defined error please?

    I'd appreciate any advice,
    Jeremy

  2. #2
    Ahh sorry, my mistake!

    I added too many double quotes at the end, should look like:

    [VBA]Range("D1").Resize(LastRowPriceMargin).Formula = "=IF($E1=""LAS"",""IS LASER"",""IS INK"")"[/VBA]

    Thanks for anyone looking at this though,
    Jeremy.

  3. #3
    Ahh I'm stuck again.

    Keeps coming up with the same error when I amend the above same formula but to:

    [VBA]Range("D1").Resize(LastRowPriceMargin).Formula = "=IF($E1=""LAS"",=$E1*margin+costlaser,IF(=$E1=""INK"",$E1*margin+cost,"""" ))"[/VBA]

    Does anyone have any ideas as to why its bringing up the same identical error?

    Bit confused now,
    Jeremy.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    sFormula = "=IF($E1=""LAS""=$E1*margin+costlaser,IF($E1=""INK"",$E1*margin+cost,"""")) "
    Range("D1").Resize(LastRowPriceMargin).Formula = sFormula
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

    Question

    Its still coming up with the same error.

    I just dont get why, here's what I have so far, might shed some light on the problem:

    [VBA]Sub marginset()

    Dim margin As Double
    Dim cost As Double
    Dim costlaser As Double

    margin = 1.136
    cost = 3.75
    costlaser = 4


    Cells(1, 4).Select
    Selection.EntireColumn.Select
    Selection.Insert Shift:=xlToLeft

    Cells(1, 4).Select

    Dim sFormula As String
    Dim LastRowPriceMargin As String


    sFormula = "=IF($E1=""LAS""=$E1*margin+costlaser,IF($E1=""INK"",$E1*margin+cost,"""")) "
    Range("D1").Resize(LastRowPriceMargin).Formula = sFormula

    'Dim LastRowPriceMargin As String

    'LastRowPriceMargin = Cells(Rows.Count, "A").End(xlUp).Row

    'Range("D1").Resize(LastRowPriceMargin).Formula = "=$E1" & "*" & margin & "+" & cost
    'Range("D1").Resize(LastRowPriceMargin).Formula = "=IF($E1=""LAS"",*margin+costlaser,""It ist not laser"")"

    Cells.Select

    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End Sub
    [/VBA]

    Just trying to find random ideas to try and get it working as I am convinced this should work, any help again is greatly appreciated.

    Thanks in advance again,
    Jeremy

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, I was sloppy, I meant

    [vba]

    sFormula = "=IF($E1=""LAS"",$E1*margin+costlaser,IF($E1=""INK"",$E1*margin+cost,"""")) "
    Range("D1").Resize(LastRowPriceMargin).Formula = sFormula
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Quote Originally Posted by xld
    Sorry, I was sloppy, I meant

    [vba]

    sFormula = "=IF($E1=""LAS"",$E1*margin+costlaser,IF($E1=""INK"",$E1*margin+cost,"""")) "
    Range("D1").Resize(LastRowPriceMargin).Formula = sFormula
    [/vba]
    Haha dont worry about it, Ive done the same many times, its just when I was fiddling with it, got to the point I couldnt remember what I had done.

    Didnt have the best of days yesturday, but will give this one a try

    Thanks again,
    Jeremy

  8. #8
    Sorry its coming up with the same error again.

    Not sure why, just cannot work this one out at all.

    Any suggestions?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Post the workbook for us to look at.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    BTW, I assume the formula in the cell should be

    =IF($E1="LAS",$E1*margin+costlaser,IF($E1="INK",$E1*margin+cost,""))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11

    Question

    I want the formula to appear in the end result (in the cell like this):

    =IF(RC[1]="LAS",RC[-1]*1.136+4,IF(RC[1]="INK",RC[-1]*1.136+3.75,""))

    1.136 is the 'margin' variable

    3.75 is the 'cost' variable

    and 4 is the 'costlaser' variable.

    Then I want the above to appear in the cell when it goes through the macro, this works perfectly when inputting directly in the cell, how would be the best way to achieve this without a loop.

    Any advice is greatly appreciated, sorry its just I cant seem to get this working, not as straight forward as I first thought.

    Also keeping away from any loops, as there's roughy 1,888 rows this must go through, their only good for short amounts of data, would be easier but not exactly efficient.

    Thanks for your help so far though,
    Jeremy

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This works in my tests, no loops, proper declaration of LastRowPrice Margin

    [vba]

    Dim LastRowPriceMargin As Long

    LastRowPriceMargin = Cells(Rows.Count, "A").End(xlUp).Row

    sFormula = "=IF(RC[1]=""LAS"",RC[1]*" & margin & "+" & costlaser & ",IF(RC[1]=""INK"",RC[1]*" & margin & "+" & cost & ",""""))"
    Range("D1").Resize(LastRowPriceMargin).FormulaR1C1 = sFormula
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Wonderful, it works!

    I just forgot to concatinate parts of it!

    Thanks ever so much for your help,
    Jeremy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •