excel剪切、复制和粘贴操作,使它们不会破坏已设置的单元格格式
‘初始化单元格的复制-粘贴Public Sub InitCutCopyPaste()Application.onKey “^X”, “DoCut”Application.onKey “^x”, “DoCut”Application.onKey “+{DEL}”, “DoCut”
Application.onKey “^C”, “DoCopy”Application.onKey “^c”, “DoCopy”Application.onKey “^{INSERT}”, “DoCopy”
Application.onKey “^V”, “DoPaste”Application.onKey “^v”, “DoPaste”Application.onKey “+{INSERT}”, “DoPaste”
Application.onKey “{ENTER}”, “DoPaste”Application.onKey “~”,”DoPaste”
‘关闭拖放操作Application.CellDragAndDrop = FalseEnd Sub
‘处理剪切单元格Public Sub DoCut()If TypeOf Selection Is Range ThenmbCut = TrueSet mrngSource = SelectionSelection.CopyElseSet mrngSource = NothingSelection.CutEnd IfEnd Sub
‘处理复制单元格Public Sub DoCopy()If TypeOf Selection Is Range ThenmbCut = FalseSet mrngSource = SelectionElseSet mrngSource = NothingEnd If
Selection.CopyEnd Sub
‘处理粘贴单元格Public Sub DoPaste()If Application.CutCopyMode And Not mrngSource Is Nothing ThenSelection.PasteSpecial xlValuesIf mbCut ThenmrngSource.ClearContentsEnd If
Application.CutCopyMode = FalseElseActiveSheet.PasteEnd IfEnd Sub将上述代码复制到VBE中,试着运行并熟悉其原理。可以将上述程序嵌入到你正在开发的应用程序的合适位置,结合实际稍作调整,使应用程序更健壮。