snasui wrote: Mon May 28, 2018 6:47 am

ควรแนบตัวอย่างไฟล์มาด้วยจะได้สะดวกในการทดสอบ ทั้งจะได้เห็นว่าเขียน Code ไว้ตำแหน่งใด อย่างไรครับ
code ที่ผมเขียน ผมได้เขียนไว้ในนี้ ครับ
- เปีด Microsoft Visual Basic ขื้นมา แล้วไปที่ ThisWorkbook
- เขียน Code ตัวนี้ลงไปครับ
Code: Select all
Private Sub Workbook_Activate()
Dim oCtrl As Office.CommandBarControl
'Disable all Cut menus
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.Enabled = False
Next oCtrl
'Disable all Copy menus
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.Enabled = False
Next oCtrl
'Disable Paste in Edit menu
For Each oCtrl In Application.CommandBars.FindControls(ID:=22)
oCtrl.Enabled = False
Next oCtrl
'Disable Paste Special...
For Each oCtrl In Application.CommandBars.FindControls(ID:=755)
oCtrl.Enabled = False
Next oCtrl
'Disable Paste button
For Each oCtrl In Application.CommandBars.FindControls(ID:=6002)
oCtrl.Enabled = False
Next oCtrl
'Disable Tools, Options so D&D cannot be restored
For Each oCtrl In Application.CommandBars.FindControls(ID:=522)
oCtrl.Enabled = False
Next oCtrl
Application.CellDragAndDrop = False
Application.CutCopyMode = False 'Clear clipboard
End Sub
Private Sub Workbook_Deactivate()
Dim oCtrl As Office.CommandBarControl
'Enable all Cut menus
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.Enabled = True
Next oCtrl
'Enable all Copy menus
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.Enabled = True
Next oCtrl
'Enable Paste in Edit menu
For Each oCtrl In Application.CommandBars.FindControls(ID:=22)
oCtrl.Enabled = True
Next oCtrl
'Enable Paste Special...
For Each oCtrl In Application.CommandBars.FindControls(ID:=755)
oCtrl.Enabled = True
Next oCtrl
'Enable Paste button
For Each oCtrl In Application.CommandBars.FindControls(ID:=6002)
oCtrl.Enabled = True
Next oCtrl
'Enable Tools, Options so D&D cannot be restored
For Each oCtrl In Application.CommandBars.FindControls(ID:=522)
oCtrl.Enabled = True
Next oCtrl
Application.CellDragAndDrop = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
With Application
.CellDragAndDrop = False
.CutCopyMode = False 'Clear clipboard
End With
End Sub
ผลที่ออกมาได้ตามต้องการ แต่ยังติดที่ว่า
- เมื่อเรา copy or cut จากที่อื่นที่ไม่ใช่อยู่ใน file excel ตัวที่ผมได้ใส่ code ลงไป มาวางลงใน file excel ตัวที่ผมได้ใส่ code ลงไป
เห็นได้ว่ามันสามารถวางได้ครับ
- คือที่ผมค้องการคืไม่ให้วางอะไรลงในไฟล์ได้เลย ครับ