在我们的团队名单中,我们正试图消除那些在某一天离开的人们的转变。对于例如在2017年5月25日截图的截图中,Leo离开了。我可以在上面的行中使用什么公式/选项/代码,以便根据此“离开”列中的手动输入自动删除所有Leo的班次。
答案 0 :(得分:1)
以下是使用Excel VBA的可能解决方案。
首先,将原始名单的内容(名为" Master")复制到新工作表"更新名册"。这样您就可以保留原始名单的未更改副本,以防您稍后删除来自"离开"行并需要重新应用更新。
接下来,扫描第一列以找到"离开:"行。
最后,对于其余每一列,"离开"名称被加载到数组中。
然后,每个"离开"名称一次处理一个。通过匹配正则表达式,然后根据需要替换为逗号或任何内容,从名单的每一行中删除每次出现的名称。
Option Explicit
Dim AwayRowNbr As Long
Sub UpdateRoster()
Dim UpdatedRoster As Worksheet
Dim RowNbr As Long
Dim ColNbr As Long
Dim MaxColNbr As Long
Dim MaxRowNbr As Long
' Insert new "Updated Roster" worksheet
Set UpdatedRoster = Sheets.Add(After:=Sheets(Sheets.Count))
UpdatedRoster.Name = "Updated Roster"
With ThisWorkbook.Worksheets("Updated Roster")
' Copy contents of "Master" worksheet to "Updated Roster"
ThisWorkbook.Worksheets("Master").Cells.Copy Destination:=.Cells
' Locate "Away" row, and determine last column with data
AwayRowNbr = .Range("A:A").Find(What:="Away:", LookIn:=xlValues).Row
MaxColNbr = .Cells(AwayRowNbr, Columns.Count).End(xlToLeft).Column
For ColNbr = 2 To MaxColNbr
Call RemoveNames(.Cells(AwayRowNbr, ColNbr).Value, ColNbr)
Next ColNbr
End With
End Sub
Sub RemoveNames(AwayNames As String, ColNbr As Long)
Dim AwayName() As String
Dim Name As String
Dim NameIdx As Integer
Dim RegEx As Object
Dim RowNbr As Long
Dim MaxRowNbr As Long
Dim BeforeReplace As String
Dim AfterReplace As String
With ThisWorkbook.Worksheets("Updated Roster")
' Create regular expression object
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = False
' Load "away" names into a String array
AwayName() = Split(AwayNames, ",")
' Determine last row with data
MaxRowNbr = .Cells(Rows.Count, ColNbr).End(xlUp).Row
' Process each "away" name
For NameIdx = LBound(AwayName) To UBound(AwayName)
Name = Trim(AwayName(NameIdx))
For RowNbr = 2 To MaxRowNbr
If RowNbr <> AwayRowNbr Then
AfterReplace = .Cells(RowNbr, ColNbr).Value
' Remove name if delimited by commas
RegEx.Pattern = ", *" & Name & " *,"
Do
BeforeReplace = AfterReplace
AfterReplace = RegEx.Replace(BeforeReplace, ",")
Loop Until BeforeReplace = AfterReplace
' Remove name if at beginning or end of cell
RegEx.Pattern = "(^ *" & Name & " *,)|(, *" & Name & " *$)|(^ *" & Name & " *$)"
Do
BeforeReplace = AfterReplace
AfterReplace = RegEx.Replace(BeforeReplace, "")
Loop Until BeforeReplace = AfterReplace
.Cells(RowNbr, ColNbr).Value = AfterReplace
End If
Next RowNbr
Next NameIdx
End With
End Sub