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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11177355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:35:39  来源:igfitidea点击:

excel vba get range of user selected range by mouse and copy it to clipboard

excelexcel-vbavba

提问by namit

I am new to excel macros. I want to copy the selected range to clip board.

我是 excel 宏的新手。我想将选定的范围复制到剪贴板。

Below is the link where half solution is present, it gives output in popup msg box.

下面是存在半解决方案的链接,它在弹出的 msg 框中给出输出。

Excel VBA get range of user selected range by mouse

Excel VBA 通过鼠标获取用户选择范围的范围

its like

就像是

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

But if i want to escape the popup and directly copies the result to clipboad????

但是如果我想逃避弹出窗口并直接将结果复制到剪贴板????

example: if i have selected the cells from B15 to E40 and F23 cell, it gives the msg as "B15:E40,F23", and i want to copy this msg, not the cell content of these selected cells.

例如:如果我选择了从 B15 到 E40 和 F23 单元格的单元格,它给出的 msg 为“B15:E40,F23”,我想复制这个 msg,而不是这些选定单元格的单元格内容。

回答by Siddharth Rout

A simple Selection.Copywill place the selected range to clipboard :)

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

FOLLOWUP

跟进

To copy the Cell Address of the selected range do this.

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

'~~> 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

MORE FOLLOWUP

更多跟进

i want to replace commas with forward slash????

我想用正斜杠替换逗号????

As mentioned in my comment, replace the comma with the Slash.

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

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