当 B6 = San Francisco 时,我试图在 D15:D54 范围内显示美元,否则显示欧元。但我的 VBA 不起作用。任何人都可以帮忙,好吗?
Private Sub Worksheet_Auto(ByVal Target As Range)
If Range("B6").Value = "San Francisco" Then
Range("D15:D54").Value = "Dollars ($)"
Else
Columns("D15:D54").Value = "Euros (€)"
End If
End Sub
最佳答案
工作表更改:源单元格与目标范围
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Check if source cell is not among the changed cells.
If Intersect(Target, Range("B6")) Is Nothing Then Exit Sub
' Prevent re-triggering this event when writing to the destination range.
Application.EnableEvents = False
' Prevent events staying disabled if an error occurs.
On Error GoTo ClearError
' Use the 'IIf' function to save a few lines.
' Use 'StrComp' with 'vbTextCompare' to ignore case ('SAN = san').
' Use 'CStr' to prevent failure if the source contains an error value.
Range("D15:D54").Value = IIf( _
StrComp(CStr(Range("B6").Value), "San Francisco", vbTextCompare) = 0, _
"Dollars ($)", _
"Euros (€)")
SafeExit:
Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
关于excel - VBA 单元格值问题以及是否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69681921/