'-----------------------------------------------------------------------
'Name: Code - Alternative For Each Loops.xlsm
'Date: 2013-07-08
'Module: Mod_01_Procedures
'Source:
'Comments: The range "Data" is B3:D5 and contains the following values:
' 1 2 3
' 4 5 6
' 7 8 9
'-----------------------------------------------------------------------
'=========================================
'Variables for Range processing procedures
'=========================================
Public MyRange As Range
Public MyRow As Range
Public MyCol As Range
Public MyCell As Range
Public MyRowTotal As Long
Public MyRowCount As Long
Public MyColTotal As Long
Public MyColCount As Long
Public MyCellTotal As Long
Public MyCellCount As Long
Sub ProcessRange01_RowAscColAsc() 'returns 1,2,3,4,5,6,7,8,9 - Left To Right, Top To Bottom
Set MyRange = Range("Data")
For Each MyCell In MyRange
MsgBox ("Address: " & MyCell.Address & Chr(10) & "Value: " & MyCell.Value)
'Do stuff with MyCell
Next
End Sub
Sub ProcessRange02_RowAscColDesc() 'returns 3,2,1,6,5,4,9,8,7 - Right To Left, Top To Bottom
Set MyRange = Range("Data")
For Each MyRow In MyRange.Rows
MyRowTotal = MyRange.Rows.Count
For MyRowCount = MyRowTotal To 1 Step -1
MsgBox ("Address: " & MyRow.Cells(MyRowCount).Address & Chr(10) & "Value: " & _
MyRow.Cells(MyRowCount).Value)
'Do stuff with MyRow.Cells(MyRowCount)
Next
Next
End Sub
Sub ProcessRange03_RowDescColAsc() 'returns 7,8,9,4,5,6,1,2,3 - Left To Right, Bottom To Top
Set MyRange = Range("Data")
MyRowTotal = MyRange.Rows.Count
For MyRowCount = MyRowTotal To 1 Step -1
For Each MyCol In MyRange.Columns
MsgBox ("Address: " & MyCol.Cells(MyRowCount).Address & Chr(10) & _
"Value: " & MyCol.Cells(MyRowCount).Value)
'Do stuff with MyCol.Cells(MyRowCount)
Next
Next
End Sub
Sub ProcessRange04_RowDescColDesc() 'returns 9,8,7,6,5,4,3,2,1 - Right To Left, Bottom To Top
Set MyRange = Range("Data")
MyCellTotal = MyRange.Cells.Count
For MyCellCount = MyCellTotal To 1 Step -1
MsgBox ("Address: " & MyRange.Cells(MyCellCount).Address & Chr(10) & _
"Value: " & MyRange.Cells(MyCellCount).Value)
'Do stuff with MyRange.Cells(MyCellCount)
Next
End Sub
Sub ProcessRange05_ColAscRowAsc() 'returns 1,4,7,2,5,8,3,6,9 - Top To Bottom, Left To Right
Set MyRange = Range("Data")
For Each MyCol In MyRange.Columns
For Each MyCell In MyCol.Cells
MsgBox ("Address: " & MyCell.Address & Chr(10) & "Value: " & MyCell.Value)
'Do stuff with MyCell
Next
Next
End Sub
Sub ProcessRange06_ColAsc_RowDesc() 'returns 7,4,8,8,5,2,9,6,3 - Bottom To Top, Left To Right
Set MyRange = Range("Data")
For Each MyCol In MyRange.Columns
MyCellTotal = MyCol.Cells.Count
For MyCellCount = MyCellTotal To 1 Step -1
MsgBox ("Address: " & MyCol.Cells(MyCellCount).Address & Chr(10) & _
"Value: " & MyCol.Cells(MyCellCount).Formula)
'Do stuff with MyCol.Cells(MyCellCount)
Next
Next
End Sub
Sub ProcessRange07_ColDesc_RowAsc() 'returns 3,6,9,2,5,8,1,4,7 - Top To Bottom, Right To Left
Set MyRange = Range("Data")
MyColTotal = MyRange.Columns.Count
For MyColCount = MyColTotal To 1 Step -1
MyCellTotal = MyRange.Columns(MyColCount).Cells.Count
For MyCellCount = 1 To MyCellTotal
MsgBox ("Address: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Address & Chr(10) & _
"Value: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Formula)
'Do stuff with MyRange.Columns(MyColCount).Cells(MyCellCount)
Next
Next
End Sub
Sub ProcessRange08_ColDesc_RowDesc() 'returns 9,6,3,8,5,2,7,4,1 - Bottom To Top, Right To Left
Set MyRange = Range("Data")
MyColTotal = MyRange.Columns.Count
For MyColCount = MyColTotal To 1 Step -1
MyCellTotal = MyRange.Columns(MyColCount).Cells.Count
For MyCellCount = MyCellTotal To 1 Step -1
MsgBox ("Address: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Address & Chr(10) & _
"Value: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Formula)
'Do stuff with MyRange.Columns(MyColCount).Cells(MyCellCount)
Next
Next
End Sub
'Function to display the formula in a cell
Function ShowFormula(MyRange As Range) As String
ShowFormula = MyRange.Formula
End Function
Welcome to the forum!
Here're a whole bunch of loops to process a range in different directions - I think you need a variation on Sub ProcessRange05_ColAscRowAsc()
Code:'----------------------------------------------------------------------- 'Name: Code - Alternative For Each Loops.xlsm 'Date: 2013-07-08 'Module: Mod_01_Procedures 'Source: 'Comments: The range "Data" is B3:D5 and contains the following values: ' 1 2 3 ' 4 5 6 ' 7 8 9 '----------------------------------------------------------------------- '========================================= 'Variables for Range processing procedures '========================================= Public MyRange As Range Public MyRow As Range Public MyCol As Range Public MyCell As Range Public MyRowTotal As Long Public MyRowCount As Long Public MyColTotal As Long Public MyColCount As Long Public MyCellTotal As Long Public MyCellCount As Long Sub ProcessRange01_RowAscColAsc() 'returns 1,2,3,4,5,6,7,8,9 - Left To Right, Top To Bottom Set MyRange = Range("Data") For Each MyCell In MyRange MsgBox ("Address: " & MyCell.Address & Chr(10) & "Value: " & MyCell.Value) 'Do stuff with MyCell Next End Sub Sub ProcessRange02_RowAscColDesc() 'returns 3,2,1,6,5,4,9,8,7 - Right To Left, Top To Bottom Set MyRange = Range("Data") For Each MyRow In MyRange.Rows MyRowTotal = MyRange.Rows.Count For MyRowCount = MyRowTotal To 1 Step -1 MsgBox ("Address: " & MyRow.Cells(MyRowCount).Address & Chr(10) & "Value: " & _ MyRow.Cells(MyRowCount).Value) 'Do stuff with MyRow.Cells(MyRowCount) Next Next End Sub Sub ProcessRange03_RowDescColAsc() 'returns 7,8,9,4,5,6,1,2,3 - Left To Right, Bottom To Top Set MyRange = Range("Data") MyRowTotal = MyRange.Rows.Count For MyRowCount = MyRowTotal To 1 Step -1 For Each MyCol In MyRange.Columns MsgBox ("Address: " & MyCol.Cells(MyRowCount).Address & Chr(10) & _ "Value: " & MyCol.Cells(MyRowCount).Value) 'Do stuff with MyCol.Cells(MyRowCount) Next Next End Sub Sub ProcessRange04_RowDescColDesc() 'returns 9,8,7,6,5,4,3,2,1 - Right To Left, Bottom To Top Set MyRange = Range("Data") MyCellTotal = MyRange.Cells.Count For MyCellCount = MyCellTotal To 1 Step -1 MsgBox ("Address: " & MyRange.Cells(MyCellCount).Address & Chr(10) & _ "Value: " & MyRange.Cells(MyCellCount).Value) 'Do stuff with MyRange.Cells(MyCellCount) Next End Sub Sub ProcessRange05_ColAscRowAsc() 'returns 1,4,7,2,5,8,3,6,9 - Top To Bottom, Left To Right Set MyRange = Range("Data") For Each MyCol In MyRange.Columns For Each MyCell In MyCol.Cells MsgBox ("Address: " & MyCell.Address & Chr(10) & "Value: " & MyCell.Value) 'Do stuff with MyCell Next Next End Sub Sub ProcessRange06_ColAsc_RowDesc() 'returns 7,4,8,8,5,2,9,6,3 - Bottom To Top, Left To Right Set MyRange = Range("Data") For Each MyCol In MyRange.Columns MyCellTotal = MyCol.Cells.Count For MyCellCount = MyCellTotal To 1 Step -1 MsgBox ("Address: " & MyCol.Cells(MyCellCount).Address & Chr(10) & _ "Value: " & MyCol.Cells(MyCellCount).Formula) 'Do stuff with MyCol.Cells(MyCellCount) Next Next End Sub Sub ProcessRange07_ColDesc_RowAsc() 'returns 3,6,9,2,5,8,1,4,7 - Top To Bottom, Right To Left Set MyRange = Range("Data") MyColTotal = MyRange.Columns.Count For MyColCount = MyColTotal To 1 Step -1 MyCellTotal = MyRange.Columns(MyColCount).Cells.Count For MyCellCount = 1 To MyCellTotal MsgBox ("Address: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Address & Chr(10) & _ "Value: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Formula) 'Do stuff with MyRange.Columns(MyColCount).Cells(MyCellCount) Next Next End Sub Sub ProcessRange08_ColDesc_RowDesc() 'returns 9,6,3,8,5,2,7,4,1 - Bottom To Top, Right To Left Set MyRange = Range("Data") MyColTotal = MyRange.Columns.Count For MyColCount = MyColTotal To 1 Step -1 MyCellTotal = MyRange.Columns(MyColCount).Cells.Count For MyCellCount = MyCellTotal To 1 Step -1 MsgBox ("Address: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Address & Chr(10) & _ "Value: " & MyRange.Columns(MyColCount).Cells(MyCellCount).Formula) 'Do stuff with MyRange.Columns(MyColCount).Cells(MyCellCount) Next Next End Sub 'Function to display the formula in a cell Function ShowFormula(MyRange As Range) As String ShowFormula = MyRange.Formula End Function
Hope this helps
Pete
loopAwesome ****!"
solved!!!!!
thanks cheers.