Excel/Vba 如何根据数值有条件地更改所有包含数字的工作簿单元格的数字格式?

问题描述 投票:0回答:1

我认为这是一个独特的问题,我需要帮助。

这段代码不起作用,但我希望有一个语法可以工作,我还需要知道把它放在哪里。

在哪个事件中以及在什么级别的代码(模块或ThisWorkbook或工作表)?

或者可以在 Excel 中使用某种类型的条件格式来完成此操作吗?

If Cells.Value = 0 Then
    Cells.NumberFormat = "0"
ElseIf Abs(Cells.Value) > 0 And Abs(Cells.Value) < 0.1 Then
    Cells.NumberFormat = "0.000"
ElseIf Abs(Cells.Value) >= 0.1 And Abs(Cells.Value) < 1 Then
    Cells.NumberFormat = "0.00"
ElseIf Abs(Cells.Value) >= 1 And Abs(Cells.Value) < 10 Then
    Cells.NumberFormat = "0.0"
ElseIf Abs(Cells.Value) >= 10 Then
    Cells.NumberFormat = "0"
End If

谢谢, 戴尔

编辑:我当前尝试使用条件格式时遇到的问题: enter image description here

excel vba formatting format
1个回答
0
投票

在整个工作簿中有条件地使用数字格式化单元格

  • 这是一个可以根据需要用数字格式化单元格的工具。
  • 在使用它之前,创建文件的副本以对其进行测试。
  • 将完整代码复制到标准模块中,例如作业簿的
    Module1
  • 运行代码并确定这是否是您所需要的。
  • 您必须使用
    .xlsm
    .xlsb
    扩展名保存文件,才能将代码保留在工作簿中。
  • 在一台十年前的机器上,对于 100k 个带有数字的单元,该代码大约需要 25 秒。分享一下你这边花了多长时间。
Option Explicit

Sub FormatCellsWithNumbers()

    Const SECONDS_PER_100k As Long = 25 ' depends on the computer
    Const WARNING_CELLS_COUNT As Long = 100000
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' If it isn't, reference it by its name or use 'ActiveWorkbook'.

    ' Count the number of cells with numbers.
    Dim CellsCount As Double: CellsCount = CellsWithNumbersCount(wb)
    If CellsCount > WARNING_CELLS_COUNT Then
        Dim msg As Long: msg = MsgBox("Found " & Format(CellsCount, "#,##0") _
            & " cells with numbers." & vbLf _
            & "Formatting that many cells will take about " _
            & Format(CellsCount / 100000 * SECONDS_PER_100k, "#,##0") _
            & " seconds." & vbLf & vbLf & "Do you want to continue?", _
            vbQuestion + vbYesNo + vbDefaultButton2, "Long Operation!")
        If msg = vbNo Then Exit Sub
    End If
    
    ' Start measuring the time passed.
    Dim t As Double: t = Timer
    
    Application.ScreenUpdating = False ' increase efficiency
    
    ' Declare additional variables.
    Dim ws As Worksheet, cell As Range, FormattedCellsCount As Long
    
    ' Loop through all worksheets and format cells containing numbers.
    For Each ws In wb.Worksheets
        For Each cell In ws.UsedRange.Cells
            FormatCellWithNumber cell, FormattedCellsCount
        Next cell
    Next ws

    Application.ScreenUpdating = True

    ' Inform.
    MsgBox Format(FormattedCellsCount, "#,##0") _
        & " cells with numbers formatted in " _
        & Format(Timer - t, "0") & " seconds.", vbInformation

End Sub

Function CellsWithNumbersCount(ByVal wb As Workbook) As Double
    
    Dim ws As Worksheet, cell As Range, CellsCount As Long
    
    For Each ws In wb.Worksheets
        For Each cell In ws.UsedRange.Cells
            If VarType(cell.Value) = vbDouble Then CellsCount = CellsCount + 1
        Next cell
    Next ws
    
    CellsWithNumbersCount = CellsCount
    
End Function

Sub FormatCellWithNumber(ByVal cell As Range, ByRef FormattedCellsCount As Long)
    
    Dim Value As Variant: Value = cell.Value
    
    Dim NumFormat As String
    
    If VarType(Value) = vbDouble Then ' is a number
        FormattedCellsCount = FormattedCellsCount + 1
        Select Case Abs(Value)
            Case 0, Is >= 10: NumFormat = "0"
            Case Is < 0.1: NumFormat = "0.000"
            Case Is < 1: NumFormat = "0.00"
            Case Is < 10: NumFormat = "0.0"
        End Select
        cell.NumberFormat = NumFormat
    'Else ' not a number
    End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.