简体   繁体   中英

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

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:

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM