【问题标题】:Excel change column format using macroExcel使用宏更改列格式
【发布时间】:2013-02-08 08:56:01
【问题描述】:

如果列名中有单词 COST,则使用 excel 宏将列格式更改为货币。

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

谢谢

【问题讨论】:

    标签: excel ms-office excel-formula office-2007 vba


    【解决方案1】:

    您可以使用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并将Number设置为Currency,然后点击确定。

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

    【讨论】:

      【解决方案2】:

      试试类似的东西

      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
      

      【讨论】:

        【解决方案3】:

        试试下面的代码

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多