Beste VBA-ers,
In een Excel sheet wil ik grote hoeveelheden cellen in één handeling opmaken.
Dit lukt mij weliswaar met de huidige code (zie verder), maar het duurt een halve minuut op mijn pc voordat het klusje geklaard is.
Het is mij bekend, dat je in dit soort gevallen het grote bereik als variant moet declareren om gegevens van en naar het werkblad over te zetten.
Dit zou 'tig' keer sneller gaan, maar.........., het wil mij niet lukken.
Kan iemand voor mij bijgaande code verbeteren?
Groetjes van Franzeman
----------------------------------------------------------------------------------------------------
'Geef op een leeg tabblad voor de cellen A1, B1, C1, D1 en E1 resp. de waarden: a, b, c, d en e in.
'Geef vervolgens willekeurig in het bereik A2:Z2000 dezelfde letters in en start dan de volgende macro:
Sub Macro1()
'
' Macro1 Macro
Dim Cell As Range
Dim Row As Long
Dim i As Integer
Application.ScreenUpdating = False
Range("A2:Z2000").Select
'blad opmaken
For Each Cell In Selection
If Cell.Value = "" Or 0 Then
Cell.Interior.ColorIndex = xlNone
Cell.FormatConditions.Delete
Cell.Interior.Pattern = xlSolid
Cell.FormatConditions.Add Type:=xlExpression, Formula1:="=REST(RIJ();2)=0"
Cell.FormatConditions(1).Interior.ColorIndex = 34
Else 'voor de cellen A1, B1, C1, D1 en E1 voor resp. de waarden: a-b-c-d-e
If Cell.Value > 0 Then Cell.FormatConditions.Delete
If Cell.Value = Range("A1") Then Cell.Interior.ColorIndex = 14
If Cell.Value = Range("A1") Then Cell.Font.Bold = True
If Cell.Value = Range("A1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("B1") Then Cell.Interior.ColorIndex = 5
If Cell.Value = Range("B1") Then Cell.Font.Bold = True
If Cell.Value = Range("B1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("C1") Then Cell.Interior.ColorIndex = 46
If Cell.Value = Range("C1") Then Cell.Font.Bold = True
If Cell.Value = Range("C1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("D1") Then Cell.Interior.ColorIndex = 12
If Cell.Value = Range("D1") Then Cell.Font.Bold = True
If Cell.Value = Range("D1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("E1") Then Cell.Borders(xlDiagonalDown).LineStyle = xlContinuous
If Cell.Value = Range("E1") Then Cell.Borders(xlDiagonalUp).LineStyle = xlContinuous
If Cell.Value = Range("E1") Then Cell.Interior.ColorIndex = 6
End If
Next Cell
Range("A1").Select
Application.ScreenUpdating = True
End Sub
In een Excel sheet wil ik grote hoeveelheden cellen in één handeling opmaken.
Dit lukt mij weliswaar met de huidige code (zie verder), maar het duurt een halve minuut op mijn pc voordat het klusje geklaard is.
Het is mij bekend, dat je in dit soort gevallen het grote bereik als variant moet declareren om gegevens van en naar het werkblad over te zetten.
Dit zou 'tig' keer sneller gaan, maar.........., het wil mij niet lukken.
Kan iemand voor mij bijgaande code verbeteren?
Groetjes van Franzeman
----------------------------------------------------------------------------------------------------
'Geef op een leeg tabblad voor de cellen A1, B1, C1, D1 en E1 resp. de waarden: a, b, c, d en e in.
'Geef vervolgens willekeurig in het bereik A2:Z2000 dezelfde letters in en start dan de volgende macro:
Sub Macro1()
'
' Macro1 Macro
Dim Cell As Range
Dim Row As Long
Dim i As Integer
Application.ScreenUpdating = False
Range("A2:Z2000").Select
'blad opmaken
For Each Cell In Selection
If Cell.Value = "" Or 0 Then
Cell.Interior.ColorIndex = xlNone
Cell.FormatConditions.Delete
Cell.Interior.Pattern = xlSolid
Cell.FormatConditions.Add Type:=xlExpression, Formula1:="=REST(RIJ();2)=0"
Cell.FormatConditions(1).Interior.ColorIndex = 34
Else 'voor de cellen A1, B1, C1, D1 en E1 voor resp. de waarden: a-b-c-d-e
If Cell.Value > 0 Then Cell.FormatConditions.Delete
If Cell.Value = Range("A1") Then Cell.Interior.ColorIndex = 14
If Cell.Value = Range("A1") Then Cell.Font.Bold = True
If Cell.Value = Range("A1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("B1") Then Cell.Interior.ColorIndex = 5
If Cell.Value = Range("B1") Then Cell.Font.Bold = True
If Cell.Value = Range("B1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("C1") Then Cell.Interior.ColorIndex = 46
If Cell.Value = Range("C1") Then Cell.Font.Bold = True
If Cell.Value = Range("C1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("D1") Then Cell.Interior.ColorIndex = 12
If Cell.Value = Range("D1") Then Cell.Font.Bold = True
If Cell.Value = Range("D1") Then Cell.Font.ColorIndex = 2
If Cell.Value = Range("E1") Then Cell.Borders(xlDiagonalDown).LineStyle = xlContinuous
If Cell.Value = Range("E1") Then Cell.Borders(xlDiagonalUp).LineStyle = xlContinuous
If Cell.Value = Range("E1") Then Cell.Interior.ColorIndex = 6
End If
Next Cell
Range("A1").Select
Application.ScreenUpdating = True
End Sub