0
我一直在研究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
難道你需要把'FormatConditions.Delete '在你的'語句裏做... while ... Loop' ...? – ManishChristian
不確定,我可以試試。我認爲我在開頭附近添加.Delete語句的唯一原因是,當我只有一個條件語句與FormatContions.xxx語句一起應用並且宏已在已填充的工作表上運行多次時,出現錯誤。 –
您正在清除第10列中第1行的條件格式。如果您打算在所有行中清除它,則應將該行置於「Do While ... Loop」中。 – ManishChristian