尝试将其放入 Sheet1 的 VBA 代码中
' Requirement: Let's say the data before change looks like this:
' Column A -- Column B -- Column C
' Fail 123 -
' Pass - 456
'
' After change of column A, data should look like this:
' Column A -- Column B -- Column C
' Pass - 123
' Fail 456 -
'
Private Sub Worksheet_Change(ByVal Target As Range)
' Do calculations only if the target of change is 1st column (i.e. column A)
If Target.Column = 1 Then
' Let's remember the data that's already in Column B and Column C
Dim DataInColumnB, DataInColumnC As String
DataInColumnB = Range(Replace(Target.Address, "A", "B")).Value
DataInColumnC = Range(Replace(Target.Address, "A", "C")).Value
' We assume that if A has Pass, dash will be in column B
' and if A has Fail, dash will be in column C
' If we find that A has Pass but B is not a dash, we must swap information in B and C
' Similarly if A has Fail but C is not a dash, we must also swap information in B and C
If (LCase(Target) = "pass" And DataInColumnB <> "-") Or _
(LCase(Target) = "fail" And DataInColumnC <> "-") Then
Range(Replace(Target.Address, "A", "B")).Value = DataInColumnC
Range(Replace(Target.Address, "A", "C")).Value = DataInColumnB
End If
End If
End Sub