2013-02-22 106 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