如何在vba中更改列的数字格式

时间:2016-03-23 13:58:26

标签: excel vba

我正在尝试使用用户选择来更改该选择内的列的数字格式。我试图将其更改为的数字格式是会计。我试过的代码如下。

Dim r As Range
Dim s As String
s = Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
r = Range(s).Columns("G")
r.NumberFormat = "$#,##0.00"

我确信String s获取用户选择的正确字符串值。获取G列的范围似乎是不正确的。

1 个答案:

答案 0 :(得分:2)

试试这个:

Dim rSel As Range, rCol As Range

Set r = Selection
If r.Columns.Count > 6 Then 'make sure there are even 7 columns

    Set rCol = r.Columns(7) 'Columns("G")
    rCol.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"

End If