【发布时间】:2020-07-06 18:20:47
【问题描述】:
首先,我知道这是一篇很长的帖子,但我不知道问题出在哪里,也不想用我没有包含的代码不断更新每个人,所以我包含了所有内容。可以查看一些代码,因为它没有问题,我提到这一点是为了帮助它更快。谢谢一百万。
背景:大家好!在工作中,我们有一个电子表格,用户可以在其中选择 1 到 2 个计算器来计算一些数字并确定公寓的成本应该是多少。我的任务是为电子表格创建一个 SOP。我认为将 cmets 添加到不同的单元格,然后添加 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 中显示 cmets。相反,活动单元格转到 F4,消失,然后添加注释(看不见,但您可以看到红色三角形)。它等待一段时间,然后在接下来的 3 行中添加红色三角形,但不显示 cmets。以下是它不能正确执行的其余代码(包括 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个(未隐藏的)cmets,添加和删除一张图片以显示右键菜单,然后删除4个cmets。虽然它确实有点(减去显示 cmets),但它正确地触发了“相当酷”的 msgbox 并删除了 F4:F7 cmets。下一部分代码适当地发生:
'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 (+/=)”字段在能够打印之前(通过 beforeprint 宏处理)。虽然联合选择和事件宏有效,但在 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
【问题讨论】:
-
我过去调试过这个问题,我试图记住解决方案是什么。基本上,在中断模式下运行代码和“正常”运行代码之间存在(微小的)差异。你有一些选择,你确定当你在中断模式下运行代码时,你从调用代码的同一页面开始吗?
-
我首先想到的是 Application.Wait。这可能会对实时执行与逐步执行产生影响。尝试注释掉所有 Application.Wait 行,然后再次运行比较。
-
@Selkie 是的,我只保留一张纸,所以我只从这里开始。实际上还有一个按钮来运行这个宏,以确保它只从正确的工作表中触发。克里斯:我认为这可能是等待,所以我试图在我的一个测试中跳过它们,但只能通过移动 VBA 中的活动线箭头来跳过它。我会尝试评论所有等待时间,看看我能找到什么。谢谢你们两个!
-
关于取出 application.waits,程序运行得很快,我无法确定到底发生了什么。我尝试在 application.wait 周围查找一些方法,并发现一些没有帮助的循环。然后我偶然发现了analystcave.com/vba-sleep-vs-wait,虽然我不太了解 DoEvents 的作用,但我用 DoEvents 替换了我所有的 application.waits 并且我没有看到的一些现在正在显示,但相反,一些正在工作现在不工作。将尝试结合 application.wait 和 doevents。