excel 对于未以黄色突出显示的像元,VBA除以1000

fgw7neuy  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(105)

我目前正在使用这个基本代码来将单元格的值除以1000:

Sub Divide_by_1000()
    Dim cell As Range
    For Each cell In Selection
        cell = cell / 1000
    Next
 End Sub

但我想知道是否可以更改这些行,使代码仅在单元格未以黄色突出显示时将单元格除以1000。有人知道如何做到这一点吗?非常感谢。
由于我想要“除以1000”的单元格非常随机地分布,所以我需要手动选择那些我不想除以1000的单元格,并将它们突出显示为黄色。但是我想知道是否有一种代码可以过滤掉那些没有用黄色突出显示的,然后将它们除以1000。谢谢你。

vuktfyat

vuktfyat1#

分割高亮

Sub DivideAndHighlight()
    Const PROC_TITLE As String = "Divide and Highlight"
    Const DIVISOR As Double = 1000
    Const HIGHLIGHT_COLOR As Long = vbYellow
    
    If Selection Is Nothing Then Exit Sub ' no visible workbooks open
    If Not TypeOf Selection Is Range Then Exit Sub ' not a range
    
    Dim urg As Range, cell As Range, cValue
    
    For Each cell In Selection.Cells
        If cell.Interior.Color <> HIGHLIGHT_COLOR Then
            cValue = cell.Value
            If VarType(cValue) = vbDouble Then ' is a number
                If urg Is Nothing Then
                    Set urg = cell
                Else
                    Set urg = Union(urg, cell)
                End If
            End If
        End If
    Next cell
    
    If urg Is Nothing Then
        MsgBox "No cells found.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    Dim arg As Range, Data, rCount As Long, cCount As Long, r As Long, c As Long
    
    For Each arg In urg.Areas
        
        rCount = arg.Rows.Count
        cCount = arg.Columns.Count
        
        If rCount * cCount = 1 Then
            ReDim Data(1 To 1, 1 To 1): Data(1, 1) = arg.Value
        Else
            Data = arg.Value
        End If
        
        For r = 1 To rCount
            For c = 1 To cCount
                Data(r, c) = Data(r, c) / DIVISOR
            Next c
        Next r
        
        arg.Value = Data
    
    Next arg
    
    urg.Interior.Color = HIGHLIGHT_COLOR
    
    MsgBox "Cells divided and highlighted.", vbInformation, PROC_TITLE
 
 End Sub

相关问题