[英]Conditional Formatting to be set as a cell variable?
我將盡我所能,從現在開始,我已經在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.