Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1952to1956
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Angepasste Teilergebnissformel

Angepasste Teilergebnissformel
10.11.2023 17:49:19
Dan
Hallo zusammen, ich habe mir mit Hilfe von Bing eine modifizierte Teilergebnisformel gebaut. Diese funktioniert super nur in manchen fällen addiert Sie auch wenn die Bedingung nicht erfüllt ist. keine Ahnung woran es liegt. ich glaube die "CountIfs" Prüfung funktioniert nicht richtig. Hier der Code und zwei beispielspalten wo das Problem auftritt und eine Beispieldatei:

https://www.herber.de/bbs/user/164188.xlsb

Function CustomSubtotal109(rng As Range, CriteriaRange As Range, ByVal Criteria As Variant) As Variant
Dim cell As Range
Dim subtotalValue As Double
Dim condition As Boolean
Dim c As Variant ' Define variable c as Variant

' Initialize the subtotal value
subtotalValue = 0

' Iterate through each cell in the range
For Each cell In rng
' Check if the cell meets the criteria and is not a calculated CustomSubtotal109 cell
condition = False ' Reset the condition for each cell

If cell.HasFormula Then
' Check if the cell formula references CustomSubtotal109
If InStr(1, cell.Formula, "CustomSubtotal109", vbTextCompare) > 0 Then
' Ignore calculated CustomSubtotal109 cell
condition = False
GoTo NextCell
End If
End If

If IsArray(Criteria) Then
' Criteria is an array of values
For Each c In Criteria
If Application.WorksheetFunction.CountIfs(CriteriaRange, c, rng, cell) > 0 Then
condition = True
Exit For ' Exit the loop if a match is found
End If
Next c
Else
' Criteria is a single value
If Application.WorksheetFunction.CountIfs(CriteriaRange, Criteria, rng, cell) > 0 Then
condition = True
End If
End If

If condition Then
' Add the cell value to the subtotal
subtotalValue = subtotalValue + cell.Value
End If

NextCell:
Next cell

CustomSubtotal109 = subtotalValue
End Function

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Angepasste Teilergebnissformel
10.11.2023 21:33:33
Uduuh
Hallo,
du musst auf den unmittelbaren Nachbarn testen.
Function CustomSubtotal109(rng As Range, CriteriaRange As Range, ByVal Criteria As Variant) As Variant

Dim cell As Range
Dim subtotalValue As Double
Dim condition As Boolean
Dim c As Variant ' Define variable c as Variant
Dim intOFF As Integer

intOFF = CriteriaRange.Column - rng.Column
' Initialize the subtotal value
subtotalValue = 0

' Iterate through each cell in the range
For Each cell In rng
' Check if the cell meets the criteria and is not a calculated CustomSubtotal109 cell
condition = False ' Reset the condition for each cell

If cell.HasFormula Then
' Check if the cell formula references CustomSubtotal109
If InStr(1, cell.Formula, "CustomSubtotal109", vbTextCompare) > 0 Then
' Ignore calculated CustomSubtotal109 cell
condition = False
GoTo NextCell
End If
End If

If IsArray(Criteria) Then
' Criteria is an array of values
For Each c In Criteria
If cell.Offset(, intOFF) = c Then
condition = True
Exit For ' Exit the loop if a match is found
End If
Next c
Else
' Criteria is a single value
If cell.Offset(, intOFF) = Criteria Then
condition = True
End If
End If

If condition Then
' Add the cell value to the subtotal
subtotalValue = subtotalValue + cell.Value
End If

NextCell:
Next cell

CustomSubtotal109 = subtotalValue
End Function

Gruß aus'm Pott
Udo
Anzeige
AW: Angepasste Teilergebnissformel
10.11.2023 21:42:09
Dan
Hallo Udo,

tausend Dank! scheint zu funktionieren :) Aber kannst du mir sagen wo das Problem lag? was hat er denn vorher gemacht?

LG in den Pott
AW: Angepasste Teilergebnissformel
10.11.2023 22:20:39
Uduuh
Hallo,
Zählenwenns() zählt in allen Zellen die Kriterien und ist für die Prüfung vollkommen ungeeignet.
In dem Fall in A2:A28 das "" und in B2:B28 die 145,0. Da die 145,0 doppelt ist und "" mehrfach vorkommt, ist Zählenwenns(A2:A28;"";B2:B29;145) =2, was >0 ist.

Gruß aus'm Pott
Udo
AW: Angepasste Teilergebnissformel
10.11.2023 23:33:12
Dan
Das erklärt einiges... DANKE nocheinmal

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige