excel - VBA 单元格值问题以及是否

标签 excel vba

当 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/

相关文章:

Excel 宏 - 使用 For Each 循环超链接回主/索引页面

excel - 为什么 VBA For Next Loop 计算不正确?{VBA}

excel - VBA中的 'Type characters'和 'Type conversion functions'有什么区别?

excel - 无法通过 VBA 从目标工作簿中删除代码模块

vba - 使用 VBA 解决问题

excel - 如何设置默认数字格式以包含千位分隔符 (,)

vba - 如何使用 VBA 从 Excel 图表中删除一个系列

ms-access - MS Access vba,打开带参数的select query,运行时3065无法执行select query

vba - Excel VBA : Date Comparison

vba - MS Access 表单绑定(bind)到 ADO 断开连接的记录集