VBA/Excel/Access/Word/Excel/Range Loop
Версия от 16:33, 26 мая 2010; (обсуждение)
Содержание
For Each Loops for a range
Sub forEach()
Dim c As Range
For Each c In Range("A1:E10")
c.Value = c.Row * c.Column
Next
End Sub
Get rows from a named range (name the range before running this cript)
Sub Bold()
Dim rngRow As Range
For Each rngRow In Range("yourRange").Rows
If rngRow.Cells(1).Value > 1 Then
rngRow.Font.Bold = True
Else
rngRow.Font.Bold = False
End If
Next rngRow
End Sub
Get Rows from a range
Sub Bold()
Dim rngRow As Range
For Each rngRow In Range("A1:E5").Rows
If rngRow.Cells(1).Value > 1 Then
rngRow.Font.Bold = True
Else
rngRow.Font.Bold = False
End If
Next rngRow
End Sub
Loop through all cells in a range
Sub ResetTest1()
For Each n In Range("A1:A13") " Substitute your range here
If n.Value <> 0 Then
n.Value = 0
End If
Next n
End Sub
Loop through a range cell by cell
Sub ColorCells1()
Dim myRange As Range
Dim i As Long, j As Long
Set myRange = Range("A1:A5")
For i = 1 To myRange.Rows.Count
For j = 1 To myRange.Columns.Count
If myRange.Cells(i, j).Value < 100 Then
myRange.Cells(i, j).Font.ColorIndex = 3
Else
myRange.Cells(i, j).Font.ColorIndex = 1
End If
Next j
Next i
End Sub
Set the value in a range by cell index
Sub ColorCells2()
Dim myRange As Range
Dim i As Long, j As Long
Set myRange = Range("A1:E5")
For i = 1 To myRange.Rows.Count
For j = 1 To myRange.Columns.Count
If myRange(i, j).Value < 10 Then
myRange(i, j).Font.ColorIndex = 5
Else
myRange(i, j).Font.ColorIndex = 1
End If
Next j
Next i
End Sub