2013-08-06 44 views
2

我打算尽可能多地意识到这一点,我已经在VBA上工作了整整3天,这是我实习项目中的一部分。将条件格式设置为单元变量?

总之,我有一组数字,如果这个数字超出了12-70的范围,这是一个不好的数据点,不应该包含在其他链接的计算中。但是,该号码需要保留在那里,我无法直接更改其他链接的公式。因此,我的想法是将单元格更改为“不适用”,但将该单元格的显示返回到原始数字(如果可能的话)。这是我的代码(其是功能性的):

'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作为实际单元格值?

+1

您是否尝试过一个数字格式的规则,这样的事'[<12]"N/A";[> 70]“N/A”; General'(我曾与这个有点玩,以确保它至少在某种程度上工作,似乎太..) – chancea

+0

问题是我想“不适用”是实际的单元格值和数字只是显示,而不是相反。我真的不确定这是否可能,但我在VBA中并不是那么了解。 –

+1

我会这样做的方式是将数字放入对单元格的评论中。这种方式可以在你将鼠标悬停在上面时看到。 – martin

回答

1

这是!见解释说明。希望有所帮助!

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 
+0

谢谢@loannis!聪明的结局我无法选择,像魅力一样工作。谢谢@马丁,你的想法也很好。 –