Vba 将粘贴数据从一张工作表复制到任何其他工作表

Vba 将粘贴数据从一张工作表复制到任何其他工作表,vba,excel,Vba,Excel,您好,我有这个代码,它只将粘贴数据从第1页复制到第2页 Sub CopyPasteCumUpdate() Dim rng As Range, inp As Range 'to remove 0 values that may be a result of a formula or direct entry. Set rng = Nothing Set inp = Selection inp.Interior.ColorIndex = 37 On

您好,我有这个代码,它只将粘贴数据从第1页复制到第2页

Sub CopyPasteCumUpdate()
    Dim rng As Range, inp As Range
    'to remove 0 values that may be a result of a formula or direct entry.
    Set rng = Nothing
    Set inp = Selection
    inp.Interior.ColorIndex = 37

    On Error Resume Next
    Set rng = Application.InputBox("Copy to", Type:=8)
    On Error GoTo 0

    If TypeName(rng) <> "Range" Then
        MsgBox "Cancelled", vbInformation
        Exit Sub
    Else
        rng.Parent.Activate
        rng.Select
        inp.Copy
        Worksheets("Sheet2").Paste Link:=True
        Application.CutCopyMode = 0
    End If
End Sub
Sub-copypestecumupdate()
变暗rng As范围,inp As范围
'删除可能是公式或直接输入结果的0个值。
设置rng=无
设置inp=选择
inp.Interior.ColorIndex=37
出错时继续下一步
设置rng=Application.InputBox(“复制到”,类型:=8)
错误转到0
如果TypeName(rng)“范围”,则
MsgBox“已取消”,VBS信息
出口接头
其他的
rng.Parent.Activate
rng.选择
复印件
工作表(“Sheet2”)。粘贴链接:=真
Application.CutCopyMode=0
如果结束
端接头

是否可以输入数据并将其粘贴到任何其他工作表,而无需硬编码?

从您的评论中可以看出,这很容易。只需更改此部分:

rng.Parent.Activate
rng.Select
inp.Copy
Worksheets("Sheet2").Paste Link:=True
Application.CutCopyMode = 0
有了这个:
编辑:这是在用户选择的范围内粘贴链接

Application.Goto rng
inp.Copy
rng.Parent.Paste Link:=True
Application.CutCopyMode = 0

是否可以在无需硬编码的情况下输入数据并粘贴到任何其他工作表
您想用什么方法?因为这要看情况了。你将如何告诉例行程序复制到哪张纸上?@L42我不太确定。例程将复制到用户单击的工作表中!如果可能的话,你能帮我回答上一个问题吗?谢谢@L42@Niva我试试:)。顺便说一句,我对我的答案添加了一个编辑。谢谢!但到底有什么区别呢@L42@Niva啊,编辑将链接粘贴到用户选择的范围上。顺便说一句,我已经评论了你的另一个问题的可能解决方案。但这不是VBA。