如果剪贴板包含Excel工作表范围,则可以使用DataObject对象访问该范围的数据
您还能找到该数据的实际源范围(即工作表,行和列)吗?
或者,您是否可以找到Last Copyed范围,该范围以虚线轮廓线表示(NOT所选范围)?
最好使用Excel 2003 VBA
Sub testClipborard()
Dim test As String
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
test = clipboard.GetText
MsgBox (test)
End Sub
请注意,您需要对Microsoft Forms 2.0库进行引用才能使其运行(并且,如果您在单元格中没有值,它也会失败)。
话虽如此,您可以尝试类似以下的操作-将其添加到VBA编辑器中的模块中。
Public NewRange As String Public OldRange As String Public SaveRange As String Public ChangeRange As Boolean
并且在工作表对象中使用以下内容
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) 'save previous selection OldRange = NewRange 'get current selection NewRange = Selection.Address 'check if copy mode has been turned off If Application.CutCopyMode = False Then ChangeRange = False End If 'if copy mode has been turned on, save Old Range If Application.CutCopyMode = 1 And ChangeRange = False Then 'boolean to hold "SaveRange" address til next copy/paste operation ChangeRange = True 'Save last clipboard contents range address SaveRange = OldRange End If End Sub
貌似可行,但是,当它试图解决剪贴板问题时,也很容易出现不同的错误。 http://www.ozgrid.com/forum/showthread.php?t=66773
'2020-02-02
Private Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal lngFormat As Long) As LongPtr
Private Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
'get range link from clipboard
Function fGetClipRange() As Range
Dim strGetClipRange As String 'return range
Dim lptClipData As LongPtr 'pointer to clipboard data
Dim strClipData As String 'clipboard data
Dim intOffset As Integer 'for parsing clipboard data
Const lngRangeLink = 50123 'clipboard format
Const intMaxSize As Integer = 256 'limit for r1c1 to a1 conversion
On Error Resume Next
If OpenClipboard(0&) = 0 Then GoTo conDone 'could not open clipboard
lptClipData = GetClipboardData(lngRangeLink) 'pointer to clipboard data
If IsNull(lptClipData) Then GoTo conDone 'could not allocate memory
lptClipData = GlobalLock(lptClipData) 'lock clipboard memory so we can reference
If IsNull(lptClipData) Then GoTo conDone 'could not lock clipboard memory
intOffset = 0 'start parsing data
strClipData = Space$(intMaxSize) 'initialize string
Call lstrcpy(strClipData, lptClipData + intOffset) 'copy pointer to string
If strClipData = Space$(intMaxSize) Then GoTo conDone 'not excel range on clipboard
strClipData = Mid(strClipData, 1, InStr(1, strClipData, Chr$(0), 0) - 1) 'trim null character
If strClipData <> "Excel" Then GoTo conDone 'not excel range on clipboard
intOffset = intOffset + 1 + Len(strClipData) 'can't retrieve string past null character
strClipData = Space$(intMaxSize) 'reset string
Call lstrcpy(strClipData, lptClipData + intOffset) 'book and sheet next
strClipData = Mid(strClipData, 1, InStr(1, strClipData, Chr$(0), 0) - 1)
strGetClipRange = "'" & strClipData & "'!" 'get book and sheet
intOffset = intOffset + 1 + Len(strClipData) 'next offset
strClipData = Space$(intMaxSize) 'initialize string
Call lstrcpy(strClipData, lptClipData + intOffset) 'range next
strClipData = Mid(strClipData, 1, InStr(1, strClipData, Chr$(0), 0) - 1)
strGetClipRange = strGetClipRange & strClipData 'add range
strGetClipRange = Application.ConvertFormula(strGetClipRange, xlR1C1, xlA1)
Set fGetClipRange = Range(strGetClipRange) 'range needs a1 style
conDone:
Call GlobalUnlock(lptClipData)
Call CloseClipboard
End Function
'get cut or copy from clipboard
Function fGetCopyCut() As String
Dim lptClipData As LongPtr 'pointer to clipboard data
Dim strClipData As String 'clipboard data
Const intMaxSize As Integer = 32 'more than we need
Const lngCopyCut = 129 'clipboard format
On Error Resume Next
If OpenClipboard(0&) = 0 Then GoTo conDone
lptClipData = GetClipboardData(lngCopyCut) 'get copy or paste
If IsNull(lptClipData) Then GoTo conDone
lptClipData = GlobalLock(lptClipData) 'lock clipboard memory so we can reference
If IsNull(lptClipData) Then GoTo conDone
strClipData = Space$(intMaxSize)
Call lstrcpy(strClipData, lptClipData)
If strClipData = Space$(intMaxSize) Then GoTo conDone 'no data
strClipData = Mid(strClipData, 1, InStr(1, strClipData, Chr$(0), 0) - 1)
fGetCopyCut = LCase(Left(strClipData, InStr(strClipData, " ") - 1))
conDone: 'return copy or cut
Call GlobalUnlock(lptClipData)
Call CloseClipboard
End Function