excel vba通过鼠标获取用户select范围的范围并将其复制到剪贴板

我是新的Excelmacros。 我想复制选定的范围到剪贴板。

以下是半解决scheme存在的链接,它在popup式消息框中给出输出。

Excel VBA通​​过鼠标获取用户select范围的范围

就像是

Sub macro1() MsgBox Selection.Address(ReferenceStyle:=xlA1, _ RowAbsolute:=False, ColumnAbsolute:=False) End Sub 

但是,如果我想逃避popup并直接将结果复制到剪贴板?

例如:如果我已经从B15到E40和F23单元格中select了单元格,它将msg设置为“B15:E40,F23”,我想复制这个msg单元格,而不是这些选定单元格的单元格内容。

一个简单的Selection.Copy将选定的范围放置到剪贴板:)

跟进

要复制所选范围的单元格地址,请执行以下操作。

 '~~> Set a reference to Microsoft Forms Object Library Sub Sample() Dim strAddr As String Dim MyDataObj As New DataObject strAddr = Selection.Address '~~> This will put the address string in the Clipboard. To test this '~~> After you run this macro, press CTL - V in Notepad. MyDataObj.SetText strAddr MyDataObj.PutInClipboard End Sub 

更多后续

我想用正斜杠replace逗号?

正如在我的评论中提到的,用斜线replace逗号。

 strAddr = Selection.Address strAddr = Replace(strAddr, ",", "/")