Sunteți pe pagina 1din 4

3/3/2016

ExcelVBALoopEasyExcelMacros

ExcelEasy
#1Exceltutorialonthenet

Loop

Follow

Excel

Introduction

Basics

Functions

DataAnalysis

15k

VBA

300Examples

Askus

SearchEntireSite

Go

SingleLoop|DoubleLoop|TripleLoop|DoWhileLoop
Loopingisoneofthemostpowerfulprogrammingtechniques.AloopinExcelVBAenablesyoutoloopthrough
arangeofcellswithjustafewcodeslines.

SingleLoop
Youcanuseasinglelooptoloopthroughaonedimensionalrangeofcells.
Placeacommandbuttononyourworksheetandaddthefollowingcodelines:

Chapter<>

DimiAsInteger
Loop
Fori=1To6
Cells(i,1).Value=100
Nexti

Learnmore,it'seasy
LoopthroughDefinedRange
LoopthroughEntireColumn
DoUntilLoop
StepKeyword
CreateaPattern
SortNumbers
RandomlySortData

Resultwhenyouclickthecommandbuttononthesheet:

RemoveDuplicates
ComplexCalculations
KnapsackProblem

DownloadExcelFile
loop.xls

FollowExcelEasy

Explanation:ThecodelinesbetweenForandNextwillbeexecutedsixtimes.Fori=1,ExcelVBAentersthe
value100intothecellattheintersectionofrow1andcolumn1.WhenExcelVBAreachesNexti,itincreasesi
with 1 and jumps back to the For statement. For i = 2, Excel VBA enters the value 100 into the cell at the
intersectionofrow2andcolumn1,etc.
Note:itisgoodpracticetoalwaysindent(tab)thecodebetweenthewordsForandNext.Thismakesyourcode
easiertoread.

DoubleLoop
Youcanuseadoublelooptoloopthroughatwodimensionalrangeofcells.
Placeacommandbuttononyourworksheetandaddthefollowingcodelines:
DimiAsInteger,jAsInteger
Fori=1To6
Forj=1To2
Cells(i,j).Value=100
Nextj
Nexti

http://www.exceleasy.com/vba/loop.html

1/4

3/3/2016

ExcelVBALoopEasyExcelMacros

Resultwhenyouclickthecommandbuttononthesheet:

Explanation:Fori=1andj=1,ExcelVBAentersthevalue100intothecellattheintersectionofrow1and
column1.WhenExcelVBAreachesNextj,itincreasesjwith1andjumpsbacktotheForjstatement.Fori=1
andj=2,ExcelVBAentersthevalue100intothecellattheintersectionofrow1andcolumn2.Next,Excel
VBAignoresNextjbecausejonlyrunsfrom1to2.WhenExcelVBAreachesNexti,itincreasesiwith1and
jumps back to the For i statement. For i = 2 and j = 1, Excel VBA enters the value 100 into the cell at the
intersectionofrow2andcolumn1,etc.

TripleLoop
YoucanuseatriplelooptoloopthroughtwodimensionalrangesonmultipleExcelworksheets.
Placeacommandbuttononyourworksheetandaddthefollowingcodelines:
DimcAsInteger,iAsInteger,jAsInteger
Forc=1To3
Fori=1To6
Forj=1To2
Worksheets(c).Cells(i,j).Value=100
Nextj
Nexti
Nextc
Explanation:Theonlychangemadecomparedtothecodeforthedoubleloopisthatwehaveaddedonemore
loopandaddedWorksheets(c).infrontofCellstogetthetwodimensionalrangeonthefirstsheetforc=1,the
secondsheetforc=2andthethirdsheetforc=3.DownloadtheExcelfiletoseethisresult.

DoWhileLoop
BesidestheForNextloop,thereareotherloopsinExcelVBA.Forexample,theDoWhileLoop.Codeplaced
betweenDoWhileandLoopwillberepeatedaslongasthepartafterDoWhileistrue.
1.Placeacommandbuttononyourworksheetandaddthefollowingcodelines:
DimiAsInteger
i=1
DoWhilei<6
Cells(i,1).Value=20
i=i+1
Loop
Resultwhenyouclickthecommandbuttononthesheet:

http://www.exceleasy.com/vba/loop.html

2/4

3/3/2016

ExcelVBALoopEasyExcelMacros

Explanation:aslongasiislowerthan6,ExcelVBAentersthevalue20intothecellattheintersectionofrowi
andcolumn1andincrementsiby1.InExcelVBA(andinotherprogramminglanguages),thesymbol'='means
becomes.Itdoesnotmeanequal.Soi=i+1meansibecomesi+1.Inotherwords:takethepresentvalueofi
andadd1toit.Forexample,ifi=1,ibecomes1+1=2.Asaresult,thevalue20willbeplacedintocolumnA
fivetimes(notsixbecauseExcelVBAstopswheniequals6).
2.EntersomenumbersincolumnA.

3.Placeacommandbuttononyourworksheetandaddthefollowingcodelines:
DimiAsInteger
i=1
DoWhileCells(i,1).Value<>""
Cells(i,2).Value=Cells(i,1).Value+10
i=i+1
Loop
Resultwhenyouclickthecommandbuttononthesheet:

Explanation:aslongasCells(i,1).Valueisnotempty(<>meansnotequalto),ExcelVBAentersthevalueinto
thecellattheintersectionofrowiandcolumn2,thatis10higherthanthevalueinthecellattheintersectionof
rowiandcolumn1.ExcelVBAstopswheniequals7becauseCells(7,1).Valueisempty.Thisisagreatwayto
loopthroughanynumberofrowsonaworksheet.

Doyoulikethisfreewebsite?PleasesharethispageonGoogle+
Share

23

(1/11)Learnmoreaboutloops,usethesidebar>
GotoTop:Loop|GotoNextChapter:MacroErrors

http://www.exceleasy.com/vba/loop.html

3/4

3/3/2016

ExcelVBALoopEasyExcelMacros
COPYRIGHT(C)20102016WWW.EXCELEASY.COM.EXCELVBALOOP.ALLRIGHTSRESERVED.
EXCEL2010TUTORIAL|HOWTOUSEEXCEL|MICROSOFTEXCEL2010|EXCELVBA

http://www.exceleasy.com/vba/loop.html

4/4

S-ar putea să vă placă și