Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

excel - Consolidation of dynamically changing data in VBA

I work with data including unique names of customers and values of their purchases. I want to conduct its consolidation, meaning the result would be customers and their total sums of purchase like in the picture below:

enter image description here

The data is changing after every VBA code operation (new customers come in, old disappear, new transactions are recorded etc.) which somehow leads to this line of code generating errors:

Sheets("Sheet1").Range(D2).Consolidate Sources:=Array(Sheet1!R2C1:R3C51), Function:=xlSum

Is there a different approach to this situation so that there's no problem with getting the right results?

question from:https://stackoverflow.com/questions/65892479/consolidation-of-dynamically-changing-data-in-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use excel formulas instead of vba.

Fomula for D2:

=IFERROR(INDEX($A$3:$A$7, MATCH(0,COUNTIF($D$1:D1, $A$3:$A$7), 0)),"")

and hit CTR+SHIFT+ENTER

Then use sumif in E2:

=SUMIF($A$2:$A$7, D2, $B$2:$B$7)

Drop the cell down.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...