我有一個公共函數我試圖運行,看看列C中的格式,並將一個基於G列格式的值與點擊一個命令按鈕。我想用黃色突出顯示的行來獲得一個零,那些沒有內部並且沒有刪除線的可以獲得一個。我寫了下面的代碼,但是當我點擊命令按鈕時,什麼也沒有發生。不知道我的代碼是否在正確的位置或錯誤的語法?任何和所有的幫助表示讚賞。如果公式格式化單元格
Sub Resort()
Dim ws As Worksheet
Dim rng As Range
Dim urng As Range
Dim rng1 As Range
Dim shCmt As Comment
Set ws = Worksheets("Workbench Report")
lastrow = ws.Cells(ws.Rows.count, "D").End(xlUp).Row
ws.Select
ws.Range(Cells(2, "B"), Cells(Cells(2, "E").End(xlDown).Row, "G")).Sort _
key1:=ws.Range("E1"), order1:=xlAscending, Header:=xlYes, Orientation:=xlSortColumns
ws.Columns("E:E").EntireColumn.AutoFit
ws.Columns("E:E").ColumnWidth = 6.86
ws.Select
For Each rng In ws.Range("C2:C" & lastrow)
If rng.Interior.Color = 65535 Then
If urng Is Nothing Then
Set urng = ws.Range("E" & rng.Row)
Else
Set urng = Union(urng, ws.Range("E" & rng.Row))
End If
End If
Next rng
If Not urng Is Nothing Then urng.copy
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(2, 3).PasteSpecial xlPasteValues
ws.Range("H2").PasteSpecial xlPasteValues
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(2, 2).Select
Selection.Formula = "=IF(H2>0,COUNTIF(E:E,H2)-2,"""")"
Selection.HorizontalAlignment = xlCenter
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(3, 2).Select
Selection.Formula = "=IF(H3>0,COUNTIF(E:E,H3)-2,"""")"
Selection.HorizontalAlignment = xlCenter
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(4, 2).Select
Selection.Formula = "=IF(H4>0,COUNTIF(E:E,H4)-2,"""")"
Selection.HorizontalAlignment = xlCenter
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(5, 2).Select
Selection.Formula = "=IF(H5>0,COUNTIF(E:E,H5)-2,"""")"
Selection.HorizontalAlignment = xlCenter
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues
ws.Range("B" & Cells.Rows.count).End(xlUp).Offset(6, 2).Select
Selection.Formula = "=IF(H6>0,COUNTIF(E:E,H6)-2,"""")"
Selection.HorizontalAlignment = xlCenter
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues
ws.Columns("H").ClearContents
SendKeys ("{ESC}")
ws.Select
ws.Range("E2").Select
End Sub
Public Function ColorIndex(rng As Range) As Boolean
For Each rng In ws.Range("C2:C" & lastrow)
If rng.Interior.Color = 65535 Then
ws.Range("G" & rng.Row).Value = "0"
End If
Next rng
For Each rng In ws.Range("C2:C" & lastrow)
If rng.Interior.Color = xlNone And rng.Font.Strikethrough = False Then
ws.Range("G" & rng.Row).Value = "1"
End If
Next rng
End Function
如何你打電話的功能? –
我在我的子模塊中調用了ColorIndex –
通過單擊VBA編輯器的邊界內部,可以啓用一個斷點,這將暫停執行VBA代碼。如果是這種情況,那麼您確定已經執行了您的代碼。如果沒有,你可能需要調查爲什麼你的代碼沒有執行。 – Dominique