Modified code not working, as expected

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I added the indirect function to a formula in VBA, but the macro stops at the 'activecell.formula' line below.

I suspect that it's because the VBA editor doesn't like one of the inverted commas?

Can anyone spot the issue here, please?

The sample data this runs on is below:

Sheet 1 has two columns, with data starting in A1, as per below:

[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]Number[/TD]
[TD="width: 64"]Count[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD="align: right"]0[/TD]
[/TR]
</tbody>[/TABLE]

Sheet 2 has one column, with data starting in A1, as per below.

[TABLE="width: 64"]
<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64, align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
</tbody>[/TABLE]
The macro should insert a count if into cell B2 of Sheet 1, then drag it down.

TIA


Code:
Sub Macro2()'Populate Data


Range("b2").Select
ActiveCell.Formula = "=COUNTIF(INDIRECT(""'Sheet2'!A:A"",'Sheet1'!A1)"


Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=COUNTIF(INDIRECT(""'Sheet2'!A:A"",'Sheet1'!A1)"


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The formula is not valid, you have more ( than you have )
 
Upvote 0
Thanks for the response, Fluff.

I'm not sure what you meant by your answer, though?

Did you mean more inverted commas?
 
Last edited:
Upvote 0
The best way to check a formula is to put it into a cell first, that way you can get the correct formula.
 
Upvote 0
I'd originally read your post in "normal" rather than "VBA" mode way, so I was expecting a word after the word "more" and was also expecting a word after the word "have," so that's the reason I didn't understand it, at first. But thank you for pointing the error out. It's appreciated.
 
Upvote 0
Must admit looking back at my post it is not very clear. Sorry for that.

Have you now got it sorted?
 
Upvote 0
Not yet - I added in a closed bracket at the end ie ")" as you correctly pointed out, but the formula is still giving me an error?
 
Upvote 0
The extra bracket does not go at the end. Did you try writing the formula in a cell?
 
Upvote 0
Yes - this is what I tested in the cell.

=COUNTIF(INDIRECT(""'Sheet2'!A:A"",'Sheet1'!A1))

In the VBA code, I'll ensure that the additional closed bracket ie ")" is put before the last quotation mark.

Not sure why the formula is not working, though? Apologies - this is only the second time that I'm using the indirect formula - it's not one that I'm familiar with....

Any further insight would be greatly appreciated.

Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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