我一直致力于在Excel中使用VBA,我的同事可以使用它来帮助从交付中检入产品库存。到目前为止,我已经能够弄清楚如何编写代码以获得我想要的所有结果,但是遇到了障碍。
在编码以删除一系列条件格式条件时,在使用三个条件中的第一个条件成功格式化列中的第一个单元格后,将引发错误。我们的目标是让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
答案 0 :(得分:0)
Zack发现的第二个条件陈述公式出现了错误。
= $ C2&gt; $ $Ĵ
应该是= $ C2&gt; $ J2
基本上我错过了一个愚蠢的错误;)