Range.ClearContents清除系统剪贴板 - 解决方法? (VBA / Excel)中 [英] Range.ClearContents clears the system clipboard - workaround? (VBA/Excel)

查看:1808
本文介绍了Range.ClearContents清除系统剪贴板 - 解决方法? (VBA / Excel)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在工作的dataview表需要重置视图的部分内容(为空白) 。历史上,我们遇到问题,用户表示此工作表清除系统剪贴板。

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.

使用 Range.ClearContents 清除这些值。

# Grab some data from cells onto the clipboard

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

这应该足以复制问题。

解决方法

我的问题是可能的解决方法,这是要做:

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

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

问题


  • 是否有一些明显的原因,为什么这不是一个合理的解决方法?

  • 是否有更多的规范方法来清除一组单元格的内容而不清除剪贴板?

  • 我只是使用 ClearContents 错误?

  • 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.

编辑:我应该提到我正在使用Excel 2007

I should mention that I'm using Excel 2007

推荐答案

更新:我刚刚尝试了以下代码,发现这里

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

这篇关于Range.ClearContents清除系统剪贴板 - 解决方法? (VBA / Excel)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆