VBA Q- Entering Formula through recorder!

aborg88

New Member
Joined
Jul 6, 2011
Messages
31
Hi All,

Seeking some excel Guru help!

I'm pretty new to VBA, but have a lot of experience using excel.

I have a sheet with two sheets;

1. PASTE
2. ANALYSIS

PASTE LOOKS LIKE THIS -

Resolved DateCountry inbox
03/06/2013Info-it
03/06/2013Info-it
03/06/2013Info-no
03/06/2013Info-fi
03/06/2013Info-no
03/06/2013Info-dk
03/06/2013Info-uk
03/06/2013Info-nl
03/06/2013Info-bg
03/06/2013Info-se
03/06/2013Info-no
03/06/2013Info-nl
03/06/2013Info-se
03/06/2013Info-dk
03/06/2013Info-nl
03/06/2013Info-se
03/06/2013Info-dk
03/06/2013Info-no
03/06/2013Info-nl
03/06/2013Info-it
03/06/2013Info-no
03/06/2013Info-no
03/06/2013Info-se
03/06/2013Info-se
03/06/2013Info-it
03/06/2013Info-pl
03/06/2013Info-no
03/06/2013Info-nl
03/06/2013Info-it
03/06/2013Info-uk
03/06/2013Info-it
03/06/2013Info-pl

<colgroup><col span="2"></colgroup><tbody>
</tbody>

Now in reality the sheet has more rows and more columns, however for this example i only need this,

In Analysis tab there are
DATE
03/06/2013
04/06/2013
05/06/2013
06/06/2013
07/06/2013
08/06/2013
09/06/2013

Dates,

Now in B2 I have a rather complicated SUM, using COUNTIFS, which only take corresponding countries for the date;

=SUM(COUNTIFS(PASTE!B:B,"Info-no",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-fi",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-uk",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-nl",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-bg",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-se",PASTE!A:A,ANALYSIS!A2),COUNTIFS(PASTE!B:B,"Info-pl",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-de",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-hu",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-lt",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-ru",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-pt",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-cz",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-gr",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-lv",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-ro",PASTE!A:A,ANALYSIS!A2), COUNTIFS(PASTE!B:B,"Info-fr",PASTE!A:A,ANALYSIS!A2))

This sum works fine when pasted and dragged down. :LOL:

My problem!!

If i record a macro and paste this into recorder and run the macro i get an;

application-defined or object-defined error

!! WHY?! Can anyone please help!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
When you place in in a module, you need to 'double up' on the double quotes (that surround your country value) so that VBA does not interpret them as string terminators:

Rich (BB code):
Range("A1").Formula = "=SUM(COUNTIFS(PASTE!B:B,""Info-no"",PASTE!A:A,ANALYSIS!A2), .....

Additions are in red.
 
Upvote 0
My apologies, this does not do the trick, the formula when pasted through recorder auto- double quotes everything.
 
Last edited:
Upvote 0
In fact the VBA code through recorder translates to this --

Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=SUM(COUNTIFS(PASTE!C,""Info-no"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-fi"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-uk"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-nl"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-bg"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-se"",PASTE!C[-1],ANALYS" & _
"C[-1]),COUNTIFS(PASTE!C,""Info-pl"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-de"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-hu"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-lt"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-ru"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-pt"",PASTE!C[-1],ANALY" & _
"C[-1]), COUNTIFS(PASTE!C,""Info-cz"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-gr"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-lv"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-ro"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]), COUNTIFS(PASTE!C,""Info-fr"",PASTE!C[-1],ANALYSIS!R[-1]C[-1]))"
Range("B4").Select
 
Upvote 0
I suspect your formula string is exceeding the formula length limit - try this instead of your current formula line of code:

Code:
Activecell.Formula = "=SUM(COUNTIFS(Paste!B:B,{""Info-bg"",""Info-dk"",""Info-fi"",""Info-it"",""Info-nl"",""Info-no"",""Info-p"l",""Info-ru"",""Info-se"",""Info-uk"",""Info-cz"",""Info-lv"",""Info-fr"",""Info-ro"",""Info-gr"",""Info-pt"",""Info-de"",""Info-hu""},Paste!A:A,Analysis!A2))"
 
Upvote 0
I suspect your formula string is exceeding the formula length limit - try this instead of your current formula line of code:

Code:
Activecell.Formula = "=SUM(COUNTIFS(Paste!B:B,{""Info-bg"",""Info-dk"",""Info-fi"",""Info-it"",""Info-nl"",""Info-no"",""Info-p"l",""Info-ru"",""Info-se"",""Info-uk"",""Info-cz"",""Info-lv"",""Info-fr"",""Info-ro"",""Info-gr"",""Info-pt"",""Info-de"",""Info-hu""},Paste!A:A,Analysis!A2))"


PERFECT!! Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,853
Members
449,051
Latest member
excelquestion515

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