excel - 条件格式设置为单元格变量?

标签 excel vba

我将尝试尽可能多地理解这一点,我已经在 VBA 上工作了整整 3 天,这是我实习项目之一的一部分。

简而言之,我有一组数字,如果这个数字超出了 12-70 的范围,那就是一个糟糕的数据点,不应该包含在其他关联的计算中。但是,数字需要保留在那里,我不能直接更改其他链接的公式。

所以,我的想法是将单元格更改为“N/A”,但将该单元格的显示返回到原始数字(如果可能的话)。这是我的代码(这是功能性的):

'This will take all questionable data and remove it from calculation
Dim i As Variant
Dim j As Variant

For Each j In Array(6, 7, 10)
    For i = 3 To 500
        If Cells(i,j) = "N/A" Or Cells(i,j) = "" Then

        ElseIf Cells(i,j) < 12 Or Cells(i,j) > 70 Then
            Cells(i, 11) = Cells(i,j)
            Cells(i,j).NumberFormat = "0;0;0;[Red]""Bad Data"""
            Cells(i,j) = "N/A"
        End If
    Next i
Next j

因此,截至目前,这会将原始数字重写到原始数字旁边的第 11 列,将值更改为 N/A,然后显示为“错误数据”。

有没有办法改变“坏数据”显示以显示原始数字,同时保持 N/A 作为实际单元格值?

最佳答案

这里是!解释见评论。希望有帮助!

Sub set_Cell_Variable()
    'This will take all questionable data and remove it from calculation
    Dim i As Variant
    Dim j As Variant

    ' Two new variables 
    Dim strTemp As String
    Const QUOTE = """" 

    For Each j In Array(6, 7, 10)
        For i = 3 To 500
            If Cells(i, j) = "N/A" Or Cells(i, j) = "" Then

            ElseIf Cells(i, j) < 12 Or Cells(i, j) > 70 Then
                Cells(i, 11) = Cells(i, j)
                ' Store the cell value as String. 
                ' That way it can go into NumberFormat
                strTemp = CStr(Cells(i, j).Value)
                ' Add it to NumberFormat dynamically
                Cells(i, j).NumberFormat = "0;0;0;[Red]" & QUOTE & strTemp & QUOTE
                Cells(i, j) = "N/A"
            End If
        Next i
    Next j
End Sub

关于excel - 条件格式设置为单元格变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083470/

相关文章:

excel - 如何将单元格值定义到工作簿中每个工作表的同一列VBA

excel - 如何找到列标题并返回列号vba

excel - 在 IF 语句中使用 VLookup

excel - 删除名称中包含单词 "sheet"的任何工作表而不发出警告

vba - 如何在 Excel 中使用 for 循环创建命名范围?

excel - VBA "Format"函数返回不一致 -

c# - 如何查找单元格的命名范围 - VSTO

arrays - 处理 Excel 的 VBA 宏中的数组

python - 使用 xlwings 写入现有 Excel 工作簿

VBA、ADO.Connection 和 PostgreSQL 如何将查询参数传递给 pg_typeof()