使用Excel 2010中的输入框复制和粘贴 - VBA

时间:2013-02-04 17:09:13

标签: vba excel-vba excel-2010 excel

需要帮助。我是VBA的新手,并尝试从简单的宏开始。 但即便失败也是如此。 我想要做的就是使用输入框从一个工作表复制并粘贴到另一个工作表,以指定要复制的范围。 现在我知道输入框是:

Application.InputBox("Enter the range from which you want to copy : ", Type:=8)

但是,为了复制到另一个工作表中的单元格,我需要什么行?

我提前为自己是个白痴道歉。

3 个答案:

答案 0 :(得分:2)

这是你在尝试的吗?

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

答案 1 :(得分:1)

一种方法是这样的:

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

答案 2 :(得分:0)

以下是一些示例代码:

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