Betrifft: Selectbefehl vereinfachen
von: Bruehmi
Geschrieben am: 15.10.2008 12:25:45
Hallo,
habe durch eure Hilfe schon einiges hier hinbekommen. Dafür nochmal ein dickes Lob von mir!
Nun stehe ich vor folgendem Rätsel:
Ich möchte verschiedene Bereiche formatieren...
Private Sub Spalten_mit_Formeln_formatieren() Range("E20:E" & Range("E7").Value + 20).Select With Selection.Interior .ColorIndex = 33 End With Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With Range("H20:H" & Range("E7").Value + 20).Select With Selection.Interior .ColorIndex = 33 End With Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With
... dies sind "nur" 2 der 4 Bereiche, jedoch möchte ich den Code, ein wenig sauberer, also kürzer haben. Gibt es eine Möglichkeit, die 4 Zellbereiche in ein Select zu bringen?
In etwa so:
Range(("E20:E" & Range("E7").Value + 20), ("I20:I" & Range("E7").Value + 20).Select
Gruß Bruehmi
Betrifft: AW: Selectbefehl vereinfachen
von: Beverly
Geschrieben am: 15.10.2008 12:38:43
Hi,
Sub format() With Union(Range("E20:E" & Range("E7").Value + 20), Range("H20:H" & Range("E7").Value + 20)) .Interior.ColorIndex = 33 With .Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium End With With .Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium End With With .Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium End With With .Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium End With End With End Sub
Betrifft: AW: Selectbefehl vereinfachen
von: Bruehmi
Geschrieben am: 15.10.2008 12:56:05
Funktioniert super, Danke!
Betrifft: AW: noch einfacher
von: Daniel
Geschrieben am: 15.10.2008 21:42:33
Hi
es geht in diesem speziellen Fall noch einfacher:
für die .LineStyle -Eigenschaft ist der Wert xlContinuous der Default-Wert.
Default-Werte werden immer dann automatisch verwendet. wenn nichts anderes angegeben ist, daher kann man das Setzen auf einen Default-Wert im Code oft weglassen und in diesem Fall so vereinfachen:
Sub format() With Union(Range("E20:E" & Range("E7").Value + 20), Range("H20:H" & Range("E7").Value + 20)) _ .Interior.ColorIndex = 33 .Borders(xlEdgeLeft).Weight = xlMedium .Borders(xlEdgeTop).Weight = xlMedium .Borders(xlEdgeBottom).Weight = xlMedium .Borders(xlEdgeRight).Weight = xlMedium End With End Sub
Eine weitere Vereinfachung ergibt sich daraus, daß "xlEdgeLeft" eine Excel-Konstante ist, die dem Wert 7 entspricht, die anderen "xlEdge..."-Konstanten den Werten 8 bis 10.
Aus dieser Tatsache heraus lässt sich noch folgende Verkürzung des Codes ableiten:
Sub format() Dim i As Long With Union(Range("E20:E" & Range("E7").Value + 20), Range("H20:H" & Range("E7").Value + 20)) _ .Interior.ColorIndex = 33 For i = 7 To 10 .Borders(i).Weight = xlMedium Next End With End Sub
Gruß, Daniel