VBA for Excel - 选择返回“编译错误:预期的函数或变量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19190355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:40:35  来源:igfitidea点击:

VBA for Excel - Selection returns "Compile Error: Expected Function or variable"

excelvbaexcel-vba

提问by user2848029

So I've been messing around with some VBA code and I am trying to apply a border to a range of three cells. When I record the code using the VBA recorder built in to Excel I get this back:

所以我一直在处理一些 VBA 代码,我试图将边框应用于三个单元格的范围。当我使用 Excel 内置的 VBA 记录器记录代码时,我得到了这个:

Range("A1").Select
ActiveCell.Offset.Range("A1:A3").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With

And so on with the other sides.

以此类推。

My issue is that as soon as I try to run the code again it stops before executing anything, highlights the first selection (in the blue active text way, not the yellow step into way), and opens a message box saying "Compile Error: Expected Function or variable". I've looked at my other sections of code that I've already written and they are also having this problem now, which I don't think they were doing yesterday.

我的问题是,一旦我尝试再次运行代码,它会在执行任何操作之前停止,突出显示第一个选择(以蓝色活动文本方式,而不是黄色步骤),并打开一个消息框,上面写着“编译错误:预期的函数或变量”。我已经查看了我已经编写的其他代码部分,他们现在也遇到了这个问题,我认为他们昨天没有这样做。

I'm trying to figure out if I clicked a wrong button yesterday or what but my code is completely unusable now for some reason.

我想弄清楚我昨天是否点击了错误的按钮,或者我的代码由于某种原因现在完全无法使用。

回答by rory.ap

Just a guess, but if you go to Tools > References, are there any that are checked that say "MISSING"? If so, try unchecking the reference. I'm not sure why, but I've come across that in the past where a missing or lost references makes otherwise-normal code behave wacky.

只是猜测,但是如果您转到“工具”>“参考资料”,是否有任何已检查的内容显示“丢失”?如果是这样,请尝试取消选中引用。我不知道为什么,但我在过去遇到过,丢失或丢失的引用会使其他正常的代码表现得很古怪。

回答by David Zemens

I don't see anything obvious but you can revise this a bit from the "recorded" version:

我没有看到任何明显的东西,但你可以从“记录”版本中稍微修改一下:

With Range("A1:A3")
    .Borders(xlDiagonalDown).LineStyle = xlNone
    .Borders(xlDiagonalUp).LineStyle = xlNone
    With .Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
End With

回答by Edward

Due to high memory usage, I turned something off in my Excel; since then, memory usage is very low (in hundreds MB instead of GB). I'm pretty sure this feature turned off has something to do with it.

由于内存使用率高,我在 Excel 中关闭了一些功能;从那时起,内存使用率非常低(数百 MB 而不是 GB)。我很确定关闭此功能与它有关。