2013-02-22 59 views
0

如果列名称中存在单词COST,则使用宏为excel将列格式更改为货币。Excel更改使用宏的列格式

如果列名是 - “最终成本”或“总成本”或“项目成本”,则将列格式更改为货币。

感谢

回答

3

试着这么做

Public Sub FormatCostColumnsAsCurrency() 

    Dim sht As Worksheet 
    Set sht = ActiveSheet 

    Dim rng As Range 
    Dim i As Integer 

    For i = 1 To sht.UsedRange.Columns.Count 

     Set rng = sht.Cells(1, i) 
     If InStr(LCase(rng.Text), "cost") > 0 Then 
      sht.Columns(i).NumberFormat = "$#,##0.00" 
     End If 

    Next 

End Sub 
+0

谢谢合作.... – Maddy 2013-02-22 17:37:12

0

你可以用Conditional Formatting做到这一点,不需要宏。

  1. 突出显示表格左列中的单元格。

  2. 在功能区的Home部分,单击Conditional Formatting,然后单击New Rule

  3. Use a formula to determine which cells to format

  4. 输入此公式。如果你的行标题不A1开始,随之改变:=SEARCH("cost",A$1,1)

  5. 点击Format并设置NumberCurrency,然后单击确定。

  6. Conditional Formatting Rules Manager中,设置Applies to,使其覆盖您的表格。

0

试试下面的代码

Sub CurrFormat() 
    Dim colHeader As Range 
    Set colHeader = Range("A1:E1") 

    Dim currCell As Range 
    Set currCell = Cells.Find("COST") 

    If Not currCell Is Nothing Then currCell.NumberFormat = "$#,##0.00" 

End Sub