VBA Help

Knightson

New Member
Joined
Jul 17, 2013
Messages
2
All, I completed the below Excel VBA code and it successfully works. However I know it’s incredibly inefficient. Can one of you VBA Gurus please show me a simplified way of writing the below loop code?

Thanks in Advance</SPAN>

Sub loops()
x = 1
Dim ZERO As Integer
Dim ONE As Integer '0100
Dim TWO As Integer '0200
Dim THREE As Integer '0300
Dim FOUR As Integer '0400
Dim FIVE As Integer '0500
Dim SIX As Integer '0600
Dim SEVEN As Integer '0700
Dim EIGHT As Integer '0800
Dim NINE As Integer '0900
Dim TEN As Integer '1000
Dim ELEVEN As Integer '1100
Dim TWELVE As Integer '1200
Dim THIRTEEN As Integer '1300
Dim FOURTEEN As Integer '1400
Dim FIFTEEN As Integer '1500
Dim SIXTEEN As Integer '1600
Dim SEVENTEEN As Integer '1700
Dim EIGHTTEEN As Integer '1800
Dim NINETEEN As Integer '1900
Dim TWENTY As Integer '2000
Dim TWENTYONE As Integer '2100
Dim TWENTYTWO As Integer '2200
Dim TWENTYTHREE As Integer '2300
Dim TWENTYFOUR As Integer '2400

Do While Cells(x, 2).Value <> ""
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value = 0 And Cells(x, 3).Value >= 0) Then ZERO = ZERO + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 1 And Cells(x, 3).Value >= 1) Then ONE = ONE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 2 And Cells(x, 3).Value >= 2) Then TWO = TWO + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 3 And Cells(x, 3).Value >= 3) Then THREE = THREE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 4 And Cells(x, 3).Value >= 4) Then FOUR = FOUR + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 5 And Cells(x, 3).Value >= 5) Then FIVE = FIVE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 6 And Cells(x, 3).Value >= 6) Then SIX = SIX + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 7 And Cells(x, 3).Value >= 7) Then SEVEN = SEVEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 8 And Cells(x, 3).Value >= 8) Then EIGHT = EIGHT + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 9 And Cells(x, 3).Value >= 9) Then NINE = NINE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 10 And Cells(x, 3).Value >= 10) Then TEN = TEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 11 And Cells(x, 3).Value >= 11) Then ELEVEN = ELEVEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 12 And Cells(x, 3).Value >= 12) Then TWELVE = TWELVE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 13 And Cells(x, 3).Value >= 13) Then THIRTEEN = THIRTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 14 And Cells(x, 3).Value >= 14) Then FOURTEEN = FOURTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 15 And Cells(x, 3).Value >= 15) Then FIFTEEN = FIFTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 16 And Cells(x, 3).Value >= 16) Then SIXTEEN = SIXTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 17 And Cells(x, 3).Value >= 17) Then SEVENTEEN = SEVENTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 18 And Cells(x, 3).Value >= 18) Then EIGHTTEEN = EIGHTTEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 19 And Cells(x, 3).Value >= 19) Then NINETEEN = NINETEEN + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 20 And Cells(x, 3).Value >= 20) Then TWENTY = TWENTY + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 21 And Cells(x, 3).Value >= 21) Then TWENTYONE = TWENTYONE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 22 And Cells(x, 3).Value >= 22) Then TWENTYTWO = TWENTYTWO + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 23 And Cells(x, 3).Value >= 23) Then TWENTYTHREE = TWENTYTHREE + 1
If Cells(x, 1).Value Like "*NMC*" And (Cells(x, 2).Value <= 24 And Cells(x, 3).Value >= 24) Then TWENTYFOUR = TWENTYFOUR + 1

x = x + 1
Loop
Cells(1, 8).Value = ZERO
Cells(2, 8).Value = ONE
Cells(3, 8).Value = TWO
Cells(4, 8).Value = THREE
Cells(5, 8).Value = FOUR
Cells(6, 8).Value = FIVE
Cells(7, 8).Value = SIX
Cells(8, 8).Value = SEVEN
Cells(9, 8).Value = EIGHT
Cells(10, 8).Value = NINE
Cells(11, 8).Value = TEN
Cells(12, 8).Value = ELEVEN
Cells(13, 8).Value = TWELVE
Cells(14, 8).Value = THIRTEEN
Cells(15, 8).Value = FOURTEEN
Cells(16, 8).Value = FIFTEEN
Cells(17, 8).Value = SIXTEEN
Cells(18, 8).Value = SEVENTEEN
Cells(19, 8).Value = EIGHTTEEN
Cells(20, 8).Value = NINETEEN
Cells(21, 8).Value = TWENTY
Cells(22, 8).Value = TWENTYONE
Cells(23, 8).Value = TWENTYTWO
Cells(24, 8).Value = TWENTYTHREE
Cells(25, 8).Value = TWENTYFOUR

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Negative, they are placeholders for each of the data items, so that I can perform a function and it stores the result.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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