【发布时间】:2019-09-19 09:01:57
【问题描述】:
我正在编写一个 excel vba 代码来导入和操作 CSV 文件中的一些数据。突然间,我的一部分代码不再工作了,尽管它以前可以正常工作。
关于 range.select 和之后的 selection.Interior.Pattern = xlSolid
我曾尝试将相同的一小部分代码复制到不同的工作簿,在这里它工作得非常完美。
Dim iPhase As Integer
iPhase = Application.WorksheetFunction.CountIf(Range("A:A"), "Phase")
Dim h As Integer
h = 1
Range("A6").Select
Do Until h > iPhase
Cells.Find(What:="Phase", after:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With selection.Interior
.Pattern = xlSolid
.Interior.PatternColorIndex = xlAutomatic
.Interior.ThemeColor = xlThemeColorAccent6
.Interior.TintAndShade = 0
.Interior.PatternTintAndShade = 0
End With
With selection.Font
.Bold = True
End With
h = h + 1
Loop
我得到一个编译错误:预期的函数或变量@"selection.interior"
【问题讨论】:
-
不要使用
.Select和.Selection:How to avoid using Select in Excel VBA。 -
还有两件事 [1.] 你假设
Cells.Find将返回一个结果。检查它是否发现了什么。您可能希望看到THIS 了解如何做到这一点[2.]I get a compile error: Expected function or variable @"selection.interior"如果您复制代码并按原样粘贴,那么您的selection有一个小的s。这意味着您已经声明了一个具有相同名称的变量/模块/类等。你也需要避免这种情况......