AW: weitere Spalten
06.10.2005 21:18:37
Erich
Hallo Daniel,
hier eine neue Version. Die verträgt es auch, wenn du weniger als AnzSp Spalten auswerten willst:
Private Sub Bereinigung_Click()
Const AB_ZEILE As Integer = 6
Const AnzSp = 9
Dim rng As Range
Dim c As Range
Dim lngAvg As Integer
Dim ii As Integer, SpW(1 To AnzSp, 1 To 3) As Long
Dim anzOK As Integer, arrOK() As Integer
' ii = ii + 1: SpW(ii, 1) = 6: SpW(ii, 2) = 0: SpW(ii, 3) = 150000
ii = ii + 1: SpW(ii, 1) = 7: SpW(ii, 2) = 0: SpW(ii, 3) = 150000
ii = ii + 1: SpW(ii, 1) = 8: SpW(ii, 2) = 0: SpW(ii, 3) = 160000
ii = ii + 1: SpW(ii, 1) = 11: SpW(ii, 2) = 0: SpW(ii, 3) = 170000
ii = ii + 1: SpW(ii, 1) = 12: SpW(ii, 2) = 0: SpW(ii, 3) = 180000
ii = ii + 1: SpW(ii, 1) = 13: SpW(ii, 2) = 0: SpW(ii, 3) = 190000
ii = ii + 1: SpW(ii, 1) = 14: SpW(ii, 2) = 0: SpW(ii, 3) = 100000
ii = ii + 1: SpW(ii, 1) = 15: SpW(ii, 2) = 0: SpW(ii, 3) = 110000
ii = ii + 1: SpW(ii, 1) = 16: SpW(ii, 2) = 0: SpW(ii, 3) = 120000
With Worksheets("Daten")
For ii = 1 To AnzSp
' --------------- "gute" Werte zählen
If SpW(ii, 1) < 1 Or SpW(ii, 1) >= Rows.Count Then GoTo NXT
If .Cells(Rows.Count, SpW(ii, 1)).End(xlUp).Row <= AB_ZEILE Then _
GoTo NXT
Set rng = .Range(.Cells(AB_ZEILE, SpW(ii, 1)), _
.Cells(Rows.Count, SpW(ii, 1)).End(xlUp))
' ------------ "gute" Werte zählen
anzOK = 0
For Each c In rng.Cells
If Not IsEmpty(c) And SpW(ii, 2) <= c And c <= SpW(ii, 3) Then
anzOK = anzOK + 1
End If
Next c
If anzOK = 0 Then _
MsgBox "Kein zulässiger Wert in Spalte" & Str(SpW(ii, 1)): GoTo NXT
' --------- Array mit "guten" Werten anlegen
ReDim arrOK(1 To anzOK)
anzOK = 0
For Each c In rng.Cells
If Not IsEmpty(c) And SpW(ii, 2) <= c And c <= SpW(ii, 3) Then
anzOK = anzOK + 1
arrOK(anzOK) = c
End If
Next c
' --------- Durchschnitt der "guten" Werte berechnen
lngAvg = WorksheetFunction.Average(arrOK)
' --------- Durchschnitt in die "schlechten" Zellen eintragen
For Each c In rng.Cells
If Not IsEmpty(c) And SpW(ii, 2) <= c And c <= SpW(ii, 3) Then
Else
c = lngAvg
End If
Next c
NXT:
Next ii
End With
Set rng = Nothing
End Sub
Rückmeldung wäre nett! - Grüße von Erich aus Kamp-Lintfort