Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write Select Case statement in VBA more briefly

Is it possible to write the following code in VBA Excel 2016 more briefly?

Select Case NemberOfProduct(i)
    Case 1
    CounterPlot(1) = CounterPlot(1) + 1
    SumPureRate(1) = SumPureRate(1) + H(i)
    Case 2
    CounterPlot(2) = CounterPlot(2) + 1
    SumPureRate(2) = SumPureRate(2) + H(i)
    Case 3
    CounterPlot(3) = CounterPlot(3) + 1
    SumPureRate(3) = SumPureRate(3) + H(i)
.
.
   Case 12
    CounterPlot(12) = CounterPlot(12) + 1
    SumPureRate(12) = SumPureRate(12) + H(i)
    
End Select

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could either manualy use NemberOfProduct(i) expresion as value.
So your whole select case could be replaced with

CounterPlot(NemberOfProduct(i)) = CounterPlot(NemberOfProduct(i)) + 1
SumPureRate(NemberOfProduct(i)) = SumPureRate(NemberOfProduct(i)) + H(i)

Or even more simplify it by storing NemberOfProduct(i) inside variable and then use it.

Dim n as Integer
n = NemberOfProduct(i)
CounterPlot(n) = CounterPlot(n) + 1
SumPureRate(n) = SumPureRate(n) + H(i)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading