执行vba宏后保留原始excel剪贴板历史记录

时间:2015-09-05 13:50:42

标签: excel-vba clipboard vba excel

背景信息

我使用VBA宏来执行几个单步骤的序列,以便在我的工作表中交换两行的内容。实际上,我因此能够逐行向上移动所选行,因此相对于其他行更改其行顺序。要执行此宏,我设置了键盘快捷键(或按钮):

<?php echo '<div class="description">' . substr($data['description'], strpos($data['description'], $info['title']), 100) . '</div>'; ?>

由于选择,剪切和插入单元格内容,宏显然会修改剪贴板。现在,我希望在宏执行后剪贴板历史记录保持不变。从而使用户能够反转/取消/撤回宏所做的事情,甚至在全局剪贴板历史记录中进一步返回。在我的Windows系统上,我通常按Sub MoveOneRowUp() Dim rowCurrent As Integer Dim colCurrent As Integer ' save current row coordinates rowCurrent = ActiveCell.Row colCurrent = ActiveCell.Column ' check validity If rowCurrent > 1 Then ' select row above active cell Rows(rowCurrent - 1).Select ' cut out entire row Selection.Cut ' select Rows(rowCurrent + 1).Select ' do the shift Selection.Insert Shift:=xlDown ' select previously current cell Cells(rowCurrent - 1, colCurrent).Select End If End Sub 来执行此操作。但是在宏执行之后,所有剪贴板历史都消失了,我不能再反转任何东西了。

问题

在描述的情况下,无论宏执行如何,我都可以保留剪贴板历史记录?关于EXCEL的剪贴板历史机制通常如何工作,是否有一些提示?

感谢。

2 个答案:

答案 0 :(得分:2)

剪贴板可能包含各种复杂的东西,因此保存任何内容的可靠方法最多也是困难的。我相信你应该避免尝试这种方法,直到你确信没有其他方法会给你你想要的东西

尝试在不更改剪贴板的情况下运行宏的问题是Excel清除剪贴板而不复制任何内容。我不知道Excel是否使用剪贴板本身,或者如果您执行某些操作它会自动清除剪贴板。

我尝试了一个宏,它将行(当前 - 1)复制到备用行,将行(当前)复制到行(当前 - 1),将备用行复制到行(当前),然后清除备用行。这会根据您的需要将当前行向上移动一行,但清除了剪贴板。

下面的宏将两行复制到变量,然后将值反复复制。根据我的测试,这不清楚剪贴板。这个宏不会复制任何格式,但如果所有行的格式都相同,这可能就足够了。

 Sub MoveOneRowUp()

  ' Integer specifies a 16-bit variable which requires special (=slow) processing
  ' on 32 and 64-bit computers.  Perhaps more importantly, the maximum value that
  ' can be held in an integer variable is 32767 so make sure your worksheets
  ' don't have too many rows.
  Dim rowCurrent As Long
  Dim colCurrent As Long
  Dim rowContents1 As Variant
  Dim rowContents2 As Variant

  ' save current row coordinates
  rowCurrent = ActiveCell.Row
  colCurrent = ActiveCell.Column

  ' check validity
  If rowCurrent > 1 Then

    ' Copy row contents to variants then copy back reversed
    rowContents1 = Rows(rowCurrent - 1).Value
    rowContents2 = Rows(rowCurrent).Value
    Rows(rowCurrent).Value = rowContents1
    Rows(rowCurrent - 1).Value = rowContents2

    ' select previously current cell
    Cells(rowCurrent - 1, colCurrent).Select

  End If

End Sub

答案 1 :(得分:0)

这些潜艇将允许您使用 Ctrl + Z (撤消)和 < kbd> Ctrl + Y (重做)快捷方式:

Public Sub MoveOneRowUp()
   Dim thisRow As Long

   thisRow = ActiveCell.Row

   With ThisWorkbook.ActiveSheet.UsedRange
      If thisRow > 2 And thisRow < .Rows.Count + 1 Then  'check upper & lower bounds
         Application.ScreenUpdating = False

         'insert new row
         .Rows(thisRow - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
         .Rows(thisRow - 1).Value2 = .Rows(thisRow + 1).Value2    'copy data
         .Rows(thisRow + 1).EntireRow.Delete                      'remove initial row

         .Cells(thisRow - 1, ActiveCell.Column).Activate
         Application.OnRepeat "Move One Row Up", "MoveOneRowUp"   'updates Repeat action
         Application.OnUndo "Move One Row Down", "MoveOneRowDown" 'updates Undo action
         Application.ScreenUpdating = True
      End If
   End With
End Sub
Public Sub MoveOneRowDown()
   Dim thisRow As Long

   thisRow = ActiveCell.Row

   With ThisWorkbook.ActiveSheet.UsedRange
      If thisRow > 1 And thisRow < .Rows.Count Then      'check upper & lower bounds
         Application.ScreenUpdating = False

         'insert new row
         .Rows(thisRow + 2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
         .Rows(thisRow + 2).Value2 = .Rows(thisRow + 0).Value2    'copy data
         .Rows(thisRow + 0).EntireRow.Delete                      'remove initial row

         .Cells(thisRow + 1, ActiveCell.Column).Activate
         Application.OnRepeat "Move One Row Up", "MoveOneRowUp"   'updates Repeat action
         Application.OnUndo "Move One Row Down", "MoveOneRowDown" 'updates Undo action
         Application.ScreenUpdating = True
      End If
   End With
End Sub

MSDN

的详细信息