希望有人可以提供帮助。
我有以下数据。基本上我想编写一个公式/宏,以便在所有子级都关闭时可以更改“父级”的状态。例如,当父1的所有子(1.1,1.2,1.3,1.4)关闭时,父1的状态也应关闭。我不确定该怎么做。
Key Type Status Parent Code
1 Parent Open 1
1.1 Child Closed 1
1.2 Child Closed 1
1.3 Child Closed 1
1.4 Child Closed 1
2 Parent Open 2
2.1 Child Closed 2
2.2 Child Open 2
2.3 Child Open 2
答案 0 :(得分:0)
假设您的表格是从A1单元格开始的,则可以将此公式放入新列E2中并填写:
=IF(B2="Parent",IF(COUNTIFS($B$2:$B$10,"Child",$C$2:$C$10,"Closed",$D$2:$D$10,D2)=COUNTIFS($B$2:$B$10,"Child",$D$2:$D$10,D2),"Closed","Open"),"")
如果您需要在“状态”列中填写空白,则可以对“类型=父项”进行自动过滤,然后在可见的“状态”列字段中输入公式:
=E1
答案 1 :(得分:0)
您可以尝试:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long, i As Long, ParentKey As Long
Dim arr As Variant
Dim booClose As Boolean
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If Not Intersect(Target, .Range("C2:C" & LastRow)) Is Nothing And Target.Count = 1 Then
If InStr(1, .Cells(Target.Row, 1).Value, ".") = 0 Then
MsgBox "You are trying to manually change Parent Status."
Else
ParentKey = Mid(.Cells(Target.Row, 1).Value, 1, InStr(1, .Cells(Target.Row, 1).Value, "."))
For i = 2 To LastRow
If InStr(1, .Cells(i, 1).Value, ".") <> 0 Then
If Mid(.Cells(i, 1).Value, 1, InStr(1, .Cells(i, 1).Value, ".")) = ParentKey Then
If .Cells(i, 3).Value = "Closed" Then
booClose = True
Else
booClose = False
Exit For
End If
End If
End If
Next i
For i = 2 To LastRow
If InStr(1, .Cells(i, 1).Value, ".") = 0 Then
If .Cells(i, 1).Value = ParentKey Then
Application.EnableEvents = False
If booClose = True Then
.Cells(i, 3).Value = "Closed"
Exit For
Else
.Cells(i, 3).Value = "Open"
Exit For
End If
Application.EnableEvents = True
End If
End If
Next i
End If
End If
End With
End Sub