EXCEL VBA 的问题?
代码在下面这个地方写就可以,但是有了这个功能后,保存会卡得很惨,特殊是数据较大的时候,因为检测单元格的格式只有一个一个的扫描,这样非常耗时。
写好代码之后,保存时执行效果如下:
——跳到有填充颜色的单元格,提示不允许保存,然后返回。
代码位置如下图,千万注意位置的重要性:
代码文本为:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim st As Worksheet, rng As Range
For Each st In Sheets
If st.Name = "电1" Or st.Name = "电2" Or st.Name = "电3" Then
For Each rng In st.UsedRange.Cells
If rng.Interior.Color <> 16777215 Then
Application.Goto rng
MsgBox "这个单元格有填充颜色,不允许保存"
Cancel = True
Exit Sub
End If
Next rng
End If
Next st
End Sub
Sub AA()
Columns("d:j").Interior.Color = xlNone
Dim i, k
For i = 1 To Range("b65536").End(xlUp).Row
For k = 4 To 10
If Cells(i, "b") = Cells(i, k) Then
Cells(i, k).Interior.Color = 255
End If
Next
Next
End Sub
辛苦老师,不光是D:J对比B,如果D:J之间有相同的也需要变红.
Sub AA()
Columns("d:j").Interior.Color = xlNone
Dim i, k
For i = 1 To Range("b65536").End(xlUp).Row
For k = 4 To 10
x = WorksheetFunction.CountIf(Range(Cells(i, "D"), Cells(i, "j")), Cells(i, k))
If Cells(i, "b") = Cells(i, k) Or x > 1 Then
Cells(i, k).Interior.Color = 255
End If
Next
Next
End Sub
=COUNTIF(B1:J1,"2")