我对 VBA 比较陌生,并且正在努力解决工作表更改问题。我试图使附加的代码工作,以在任何“是”/“否”单元格更改时自动更改单元格 A47:B53。问题在于“是”/“否”的更改是根据公式自动进行的。我知道 Worksheet_change 不适用于根据公式更改的单元格,它主要用于手动更改(例如下拉列表或其他内容),但如何更改代码以执行我需要执行的操作?有没有办法使用Worksheet_Calculate之类的?
当单元格为“是”时,我希望蓝色区域中的关联单元格更改为“输入%”,当单元格为“否”时,我希望蓝色区域中的关联单元格更改为“0”。我希望能够将“输入%”更改为我想要的任何数字,并且它将保持不变,直到“是”变成“否”并再次使其变为0。我希望这是有道理的。请参阅名称管理器以查看单元名称。
我尝试过 worksheet_change 选项,但通过研究解决方案,我发现这只适用于手动更改。问题是我希望能够输入“Enter %”,并且只有当“是”再次变为“否”时才能再次更改。
这是“是/否”单元格中的公式(“是/否”单元格称为“Brad#a”或“Nick#a”,而“Brad#b”或“Nick#b”是我想要的位置要定位的代码的输出)
=IF(SUM(34澳元:36澳元)=0,"否","是")
这是我的VBA代码:
Private Sub Worksheet_Change(ByVal Destination As Range)
'Brad
ElseIf Not Intersect(Destination, Range("Brad7a")) Is Nothing Then
If Range("Brad7a") = "YES" Then
Sheets("Resource Allocation").Range("Brad7b") = "Enter %"
ElseIf Range("Brad7a") = "no" Then
Sheets("Resource Allocation").Range("Brad7b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad6a")) Is Nothing Then
If Range("Brad6a") = "YES" Then
Sheets("Resource Allocation").Range("Brad6b") = "Enter %"
ElseIf Range("Brad6a") = "no" Then
Sheets("Resource Allocation").Range("Brad6b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad5a")) Is Nothing Then
If Range("Brad5a") = "YES" Then
Sheets("Resource Allocation").Range("Brad5b") = "Enter %"
ElseIf Range("Brad5a") = "no" Then
Sheets("Resource Allocation").Range("Brad5b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad4a")) Is Nothing Then
If Range("Brad4a") = "YES" Then
Sheets("Resource Allocation").Range("Brad4b") = "Enter %"
ElseIf Range("Brad4a") = "no" Then
Sheets("Resource Allocation").Range("Brad4b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad3a")) Is Nothing Then
If Range("Brad3a") = "YES" Then
Sheets("Resource Allocation").Range("Brad3b") = "Enter %"
ElseIf Range("Brad3a") = "no" Then
Sheets("Resource Allocation").Range("Brad3b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad2a")) Is Nothing Then
If Range("Brad2a") = "YES" Then
Sheets("Resource Allocation").Range("Brad2b") = "Enter %"
ElseIf Range("Brad2a") = "no" Then
Sheets("Resource Allocation").Range("Brad2b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Brad1a")) Is Nothing Then
If Range("Brad1a") = "YES" Then
Sheets("Resource Allocation").Range("Brad1b") = "Enter %"
ElseIf Range("Brad1a") = "no" Then
Sheets("Resource Allocation").Range("Brad1b") = "0"
End If
'Nick
ElseIf Not Intersect(Destination, Range("Nick7a")) Is Nothing Then
If Range("Nick7a") = "YES" Then
Sheets("Resource Allocation").Range("Nick7b") = "Enter %"
ElseIf Range("Nick7a") = "no" Then
Sheets("Resource Allocation").Range("Nick7b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick6a")) Is Nothing Then
If Range("Nick6a") = "YES" Then
Sheets("Resource Allocation").Range("Nick6b") = "Enter %"
ElseIf Range("Nick6a") = "no" Then
Sheets("Resource Allocation").Range("Nick6b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick5a")) Is Nothing Then
If Range("Nick5a") = "YES" Then
Sheets("Resource Allocation").Range("Nick5b") = "Enter %"
ElseIf Range("Nick5a") = "no" Then
Sheets("Resource Allocation").Range("Nick5b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick4a")) Is Nothing Then
If Range("Nick4a") = "YES" Then
Sheets("Resource Allocation").Range("Nick4b") = "Enter %"
ElseIf Range("Nick4a") = "no" Then
Sheets("Resource Allocation").Range("Nick4b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick3a")) Is Nothing Then
If Range("Nick3a") = "YES" Then
Sheets("Resource Allocation").Range("Nick3b") = "Enter %"
ElseIf Range("Nick3a") = "no" Then
Sheets("Resource Allocation").Range("Nick3b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick2a")) Is Nothing Then
If Range("Nick2a") = "YES" Then
Sheets("Resource Allocation").Range("Nick2b") = "Enter %"
ElseIf Range("Nick2a") = "no" Then
Sheets("Resource Allocation").Range("Nick2b") = "0"
End If
ElseIf Not Intersect(Destination, Range("Nick1a")) Is Nothing Then
If Range("Nick1a") = "YES" Then
Sheets("Resource Allocation").Range("Nick1b") = "Enter %"
ElseIf Range("Nick1a") = "no" Then
Sheets("Resource Allocation").Range("Nick1b") = "0"
End If
End If
End Sub
可能是这样的,使用
Calculate
事件:
Private Sub Worksheet_Calculate()
CheckYesNo Me 'in the sheet code module, Me = the worksheet
End Sub
Sub CheckYesNo(ws As Worksheet)
Dim arrNames, i As Long, el, nm As String, v, c As Range
arrNames = Array("Brad", "Nick")
For Each el In arrNames 'loop names
For i = 7 To 1 Step -1
nm = el & i 'eg "Brad7", "Nick3"
Set c = ws.Range(nm & "b") 'updating this cell
Select Case Me.Range(nm & "a").Value
Case "YES"
'don't replace any already-entered numbers....
If Len(c.Value) = 0 Or Not IsNumeric(c.Value) Then
c.Value = "Enter %"
End If
Case "no"
c.Value = 0
End Select
End With
Next i
Next i
End Sub