Excel VBA How to detect if something was pasted in a Worksheet

后端 未结 2 1466
我寻月下人不归
我寻月下人不归 2020-11-27 22:41

I\'ll start by saying that my experience with Excel and VBA is limited to what I saw in school. I have programming experience, but in other languages.

I have a file

相关标签:
2条回答
  • 2020-11-27 22:50

    Worksheet_Change event will do the job if you add a formula into cell which will never be overwritten. Let's say your data are pasted into A1 cell and occupied 5 columns. So, enter below formula into 6. column and row 1.

    =COUNTBLANK(A1:A1048576)
    

    Now, you're able to handle/detect paste event ;)

    0 讨论(0)
  • 2020-11-27 23:08
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim UndoList As String
    
      '~~> Get the undo List to capture the last action performed by user
      UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
    
      '~~> Check if the last action was not a paste nor an autofill
      If Left(UndoList, 5) = "Paste" Then
        'Do stuff
      End If
    End Sub
    

    This did the trick. For those who need something similar and know the size of their list @MaciejLos' answer would also work.

    0 讨论(0)
提交回复
热议问题