Why this VBA doesn't work?

excelos

Well-known Member
Joined
Sep 25, 2011
Messages
591
Office Version
  1. 365
Platform
  1. Windows
I try to do this:

Range("A1").Formula = "This is some text ",TEXT((COUNTIFS(UDE!$AC:$AC,"Low",UDE!$G:$G,"High")+COUNTIFS(UDE!$AC:$AC,"4AMLD",UDE!$G:$G,"High"))/COUNTIF(UDE!$G:$G,"High"),"00%"))"
Range("A1").Formula = "=CONCATENATE("This is some text ",TEXT((COUNTIFS(UDE!$AC:$AC,"Low",UDE!$G:$G,"High")+COUNTIFS(UDE!$AC:$AC,"4AMLD",UDE!$G:$G,"High"))/COUNTIF(UDE!$G:$G,"High"),"00%"))"


Any idea?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
What doesn't work?
I see TWO VBA statements.
The first doesn't work? The second doesn't work? They both don't work?

How don't they work?
What error are you getting?
Are you getting an error?

So many questions because of so little information supplied.
 
Upvote 0
Both statements are trying to do the same time but none works.

It says Syntax error.

I want basically to make a cell contain some text ("This is some text") followed by the return from a formula.

How can I do that?
 
Upvote 0
In your second VBA formula you need to encompass ALL strings in double double qoutes
So...

instead of
"This is some text"
use
""This is some text""

Do the same for the other text entries so Excel can determine what is part of the "formula" and what is text
 
Upvote 0
Put the formula in a cell & check that it works.
Then start the macro recorder, select the cell with the formula, hit F2, stop the recorder & you have the code.
 
Upvote 0
Change your first formula to

Range("A1").Formula = "=""This is some text "",TEXT((COUNTIFS(UDE!$AC:$AC,""Low"",UDE!$G:$G,""High"")+COUNTIFS(UDE!$AC:$AC,""4AMLD"",UDE!$G:$G,""High""))/COUNTIF(UDE!$G:$G,""High""),""00%""))"

Again double quotes around text.

The proper format for storing a formula in a cell via VBA is

Destination.formula = "=formula"

UPDATE: I don't know if this works, but it cures the syntax errors that you originally had in both.
 
Last edited:
Upvote 0
I still get Syntax error.

Can you write it for me or give me an example?
 
Upvote 0
I put in everything double quotes and now it puts the whole thing in A1! Like the " Range("A1").Formula = "=CONCATENATE(""This is some..........."
 
Upvote 0
Does this work?

Range("A1").Formula = "=""This is some text ""&(COUNTIFS(UDE!$AC:$AC,""Low"",UDE!$G:$G,""High"")+COUNTIFS(UDE!$AC:$AC,""4AMLD"",UDE!$G:$G,""High""))/COUNTIF(UDE!$G:$G,""High"")"

I don't have any data so am getting a DIV/0 error.


CONCATENATE( ) parameters are separated by commas.

But if you're not using the CONCATENATE function as in your first formula then you need ampersand not comma.
Like these

="ABCDE" & "FGHIJ"

=CONCATENATE("ABCDE" , "FGHIJ")

Spaces inserted for clarity.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top