单元格值更改时的Excel VBA更改编号格式 [英] Excel VBA Change Number format on Cell value change

查看:97
本文介绍了单元格值更改时的Excel VBA更改编号格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用一些Excel Visual Basic时遇到了一些困难.特别是我想根据单元格的值更改列的格式.这就是我到目前为止所拥有的.

I am having a little difficulty with some Excel visual basic. Particularly I want to change the format of a column based on the value of a cell. Here's what I have so far.

 Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = Range("$b$1") Then
        If Range("$b$1") = "Date" Then
            Range("d:d").NumberFormat = "m/d/yyyy"
        ElseIf Range("$b$1") = "Number" Then
            Range("d:d").NumberFormat = "#,##0.00"
        End If
    End If
End Sub    

我不熟悉使用目标"功能,因此任何建议都将对您有所帮助.

I am new to using the Target feature so any advice would be helpful.

推荐答案

我认为您不需要使用Target,只需检查内的代码块就足够了.像这样;

I don't think you need to use Target, just the block inside that check should enough. Like this;

Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("B1") = "Date" Then
        Range("D:D").NumberFormat = "m/d/yyyy"
    ElseIf Range("B1") = "Number" Then
        Range("D:D").NumberFormat = "#,##0.00"
    End If

End Sub

然后将B1中的值更改为日期"或数字".

Then change the value in B1 to "Date" or "Number".

这篇关于单元格值更改时的Excel VBA更改编号格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆