Results 1 to 9 of 9

Thread: [RESOLVED] problem with ' in a formula

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Resolved [RESOLVED] problem with ' in a formula

    Hi
    I want to write a formula into a cell
    I used the following code:
    oXLSheet.cells(20,2).Formula="=SUMPRODUCT(COUNTIF(INDIRECT('Sheet1'!D2:D100"),"*Economics*"))" but it considered ' as a comment so only =SUMPRODUCT(COUNTIF(INDIRECT( is considered as a formula.('Sheet1'!D2:D100"),"*Economics*")) is colored in green like a comment.
    How can I deal with this problem
    thanks
    Last edited by RobDog888; Jun 4th, 2006 at 09:09 AM. Reason: Disabled smilies in this post

  2. #2
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: problem with ' in a formula

    I got rid of the " after D100, and then I placed double " so VBA reads it as actual quotes in the formula. I hope this is what you were trying to accomplish.

    oXLSheet.Cells(20, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT('Sheet1'!D2:D100),""*Economics*""))"

    By the way, the code didn't comment anything out for me when I tried your original formula.




    If you find any of my posts of good help, please rate it

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: problem with ' in a formula

    Hi
    I am trying to write formulas from VB6 to an excel worksheet,the formula that i am trying to write is:

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&J2&"'!L2:L1000"),"*Economics*")) but it considered ' as a comment so only =SUMPRODUCT(COUNTIF(INDIRECT( is considered as a formula.The remaining of the formula is colored in green like a comment.

    May someone help me?
    thanks

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: problem with ' in a formula

    Hi

    If I am not wrong then there should be a " sign after ...(INDIRECT(

    so the code should actually be

    VB Code:
    1. oXLSheet.cells(20,2).Formula="=SUMPRODUCT(COUNTIF(INDIRECT("'Sheet1'!D2 : D100"),"*Economics*"))"

    plz correct me if i am wrong...

    oops... i see what you mean...

    i tried various combinations and i found out that when i was using oXLSheet.cells(20,2).Formula it gave me an error "object required"
    however when i keyed in the below code it didn't give me any error but the cell value turned out to be "ref#!"

    VB Code:
    1. Worksheets("Sheet1").Cells(20, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT(Sheet1!$D$2:$D$100),"" * Economics * ""))"

    would this help?
    Last edited by Siddharth Rout; Jun 5th, 2006 at 07:52 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: problem with ' in a formula

    Code:
    oXLSheet.cells(20,2).Formula="=SUMPRODUCT(COUNTIF(INDIRECT(" & chr(34) & "'Sheet1'!D2 : D100" & chr(34) & ")," & chr(34) & "*Economics*" & chr(34) & "))"
    You need chr(34) the " speechmarks in the string itself. Either do two per speechmark ("") or escape the character or as I did above use the ascii/chr function.

    You should have been worried when VBA coding changed colour! intellisense to the rescue!

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: problem with ' in a formula

    Quote Originally Posted by malik641
    I got rid of the " after D100, and then I placed double " so VBA reads it as actual quotes in the formula. I hope this is what you were trying to accomplish.

    oXLSheet.Cells(20, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT('Sheet1'!D2:D100),""*Economics*""))"

    By the way, the code didn't comment anything out for me when I tried your original formula.
    This worked just fine for me....did anyone else have a problem with it?




    If you find any of my posts of good help, please rate it

  7. #7
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: problem with ' in a formula

    Even though I was using VBA, I tried with VB6 and got a good result.
    I just made a command button on a blank userform:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim oXLApp As Object
    3. Dim oXLWB As Object
    4. Dim oXLSheet As Object
    5.  
    6. Set oXLApp = GetObject(, "Excel.Application")
    7. Set oXLWB = oXLApp.Workbooks.Add
    8. Set oXLSheet = oXLWB.Sheets(1)
    9.  
    10. oXLSheet.Cells(20, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT('Sheet1'!D2:D100),""*Economics*""))"
    11.  
    12. Set oXLApp = Nothing
    13. Set oXLWB = Nothing
    14. Set oXLSheet = Nothing
    15.  
    16. Me.Hide
    17. End Sub

    Also, even though the "Code" tags are finding a comment in that, it didn't in VB6 or VBA.




    If you find any of my posts of good help, please rate it

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: problem with ' in a formula

    Hi Ecniv

    VB Code:
    1. oXLSheet.cells(20,2).Formula="=SUMPRODUCT(COUNTIF(INDIRECT(" & chr(34) & "'Sheet1'!D2 : D100" & chr(34) & ")," & chr(34) & "*Economics*" & chr(34) & "))"
    thanks a lot, ur code worked well.

  9. #9
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: problem with ' in a formula

    No problem. Try out other peoples suggestions too - gives you more options when programming

    Oh and if this thread is resolved, set it so via the tools link above the thread.

    Happy programming

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width