简体   繁体   English

检查单元格是否为空白,如果是,填充它,然后将结果与旧值进行比较,如果不同,使用 VBA 突出显示?

[英]Checking if cell is blank, if it is, fill it, then compare result to old value & if different, highlight using VBA?

Im trying to have this function loop through a range of values in column Z from rows 6 to the number stored in the OS_PLs variable.我试图让这个函数循环遍历 Z 列中从第 6 行到存储在 OS_PLs 变量中的数字的一系列值。 I want it to check to see if there is already an invoice number in the cell...and if there is, to move on to the next cell....however if there is not, to put this match formula in to see if it can find a corresponding invoice number on the ARbalance sheet.我想让它检查单元格中是否已经有发票号……如果有,继续到下一个单元格……但是如果没有,把这个匹配公式放进去看看如果它可以在 AR 资产负债表上找到相应的发票编号。 Then, if it placed something other than the original value in the cell, to highlight the cell in orange with black hash marks.然后,如果它在单元格中放置了原始值以外的内容,则用黑色散列标记以橙色突出显示单元格。

To accomplish this I have tried to first store the initial value in a variable "valuestore"为此,我尝试首先将初始值存储在变量“valuestore”中

Next I tried to make sure to clear any cells that only contain "" from the last time I ran this function (later in the macro it copys the range and pastes as values to get rid of the formulas) so that the "isempty" check will work properly.接下来,我尝试确保清除上次运行此函数时仅包含“”的所有单元格(稍后在宏中它复制范围并粘贴为值以摆脱公式),以便“isempty”检查将正常工作。

Next I have it check if the cell is empty and if it is not, it should just move on to the next i but if it is empty, then it should input the match formula接下来我检查单元格是否为空,如果不是,它应该移动到下一个 i 但如果它是空的,那么它应该输入匹配公式

Finally, assuming the cell was empty in the last step, it should compare the new value to the old value and if it is different, then it should highlight the cell.最后,假设在最后一步中单元格为空,则应将新值与旧值进行比较,如果不同,则应突出显示该单元格。

For whatever reason this seems to be clearing each cell, it seems to always show nothing as the value of the valuestore variable, and it is inputting the formula in every cell.无论出于何种原因,这似乎都在清除每个单元格,它似乎总是不显示 valuestore 变量的值,并且在每个单元格中输入公式。 the only thing it seems to do (kind of) correctly is to highlight the cells which end up with values at the end and not highlight the cells which remain "".它似乎做(有点)正确的唯一一件事是突出显示最后以值结束的单元格,而不是突出显示保留“”的单元格。 However, it is also highlighting the cells which originally contained invoice numbers that it has deleted and since overwritten.但是,它还突出显示了最初包含已删除并已覆盖的发票编号的单元格。

Anyone have any ideas?谁有想法?

Thanks谢谢

For i = 6 To OS_PLs

   Dim valuestore As String
   Range("Z" & i).Value = valuestore
   MsgBox (valuestore)

    If Range("Z" & i).Value = """" Then
        Range("Z" & i).Clear
    End If

    If IsEmpty(Range("Z" & i)) Then
        Range("Z" & i).Select
            ActiveCell.FormulaR1C1 = _
                "=IFERROR(IF(LEFT(INDEX('AR balance.xlsx'!AR_Invoice_Nums,MATCH(RC[-20],'AR balance.xlsx'!AR_PL_Nums,0)),5)=""CMSHK"","""",INDEX('AR balance.xlsx'!AR_Invoice_Nums,MATCH(RC[-20],'AR balance.xlsx'!AR_PL_Nums,0))),"""")"

        If Not (Range("Z" & i)) = """" Then
            If Not (Range("Z" & i)) = valuestore Then
                With Selection.Interior
                .Pattern = xlUp
                .PatternColorIndex = xlAutomatic
                .Color = 49407
                .TintAndShade = 0
                .PatternTintAndShade = 0
                End With
            End If
        End If
    Else
    End If
Next i

For a blank cell Range("Z" & i).Value = """" will always return FALSE对于空白单元格Range("Z" & i).Value = """"将始终返回FALSE

Range("Z" & i).Value = "" will return TRUE Range("Z" & i).Value = ""将返回TRUE

Range("Z" & i).Value = vbNullString is possibly a more specific test. Range("Z" & i).Value = vbNullString可能是一个更具体的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM