Range.ClearContents clears the system clipboard - workaround? (VBA/Excel)

对着背影说爱祢 提交于 2019-12-11 02:47:06

问题


Problem

A dataview sheet I'm working on regularly wants to reset the contents of parts of the view (to be blank). Historically, we've had problems with users saying this sheet clears their system clipboard.

It looks like there's a problem with using Range.ClearContents to clear these values.

# Grab some data from cells onto the clipboard

Sub ClearTheClipboardWhenTheUserIsntExpectingIt()
    Cells(1, 1).EntireRow.ClearContents    ' Or something like that
End Sub

This should be enough to replicate the problem.

Workaround

My question is about a possible workaround, which is to do:

Dim r as Range
...
r.Value2 = Empty

Question(s)

  • Is there some obvious reason why this is not a reasonable workaround?
  • Is there some more canonical way to clear the contents of a set of cells without clearing the clipboard?
  • Am I just using ClearContents wrong?

I'm loathed to go through a large codebase and search/replace this behaviour if it's going to turn out later that I have to go back and fix it because I've introduced some bug.

Edit: I should mention that I'm using Excel 2007


回答1:


Update: I have just tried the below code, found here, in a vanilla Excel spreadsheet with success:

Sub Button1_Click()

Dim clipboardText As String
clipboardText = GetTextFromClipboard()

Cells(1, 1).EntireRow.ClearContents

CopyTextToClipboard (clipboardText)

End Sub

Sub CopyTextToClipboard(ByVal inText As String)

Dim objClipboard As Object
Set objClipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objClipboard.SetText inText
objClipboard.PutInClipboard
Set objClipboard = Nothing

End Sub


Function GetTextFromClipboard() As String

Dim objClipboard As Object
Set objClipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objClipboard.GetFromClipboard
GetTextFromClipboard = objClipboard.GetText
Set objClipboard = Nothing

End Function



回答2:


The only thing I notice is that it .ClearContents clears the MSO clipboard, which is different than Windows's clipboard. This is because .ClearContents for some reason sets CutCopyMode to False.

I would recommend either of these:

Sub test()
    Cells(1, 1).EntireRow.Value = vbNullString
End Sub

Sub test3()
    Cells(1, 1).EntireRow.Value = Empty
End Sub

Although I like brettdj's method better just because it's cleaner (Rows(1).Value = vbNullstring).

Also, from the help files:

Empty

Indicates that no beginning value has been assigned to a Variant variable. An Empty variable is represented as 0 in a numeric context or a zero-length string ("") in a string context.

Apparently when a cell value is set to Empty, it is considered a string value I supposed because when I tested it, it didn't set the cell to 0. So, when applying Empty or vbNullString to a cell value, there is pretty much no difference between them.



来源:https://stackoverflow.com/questions/13988783/range-clearcontents-clears-the-system-clipboard-workaround-vba-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!