Your result is exactly what you told Excel to do.
when you put something in quotes ("), you're telling Excel to "take this string 
LITERALLY"
and do not "evaluate" the string.
So you basically said to set the cell value to the value of 'indname' and 
append a string.

You might as well say:
activecell.value = indname & " Excel is Fun!"

So, the problem is with the syntax.

What I would suggest that you do is manually enter the countif into a cell.

Something like:
=COUNTIF(Data!B2:B100,A4)
(using the appropriate ranges)

Then, "prefix" the cell formula with something like: "Test_Title:"
="Test_Title:" & COUNTIF(Data!B2:B100,A4)

Next,  Fire up the Macro Recorder.
Simply click inside the formula and hit "enter".
(You don't really need to change anything)
Stop Recording and look at the macro.

You'll see a formula that looks something like:

ActiveCell.FormulaR1C1 = _
        "=""Test_Title:"" & COUNTIF(Data!R[-3]C[-36]:R[96]C[-36],RC[-37])"

(your values will vary)

Now, replace "Test_Title" with your  variable "indname"

Now, the problem here is that you want VBA to substitute the VALUE of "indname"
But you need the quotes (") in Excel.
So you'll actually have to add a couple of quotes.

Note:  "=" means to add a literal string EQUAL SIGN
But how do you add a double-quote? (")
The trick here is that "special" characters like this are represented by using 
them twice:
"" will represent a single (").
But """ doesn't work, because VBA interprets it as an OPEN delimiter, then 
a quote string, and no closing delimiter.
So, actually to enter a double-quote, it takes (4) of them ("""")
this is (open delimiter)"(close delimiter)
if you want to include an equal sign (=) then it's: (open delimiter)="(close 
delimiter)
or "="""

so your resulting macro code looks like:

    ActiveCell.FormulaR1C1 = _
        "=""" & indname & """ & COUNTIF(Data!R[-3]C[-36]:R[96]C[-36],RC[-37])"

in your example, it would probably be:

ActiveCell.FormulaR1C1 = "=""" & indname & """ & 
COUNTIF(Stocks!C[-2]:C,Main!RC[-2])"

although I cannot test it.
--------------
OK, I COULD have simply given you the syntax correction, but how can you learn 
anything from THAT?
--------------
Paul
-----------------------------------------
“Do all the good you can,
By all the means you can,
In all the ways you can,
In all the places you can,
At all the times you can,
To all the people you can,
As long as ever you can.” - John Wesley
-----------------------------------------




________________________________
From: mburkett <michaelburk...@gmail.com>
To: excel-macros@googlegroups.com
Sent: Fri, August 31, 2012 12:38:20 PM
Subject: $$Excel-Macros$$ Combining variables with Excel Formulas

I have some code that is not working as I expected. I set a variable from a 
value in a cell within the worksheet and add a countif formula. The result I 
get 
in the cell is the variable along with the formula as written (as opposed to 
the 
result of the formula). Ideas?


'set variable
indname = Selection.Value

ActiveCell.FormulaR1C1 = indname & "+=COUNTIF(Stocks!C[-2]:C,Main!RC[-2])"

'Here is the result in the cell: LC Consumer Staples Sector 
+=COUNTIF(Stocks!C[-2]:C,Main!RC[-2])-- 

Join official facebook page of this forum @ 
https://www.facebook.com/discussexcel
 
FORUM RULES (1120+ members already BANNED for violation)
 
1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.
 
2) Don't post a question in the thread of another member.
 
3) Don't post questions regarding breaking or bypassing any security measure.
 
4) Acknowledge the responses you receive, good or bad.
 
5) Cross-promotion of, or links to, forums competitive to this forum in 
signatures are prohibited. 

 
6) Jobs posting is not allowed.
 
7) Sharing copyrighted ebooks/pirated ebooks/their links is not allowed.
 
NOTE : Don't ever post personal or confidential data in a workbook. Forum 
owners 
and members are not responsible for any loss.
--- 
You received this message because you are subscribed to the Google Groups "MS 
EXCEL AND VBA MACROS" group.
To post to this group, send email to excel-macros@googlegroups.com.
To unsubscribe from this group, send email to 
excel-macros+unsubscr...@googlegroups.com.

-- 
Join official facebook page of this forum @ 
https://www.facebook.com/discussexcel

FORUM RULES (1120+ members already BANNED for violation)

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.

2) Don't post a question in the thread of another member.

3) Don't post questions regarding breaking or bypassing any security measure.

4) Acknowledge the responses you receive, good or bad.

5)  Cross-promotion of, or links to, forums competitive to this forum in 
signatures are prohibited. 

6) Jobs posting is not allowed.

7) Sharing copyrighted ebooks/pirated ebooks/their links is not allowed.

NOTE  : Don't ever post personal or confidential data in a workbook. Forum 
owners and members are not responsible for any loss.
--- 
You received this message because you are subscribed to the Google Groups "MS 
EXCEL AND VBA MACROS" group.
To post to this group, send email to excel-macros@googlegroups.com.
To unsubscribe from this group, send email to 
excel-macros+unsubscr...@googlegroups.com.


Reply via email to