在 Excel 2010 中使用输入框复制和粘贴 - VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14691738/
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
Copy and Paste using Input Box in Excel 2010 - VBA
提问by Nick
Help needed. I'm a newbie to VBA and trying to start with simple macro. But even with that failing miserably. All i'm trying to do is copy and paste from one worksheet to another using an input box to specify the range to be copied. Now I know for the input box is:
需要帮助。我是 VBA 的新手,并试图从简单的宏开始。但即使失败了。我要做的就是使用输入框从一个工作表复制并粘贴到另一个工作表以指定要复制的范围。现在我知道输入框是:
Application.InputBox("Enter the range from which you want to copy : ", Type:=8)
But what do line do i need in order to copy to a cell in another worksheet?
但是我需要什么行才能复制到另一个工作表中的单元格?
I apologise in advance for being an idiot.
我为自己是个白痴提前道歉。
回答by Siddharth Rout
Is this what you are trying?
这是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim rngCopyFrom As Range
On Error Resume Next
Set rngCopyFrom = Application.InputBox("Enter the range from which you want to copy", Type:=8)
On Error GoTo 0
If Not rngCopyFrom Is Nothing Then
'~~> Copy the range to say Cell A1 of Sheets("weekly raw")
rngCopyFrom.Copy ThisWorkbook.Sheets("weekly raw").Range("A1")
End If
End Sub
回答by peterm
One way to do it is like this:
一种方法是这样的:
Sub CopyRange()
Dim FromRange As Range
Dim ToRange As Range
Set FromRange = Application.InputBox("Enter the range from which you want to copy : ", Type:=8)
Set ToRange = Application.InputBox("Enter the range to where you want to copy : ", Type:=8)
FromRange.Copy ToRange
'Or you can do it like this if you need some flexibility on Paste
'FromRange.Copy
'ToRange.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
End Sub
回答by SeanC
Here's some sample code:
这是一些示例代码:
Option Explicit
Sub CopyStuff()
Dim x As Range
Dim y As Range
Set x = Application.InputBox("Select what copy using the mouse", Type:=8)
Set y = ActiveWorkbook.Sheets("Sheet2").Range("A1")
x.Copy y
End Sub