VBA code to clear the clipboard

Anonymous
2021-01-22T16:41:28+00:00

After copying the Range A1:XFD1048576 and paste while I close the workbook I get the dialogue. Although I keep display alerts off. I want the clipboard to be cleared what is the code to clear clipboard. E.g Openworkbook = Nothing doesn't work.

Also OpenBook.Close False the open book is closed for both False and True

Sub Get_Data_From_File()

Dim FileToOpen As Variant

Dim OpenBook As Workbook

Application.ScreenUpdating = False

Application.DisplayAlerts = False

FileToOpen = Application.GetOpenFilename(Title:="Browse for the detailed CSV file", FileFilter:="CSV Files (*.csv*),*csv*")

If FileToOpen <> False Then

Set OpenBook = Application.Workbooks.Open(FileToOpen)

OpenBook.Sheets(1).Range("A1:XFD1048576").Copy

Windows("Macro Template.xlsm").Activate

ActiveSheet.Range("A1").Select

ActiveSheet.Paste

ActiveSheet.Range("A1").Select

OpenBook.Close False

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End If

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} vote
Answer accepted by question author
  1. Lz._ 38,106 Reputation points Volunteer Moderator
    2021-01-22T17:40:09+00:00

    Paste the following code (for 64-bit version) in a Module and where necessary call function ClearClipboard

    Option Explicit

    Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPtr

    Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr

    Private Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr

    Private Function ClearClipboard()

      OpenClipboard (0&)

      EmptyClipboard

      CloseClipboard

    End Function

    9 people found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2021-01-22T16:52:53+00:00

    Use

            Application.CutCopyMode = False

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2021-01-22T17:15:16+00:00

    It did not work I am still able to copy the same data into a new workbook. I also see in the clipboard it is still there.

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2021-01-22T18:48:32+00:00

    Image

    I am not able to call ClearClipboard it won't show up in the drop down. I also tried to copy paste without ().  

    Why variables are separated from the function procedure with a line?

    2 people found this answer helpful.
    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more