【发布时间】:2017-08-28 20:33:26
【问题描述】:
从一开始,我就是一个编码新手,我的技能范围正在根据 Google 搜索进行修改。
我正在使用 Excel 2010,并尝试使用 VBA 来避免使用条件格式。我使用的电子表格是一个包含 31 列的模板,其中 20 列是强制性的。有适当的编码,如果任何给定行的这些列之一中有条目,则必须先填充其他 19 个单元格,然后才能保存文件。
我正在尝试执行类似的功能来突出显示阻止文件保存的空白单元格。我知道我可以使用条件格式,但这需要它查看每一行中的一个单元格,而不是该行中的任何所需单元格。我也知道它可以处理多种条件格式,但我知道层数越多,电子表格的运行速度就越慢。
这可能吗?我必须的列是 A-D、F-K、M-N、S-Y 和 AE。万一这会是一个因素,我附上了我用来强制输入的代码,一旦填充了这些列中的任何一个:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
Dim rg As Range, c As Range
Dim bCanSave As Boolean
Dim sWarning As String
Set ws = Sheets("Main")
Set rg = ws.Range("A2:A10000,B2:B10000,C2:C10000,D2:D10000,F2:F10000,G2:G10000,H2:H10000,J2:J10000,K2:K10000,N2:N10000,S2:S10000,T2:T10000,U2:U10000,V2:V10000,W2:W10000,X2:X10000,Y2:Y10000,AE2:AE10000")
sWarning = "File not saved!" & vbNewLine & "Mandatory cells missing in rows: " & vbNewLine
With ws
bCanSave = True
For Each c In rg
If Not IsEmpty(c) Then
If .Cells(c.Row, "A") = "" Or .Cells(c.Row, "B") = "" Or .Cells(c.Row, "C") = "" Or .Cells(c.Row, "D") = "" Or .Cells(c.Row, "F") = "" Or .Cells(c.Row, "G") = "" Or .Cells(c.Row, "H") = "" Or .Cells(c.Row, "I") = "" Or .Cells(c.Row, "J") = "" Or .Cells(c.Row, "K") = "" Or .Cells(c.Row, "N") = "" Or .Cells(c.Row, "S") = "" Or .Cells(c.Row, "T") = "" Or .Cells(c.Row, "U") = "" Or .Cells(c.Row, "V") = "" Or .Cells(c.Row, "W") = "" Or .Cells(c.Row, "X") = "" Or .Cells(c.Row, "Y") = "" Or .Cells(c.Row, "AE") = "" Then
bCanSave = False
sWarning = sWarning & c.Row & ","
End If
End If
Next c
End With
If Not bCanSave Then
MsgBox sWarning, vbExclamation
Cancel = True
End If
End Sub
【问题讨论】:
-
“我正在使用 Excel _____” - “____”是年份/版本的占位符吗?
-
Mat's Mug:是的...用 Excel 2010 编辑了 OP。 pnuts:可能是我措辞不佳?如果我想影响 20 列的条件格式,我需要有 20 个依赖条件。
-
仅用于 cf 部分:如果您对
$A$1:$D$100,$F$1:$K$100,$M$1:$N$100,$S$1:$Y$100,$AE$1:$AE$100之类的范围进行条件格式设置并设置Format only cells that contain->specific text¬ containing&*您将获得这个超快...每个单元格只有在更改时才会被检查 -> 没有减速(至少:人类无法注意到):P
标签: excel vba conditional-formatting