【问题标题】:msgBox is not showing up on successful resultmsgBox 未显示成功结果
【发布时间】:2015-03-09 03:39:14
【问题描述】:

我有以下设置来复制列表并粘贴到工作表(数据)。我希望它在成功时显示一条消息,告诉我它是从哪一行开始粘贴的。但是,errmsg 会显示。

提前致谢

    Dim current As String
   current = ActiveCell.Index
    MsgBox current & "pasted there"

    Exit Sub
errmsg:
    MsgBox "failed to copy."
End Sub

完整代码

Sub move()
    Range("A3:B3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Range("K3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Range("F3:I3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

    Range("F3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

    Range("A3:G3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    On Error GoTo errmsg
    Sheets("data").Select
    Range("A1").Select
    Selection.End(xlDown).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Dim current As String
   current = ActiveCell.Index
    MsgBox current & "pasted there"

    Exit Sub
errmsg:
    MsgBox "failed to copy."
End Sub

【问题讨论】:

  • 范围对象没有Index property or method。这可能会导致@Pillgram 在他的帖子中提到的错误。您可以尝试ActiveCell.Row 来获取您粘贴值的行。
  • 我使用 .Row 而不是 .address 但两者都有效。谢谢。

标签: excel msgbox vba


【解决方案1】:

欢迎来到 SO:) 尝试调试此问题的一种方法是删除/注释掉“On Error GoTo”,然后运行代码。这应该告诉你哪一行产生了错误。 也就是说,我怀疑您希望 current = ActiveCell.Index 成为 current = ActiveCell.Address

【讨论】:

  • 我使用 .Row(@L42 建议)而不是 .address,但两者都有效。谢谢
猜你喜欢
  • 2021-10-14
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多