Vb.net 具有条件格式的多个条件

Vb.net 具有条件格式的多个条件,vb.net,excel,conditional-formatting,Vb.net,Excel,Conditional Formatting,我一直在开发VBA,以便在Excel中使用,我的同事可以使用它来帮助签入交付的产品库存。到目前为止,我已经能够找出如何编码以获得所有我想要的结果,但遇到了一个障碍 编码以删除一系列条件格式设置条件时,在使用三个条件中的第一个条件成功格式化列中的第一个单元格后,将引发错误。目标是在开始向单元格添加内容之前,让VBA将所有单元格预先格式化为一列,我们希望使用该列 如果存在导致问题的其他项目,则显示到目前为止的代码: Sub PopulateReceivingWorksheet()

我一直在开发VBA,以便在Excel中使用,我的同事可以使用它来帮助签入交付的产品库存。到目前为止,我已经能够找出如何编码以获得所有我想要的结果,但遇到了一个障碍

编码以删除一系列条件格式设置条件时,在使用三个条件中的第一个条件成功格式化列中的第一个单元格后,将引发错误。目标是在开始向单元格添加内容之前,让VBA将所有单元格预先格式化为一列,我们希望使用该列

如果存在导致问题的其他项目,则显示到目前为止的代码:

    Sub PopulateReceivingWorksheet()

    ''Step 1: Remove the first empty line from UNFI's text report of an invoice
        ''If Worksheets("Paste Invoice Here").Cells(1, 1) = "" Then
        ''    Worksheets("Paste Invoice Here").Rows(1).Delete
        ''End If
            ''Step 1 probably not needed.  Commented out just in case.

    ''Step 2: Use the LN column from the UNFI Invoice Report to calculate the number of rows
    ''to move to the Receiving Worksheet worksheet.

Dim lastUsedRow As Integer

lastUsedRow = Worksheets("Paste Invoice Here").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row

    ''Step 3: Using the contents of variable lastUsedRow to tell Do..While.. loop to end.

Dim i As Integer
    i = 1

    Worksheets("Receiving Worksheet").Cells(1, 10) = "Qty Received"
    Worksheets("Receiving Worksheet").Cells(1, 11) = "Notes"


            ''Clear old Conditional Formatting in Column 10.  This should be QtyReceived.
    Worksheets("Receiving Worksheet").Range(Cells(1, 10), Cells(i, 10)).FormatConditions.Delete


    Do While i < (lastUsedRow + 1)

        Worksheets("Receiving Worksheet").Cells(i, 1) = Worksheets("Paste Invoice Here").Cells(i, 1) ''Cell i from Column A
        Worksheets("Receiving Worksheet").Cells(i, 2) = Worksheets("Paste Invoice Here").Cells(i, 2) ''Cell i from Column B
        Worksheets("Receiving Worksheet").Cells(i, 3) = Worksheets("Paste Invoice Here").Cells(i, 3) ''Cell i from Column C
        Worksheets("Receiving Worksheet").Cells(i, 4) = Worksheets("Paste Invoice Here").Cells(i, 4) ''Cell i from Column D
        Worksheets("Receiving Worksheet").Cells(i, 5) = Worksheets("Paste Invoice Here").Cells(i, 5) ''Cell i from Column E
        Worksheets("Receiving Worksheet").Cells(i, 6) = Worksheets("Paste Invoice Here").Cells(i, 6) ''Cell i from Column F
        Worksheets("Receiving Worksheet").Cells(i, 7) = Worksheets("Paste Invoice Here").Cells(i, 7) ''Cell i from Column G
        Worksheets("Receiving Worksheet").Cells(i, 8) = Worksheets("Paste Invoice Here").Cells(i, 8) ''Cell i from Column H
        Worksheets("Receiving Worksheet").Cells(i, 9) = Worksheets("Paste Invoice Here").Cells(i, 9) ''Cell i from Column I

        With Worksheets("Receiving Worksheet").Cells(i + 1, 10)
            .Activate
            .FormatConditions.Add Type:=xlExpression, Formula1:="=$C2 = $J2"
                .FormatConditions(1).Interior.ColorIndex = 4

            .FormatConditions.Add Type:=xlExpression, Formula1:="=$C2 > $J2"
                .FormatConditions(2).Interior.ColorIndex = 3

            .FormatConditions.Add Type:=xlExpression, Formula1:="=OR($C2-$J2 < 1, $C2 < $J2)"
                .FormatConditions(3).Interior.ColorIndex = 6
        End With

        Worksheets("Receiving Worksheet").Cells(i + 1, 11).Formula = "=IF($C2=0,""Out of Stock"",IF($C2-$J2<0,CONCATENATE(-($C2-$J2),"" extra prodct received.  Check scope tags.""),IF($C2>$J2,CONCATENATE($C2-$J2,"" products unaccounted for.""),IF($C2=$J2,""All products received."",))))" ''Adds the If statement to each row in column, matching the number of rows from Paste Invoice Here worksheet.

            i = i + 1 ''Increases the value in the increment variable by 1 for each time the Loop is completed.

    Loop

    End Sub  

Zack发现的第二个条件语句的公式有错误

=$C2>$J$

应该是=$C2>$J2


基本上我错过了一个愚蠢的错误

您不需要放置FormatConditions。删除Do中的语句,同时。。。循环…?不确定,我可以试试。我认为我在开头附近添加.Delete语句的唯一原因是,当我只有一条条件语句与FormatContions.xxx语句一起应用时,我遇到了一个错误,并且宏在已填充的工作表上多次运行。您正在清除第10列第1行中的条件格式。如果您打算在所有行中清除它,那么您应该将该行放在Do中,而。。。在添加条件格式的第二行中,公式显示=$C2>$J$。这不应该是$J2吗?@Zack。谢谢你的帮助。它已被更正,但现在第三个条件抛出错误。