Option Explicit
#If VBA7 Then
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long
Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
#Else
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
#End If
Function GetCutCopyRange() As Range
#If VBA7 Then
Dim hClipMem As LongPtr, lMemSize As LongPtr, lMemPtr As LongPtr
#Else
Dim hClipMem As Long, lMemSize As Long, lMemPtr As Long
#End If
Const CF_LINK = &HC215&
Dim bytBuffer() As Byte
Dim oSheet As Worksheet
Dim sClipLinkString As String, sRangeAddr As String, sSheetName As String, sWbookName As String
On Error GoTo errHandler
DoEvents
If OpenClipboard(0) Then
hClipMem = GetClipboardData(CF_LINK)
If hClipMem Then lMemSize = GlobalSize(hClipMem)
If lMemSize Then lMemPtr = GlobalLock(hClipMem)
If lMemPtr Then
ReDim bytBuffer(0 To CLng(lMemSize) - 1) As Byte
Call CopyMemory(bytBuffer(0), ByVal lMemPtr, lMemSize)
sClipLinkString = strConv(bytBuffer, vbUnicode)
sClipLinkString = Right(sClipLinkString, Len(sClipLinkString) - InStrRev(sClipLinkString, "[") + 1)
sClipLinkString = Replace(sClipLinkString, vbNullChar & vbNullChar, "")
sRangeAddr = Split(sClipLinkString, vbNullChar)(1)
sRangeAddr = Application.ConvertFormula(sRangeAddr, xlR1C1, xlA1)
sSheetName = Split(Split(sClipLinkString, vbNullChar)(0), "]")(1)
sWbookName = Split(Split(sClipLinkString, vbNullChar)(0), "]")(0) & "]"
sWbookName = Replace(Replace(sWbookName, "[", ""), "]", "")
Set oSheet = CallByName(Workbooks(sWbookName).Sheets, "Item", VbGet, sSheetName)
Set GetCutCopyRange = CallByName(oSheet, "Range", VbGet, sRangeAddr)
Call GlobalUnlock(hClipMem)
End If
Call CloseClipboard
End If
errHandler:
Call GlobalUnlock(hClipMem)
Call CloseClipboard
End Function