代码之家  ›  专栏  ›  技术社区  ›  Andre Nevares sj95126

如何将所有单元格的数字格式更改为“常规”?

  •  0
  • Andre Nevares sj95126  · 技术社区  · 4 年前

    我需要写一个宏 NumberFormat = "General" 到电子表格中的所有单元格。

    这就是我所拥有的

    Sub set_general_format()
        Select Case _
            MsgBox("Do you want to apply generic to all cells?" _
            & "Save before?", vbYesNoCancel, _
            "Or just change to numeric?")
            Case Is = vbYes
                ThisWorkbook.Save
            Case Is = vbCancel
                Exit Sub
        End Select
    
        Sheets(1).Select
        
        For i = 1 To Sheets.Count
            Sheets(i).Select
            Cells.Select
            Selection.NumberFormat = "General"
            Application.CutCopyMode = False
        Next i
    End Sub
    

    有人能帮我改进这个代码吗?

    2 回复  |  直到 4 年前
        1
  •  2
  •   kevin9999    4 年前

    全部替换:

    Sheets(1).Select
        
        For i = 1 To Sheets.Count
            Sheets(i).Select
            Cells.Select
            Selection.NumberFormat = "General"
            Application.CutCopyMode = False
        Next i
    

    有:

        For Each Sheet In ThisWorkbook.Sheets
            Sheet.Cells.NumberFormat = "General"
        Next
    
        2
  •  1
  •   Simon Sultana    4 年前

    您可以合并三条线:

    Sheets(i).Select
    Cells.Select
    Selection.NumberFormat = "General"
    

    一行如下:

    Sheets(i).Cells.NumberFormat = "General"  
    
    推荐文章