|
-
Jun 4th, 2006, 07:43 AM
#1
Thread Starter
Fanatic Member
[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
-
Jun 4th, 2006, 11:17 AM
#2
Addicted Member
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.
-
Jun 5th, 2006, 01:16 AM
#3
Thread Starter
Fanatic Member
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
-
Jun 5th, 2006, 06:39 PM
#4
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:
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:
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
-
Jun 6th, 2006, 06:27 AM
#5
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!
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...
-
Jun 6th, 2006, 09:22 PM
#6
Addicted Member
Re: problem with ' in a formula
 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?
-
Jun 6th, 2006, 10:03 PM
#7
Addicted Member
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:
Private Sub Command1_Click()
Dim oXLApp As Object
Dim oXLWB As Object
Dim oXLSheet As Object
Set oXLApp = GetObject(, "Excel.Application")
Set oXLWB = oXLApp.Workbooks.Add
Set oXLSheet = oXLWB.Sheets(1)
oXLSheet.Cells(20, 2).Formula = "=SUMPRODUCT(COUNTIF(INDIRECT('Sheet1'!D2:D100),""*Economics*""))"
Set oXLApp = Nothing
Set oXLWB = Nothing
Set oXLSheet = Nothing
Me.Hide
End Sub
Also, even though the "Code" tags are finding a comment in that, it didn't in VB6 or VBA.
-
Jun 7th, 2006, 01:29 AM
#8
Thread Starter
Fanatic Member
Re: problem with ' in a formula
Hi Ecniv
VB Code:
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.
-
Jun 7th, 2006, 04:04 AM
#9
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|