FOR Loops Syntax in VBA

Here MS Excel related topics will be posted.
Post Reply
admin
Posts: 2062
Joined: Fri Mar 31, 2006 12:59 am
Location: Pakistan
Contact:

FOR Loops Syntax in VBA

Post by admin »

-- Single For Loop
Dim x As Integer

For x = 1 To 10
Cells(x, 1).Value = x
-- Exit For (can be used to exit)
Next x

-- 2 For loops

Dim x As Integer
Dim y As Integer

For x = 1 To 3
For y= 1 To 10
Cells(y, x).Value = y &"-"& x
Next y
Next x

-- 3 For loops

Dim x As Integer
Dim y As Integer
Dim z As Integer

For z = 3 To 6
For x = 1 To 3
For y = 1 To 10

Worksheets(z).Cells(y , x).Value = "ERPstuff"

Next y
Next x
Next z

-- For Each Loop

Dim ws As Worksheet

For Each ws In Worksheets
MsgBox ws.Name
Next ws
Malik Sikandar Hayat
admin@erpstuff.com
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests