我对 VBA 很陌生,但到目前为止,我还在继续。直到这一刻,我都被这个挑战困住了。
My goal is to select a percentage (10, 90 or 100) from a drop down menu in a cell in column B, C, D or E and when one of the percentages is selected, I want the same cell to calculate the selected percentage A列中的值。
因此,当 A 列中的单元格的值为“500”并且在 CI 列的同一行中选择“90”时,我希望将 90 替换为 450 (0.9*500)
这里给出的代码是我到目前为止所得到的,包括一些已经从另一个主题“借来”的代码。虽然它可以在一个新的、干净的 excel 表中工作,但它不能在我希望它工作的文档中工作。
你们中的一些人有什么想法可以找到缺陷吗?(我什至创建了一个只有任务的命令按钮:Application.EnableEvents = True)
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Not Intersect(Target, Range("B:D")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = 90 Then Target.Value = 0.9 * Cells(Target, 1).Value
ElseIf Target.Value = 10 Then Target.Value = 0.1 * Cells(Target, 1).Value
ElseIf Target.Value = 100 Then Target.Value = Cells(Target, 1).Value
Else
MsgBox "Not a valid percentage"
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub