简体   繁体   English

通过按钮调用时宏无法正常工作

[英]Macro not working right when called through a button

Using Excel 07 使用Excel 07

Below is the code I currently have working. 以下是我目前正在使用的代码。 However when I assign it to a code the section where it clear contents does not run but everything else does. 但是,当我将其分配给代码时,清除内容的部分不会运行,而其他所有内容都会运行。 If I just F8 through it clears the contents no problem at all. 如果我只是按F8键,则清除内容完全没有问题。 I am not sure why this is and was hoping you could help. 我不确定为什么会这样,并希望您能提供帮助。

Sub Save_Session()

Call UnProtectAll

If Worksheets("Drawing").Range("A2").Value = Worksheets("Drawing").Range("I2").Value Then


i = (Worksheets("Drawing").Range("B2").Value * 25) - 24

Worksheets("Drawing").UsedRange.Copy Destination:=Worksheets("Sessions").Range("A" & i)

Worksheets("Drawing").Range("B2").Value = Worksheets("Drawing").Range("B2").Value + 1

    With Sheets("Drawing")
        Range("A2").ClearContents
        Range("C2:C20").ClearContents
        Range("E2:H20").ClearContents
    End With

Else

MsgBox "You do not have all the winners yet!!!"

End If

Sheets("Drawing").Select
Call ProtectAll

End Sub

It's probably working when you step through coincidentally because you happen to be on the sheet "Drawing" when running the macro. 巧合地逐步执行时可能会起作用,因为运行宏时碰巧在图纸“绘图”上。

To access properties or parameters of an object, you need to use the "." 要访问对象的属性或参数,需要使用“。”。 Since you are already declaring what you are trying to access with the WITH statement. 由于您已经在声明要使用WITH语句访问的内容。

Here is an article from MSDN explaining how the WITH...End statement works In VBA. 这是MSDN上的一篇文章,解释WITH ... End语句在VBA中的工作方式。

With Sheets("Drawing")
    .Range("A2").ClearContents
    .Range("C2:C20").ClearContents
    .Range("E2:H20").ClearContents
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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