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:

NumberCount
12
21
30

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>

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

1
1
2

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>
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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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