Countif after columns have been deleted

Mr2017

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

Is it possible to have a formula in a sheet that looks at another sheet where columns have been re-arranged and deleted by VBA?

To add some context, let's assume the data below is in column B of Sheet 1

A
B
C
A


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

Then Sheet 2 has the following data in columns A and B

A2
B1
C1
A2

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


The data in column B of Sheet 2 is derived from this formula (in cell B1): =COUNTIF(Sheet1!B:B,Sheet2!A1)

The problem I have is that data in Sheet 1 is obtained from another Sheet using VBA and the code deletes and re-arranges columns everytime it puts data into Sheet 1 so that the columns are in a certain order.

This then throws out the countif formula in Sheet 2 which then has a REF error.

How can I make the formula in Sheet 2 still check the right column in Sheet 1 without getting a REF error after the code is run?

NB Out of approx. 20 columns in Sheet 1, two columns in the middle are cut then pasted at the end, which is what I believe may be causing the REF error.

Thanks in advance.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If you know that the data you're looking for will always end up in column B, you can do something like:

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

If it may end up somewhere else, but you can identify the right column by a specific heading, there are other options.
 
Upvote 0
Hi Eric

Apologies for the delay in responding.

But thank you for posting your solution!
 
Upvote 0
Hi Eric

Do you know how to amend the formula if Sheet 1 is given a name eg "orig data"?

I tried this, but it didn't work...?

=COUNTIF(INDIRECT("orig data!B:B"),Sheet2!A1)
 
Upvote 0
If the sheet name has spaces in it, you need to put single quotes around the name, like this:

=COUNTIF(INDIRECT("'orig data'!B:B"),Sheet2!A1)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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