Search code examples
excelms-officeexcel-formulaoffice-2007vba

Excel change column format using macro


If there is word COST in column name then change the column format to currency using macro for excel.

like if column name is - "Final Cost" or "Total Cost" or "Item Cost" then change column format to currency.

Thanks


Solution

  • Try something like

    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