VBA执行代码时不会出错,并且在逐步执行时可以正确执行,但在自行运行时则不能执行

时间:2018-07-26 20:04:42

标签: excel-vba

首先,我知道这是一篇很长的文章,但是我无法弄清楚问题出在哪里,也不想继续使用未包含的代码来更新每个人,所以我包含了所有内容。可以查看一些代码,因为它没有问题,我提到这是为了使其更快。谢谢你一百万。

背景:大家好!在工作中,我们有一个电子表格,用户可以在其中选择1-2个计算器来计算一些数字并确定公寓的价格。我的任务是为电子表格创建SOP。我以为在不同的单元格中添加注释,然后添加application.wait将是一个不错的选择,这样它们就可以留在工作簿VS中,而不必告诉它们,而不能通过Word文档显示。

问题:当我单步执行宏时,一切都会按预期运行。但是,当我尝试一次全部运行该程序时,许多代码行都没有按照我认为的方式执行。

这是代码的开头,按预期工作...

Sub Macro1()
Dim rng1 As Range, rng2 As Range

MsgBox "This tour will auto play and show you how to go about using the Rate Calculator. Let's begin!"

'1 Selecting a calculator
Range("A2").AddComment
Range("A2").Comment.Visible = True
Range("A2").Comment.Text Text:= _
    "Start by selecting the type of rate calculator you would like to use. Do so by typing in your selection or using the drop down menu."
Range("A2").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:07"))
Range("A2").Comment.Delete

我还有另一个子程序在A2:A3中查找事件更改,因此我将其打开以允许该宏运行...

'2 After calculator selection
Application.EnableEvents = True
Range("A2").Value = "Match Lease"
Application.EnableEvents = False

Range("A2").AddComment
Range("A2").Comment.Visible = True
Range("A2").Comment.Text Text:= _
    "Once we've selected an option, 'Match Lease' in this case, the appropriate calculator becomes visible. Fill in the appropriate information in the gray boxes to obtain your daily rate."
Range("A2").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:09"))
Range("A2").Comment.Delete

最后三行是开始变得奇怪的地方。出现“一旦选择”注释,但在适当的时候不会删除。然后,即将出现的代码应开始在F4中显示注释。相反,活动单元转到F4,消失,然后添加注释(看不见,但您可以看到红色三角形)。它会稍等片刻,然后在接下来的3行中添加红色三角形,但不显示注释。这是其余代码无法正常运行的部分(包括F4部分):

'3 Explain comments
Range("F4").AddComment
Range("F4").Comment.Visible = True
Range("F4").Comment.Text Text:= _
    "Let's look at adding comments. At times you may need to provide a brief explanation of a cells contents. Right click in the appropriate cell then select 'Insert Comment'."
Range("F4").Comment.Shape.TextFrame.AutoSize = True

ActiveSheet.Pictures.Insert("C:...png").Select
Selection.ShapeRange.IncrementLeft 109.5
Selection.ShapeRange.IncrementTop 10.5
Application.Wait (Now + TimeValue("0:00:07"))
ActiveSheet.Pictures.Delete
Range("F4").Comment.Text Text:= _
    "Excel is not likely to size the box around your comments correctly. And at times, you may even have comments that overlap each other. Excel also requires the end-user " & Chr(10) & "to right-click each cell individually to show or hide comments in different cells. All of this is a piece of cake thanks to the buttons found below your 'Tour' button."
Range("F4").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:15"))
Range("F4").Comment.Text Text:= _
    "I'm going to add a few comments to show you how they might look with no added effort other than adding and typing in the comment."
Range("F4").Comment.Shape.TextFrame.AutoSize = True

Range("F5").AddComment
Range("F5").Comment.Visible = True
Range("F5").Comment.Text Text:= _
    "This is comment 1. This is comment 1."
Range("F6").AddComment
Range("F6").Comment.Visible = True
Range("F6").Comment.Text Text:= _
    "This is comment 2. This is comment 2."
Range("F7").AddComment
Range("F7").Comment.Visible = True
Range("F7").Comment.Text Text:= _
    "Here's comment 3 but I can't see all of the two previous comments! I'm going to give you a little time to finish reading then I'll select the 'Autofit and Space All Comments Button'"
Application.Wait (Now + TimeValue("0:00:10"))

Call AutoFitAndAddSpaceToComments

Range("F4").Comment.Text Text:= _
    "What a relief! Who has time to re-adjust boxes all day? UPS maybe?"
Range("F4").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:06"))
Range("F4").Comment.Text Text:= _
    "Do you have comments that are in the way while you work and would like to hide them? Or do you have hidden comments you need to view? (*Comments need to be viewed in order to print*)" & Chr(10) & "Simply use the 'Show / Hide All Comments' button. I'll do that for you in a few moments..."
Range("F4").Comment.Shape.TextFrame.AutoSize = True

Call ShowHideComments

MsgBox "Pretty cool, eh? Notice the red triangles in the cells? This means there's a comment in the cell. Hovering over it will display the message until you un-hover. Let's get rid of these comments and continue our tour!"
Call ShowHideComments
Range("F4").Comment.Delete
Range("F5").Comment.Delete
Range("F6").Comment.Delete
Range("F7").Comment.Delete

使用上面的代码,我们想要添加4条(未隐藏)注释,添加和删除图片以显示右键菜单,然后删除4条注释。尽管确实有所作为(减去了显示的注释),但是它正确地触发了“非常酷”的msgbox并删除了F4:F7注释。下一部分代码将适当地发生:

'4 Discuss helpful hints
Range("F27").AddComment
Range("F27").Comment.Visible = True
Range("F27").Comment.Text Text:= _
    "Notice that when your active cell is at F27 or F28, a tip is provided to remind you to select an option in the drop down to the left of your current, active cell."
Range("F27").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:11"))
Range("F27").Comment.Delete

下一部分通常有效。它应在D41中显示一个注释,并在两次选中和取消选中两个框之间来回移动,以帮助向用户显示这些框的位置。第一个框被选中,等待一段时间,然后最终取消选中它(但不会被选中两次,第二个框则永远不会被选中。)

'5 Discuss checkboxes
Range("D41").AddComment
Range("D41").Comment.Visible = True
Range("D41").Comment.Text Text:= _
    "When you're done filling in all necessary gray boxes, place a checkmark in the appropriate box to indicate whether the client will be paying by check or credit card."
Range("D41").Comment.Shape.TextFrame.AutoSize = True
ActiveSheet.CheckBoxes("Check box 43").Value = True
Application.Wait (Now + TimeValue("0:00:02"))
ActiveSheet.CheckBoxes("Check box 43").Value = False
ActiveSheet.CheckBoxes("Check box 44").Value = True
Application.Wait (Now + TimeValue("0:00:02"))
ActiveSheet.CheckBoxes("Check box 44").Value = False
ActiveSheet.CheckBoxes("Check box 43").Value = True
Application.Wait (Now + TimeValue("0:00:02"))
ActiveSheet.CheckBoxes("Check box 43").Value = False
ActiveSheet.CheckBoxes("Check box 44").Value = True
Application.Wait (Now + TimeValue("0:00:02"))
ActiveSheet.CheckBoxes("Check box 44").Value = False
Range("D41").Comment.Delete

最后,我们设置2个不连续范围的并集以显示要引用的单元格,以便让员工知道如果其中任何一个具有值,则必须填写“ $ Amount(+ / =)”字段在能够打印之前(通过preprint宏处理)。在并集选择和事件宏有效的同时,在F102中添加我的注释无效。

'6 Explain printing won't work if table is partially filled out
With Sheets("Rate Calculator v6")
    Set rng1 = Range("A102:D102")
    Set rng2 = .Range("P102:Q102")
    Application.Union(rng1, rng2).Select
End With

Range("F102").AddComment
Range("F102").Comment.Visible = True
Range("F102").Comment.Text Text:= _
    "If any of the cells on this bottom table are filled in, Excel will not allow you to print until the '$ Amount (+/-)' field is entered."
Range("F102").Comment.Shape.TextFrame.AutoSize = True
Application.Wait (Now + TimeValue("0:00:11"))
Range("F102").Comment.Delete

如果是导致问题的原因之一是我所调用的宏,这是我的所在:

Sub AutoFitAndAddSpaceToComments()
Dim rngComments As Range, cell As Range
Dim Cmt As Comment
Dim wb As Workbook
Dim ws As Worksheet
Dim Cntr As Long
ActiveSheet.Unprotect "password"
Set rngComments = Sheets("Rate Calculator v6").Range("F3:N46").SpecialCells(xlCellTypeComments)
If rngComments.Count = 0 Then MsgBox "There are no comments."

For Each cell In rngComments                                                                                        'Run through all comments and...
cell.Comment.Shape.TextFrame.AutoSize = True                                                                'autofit
Next cell

For Each cell In rngComments                                                                                    'Run through all comments and...
If BottomCntr > cell.Comment.Shape.Top Then cell.Comment.Shape.Top = BottomCntr + 2                     'If bottom of last comment is greater than the     top of current comment, add 2 points
TopCntr = cell.Comment.Shape.Top
HeightCntr = cell.Comment.Shape.Height
BottomCntr = TopCntr + HeightCntr
'Debug.Print cell.Comment.Shape.Name; " Top:"; TopCntr; " Height:"; HeightCntr; " Bottom:"; BottomCntr
Next cell
ActiveSheet.Protect "password", "false"
End Sub


Sub ShowHideComments()
If Application.DisplayCommentIndicator = xlCommentIndicatorOnly Then
Application.DisplayCommentIndicator = xlCommentAndIndicator
Else
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End If
End Sub

1 个答案:

答案 0 :(得分:0)

我使用了睡眠功能代替了application.wait并获得了更好的成功。感谢您的建议! http://www.fmsinc.com/microsoftaccess/modules/examples/avoiddoevents.asp