我有一个公共函数我试图运行,看看列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