vba 将值从一个工作表复制到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4597623/
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 12:28:19  来源:igfitidea点击:

Copy values from one worksheet to another

excelvba

提问by xbonez

How can I copy some data from one worksheet to another?

如何将一些数据从一个工作表复制到另一个?

I tried this code, but get an error:

我试过这段代码,但得到一个错误:

Private Sub CommandButton2_Click()
    Sheets("Gas Opt").Select
    Range("A1:A3").Select
    Selection.Copy

    Sheets("ExportToPPServer").Select
    Cells(3, AColumn).Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    LFound = True
    MsgBox "Data coped."


End Sub

Error:

错误:

Select method of Range class failed.

回答by PowerUser

How does this look?

这看起来如何?

Sub x()
    Sheets("Gas Opt").Select
    Range("A1:A3").Select
    Selection.Copy
    ActiveWorkbook.Sheets("ExportToPPServer").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub

Edit

编辑

Is your Control button on a different sheet than "Gas Opt"? That would explain it. Try this:

您的控制按钮与“Gas Opt”在不同的工作表上吗?这样就可以解释了。尝试这个:

Sub x()
    Sheets("Sheet2").Range("A1:A3").Copy
    ActiveWorkbook.Sheets("Sheet3").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub

回答by Alex P

Something like this should work:

这样的事情应该工作:

Private Sub CommandButton2_Click()
    Dim copyRng As Range, targetRng As Range

    Set copyRng = Worksheets("Gas Opt").Range("A1:A3")
    Set targetRng = Worksheets("ExportToPPServer").Cells(3, AColumn)

    copyRng.Copy 
    targetRng.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    LFound = True
    MsgBox "Data coped."
End Sub

回答by GolezTrol

You need to activate the sheet, else you cannot select cells in it.

您需要激活工作表,否则您无法选择其中的单元格。

Sheets("ExportToPPServer").Activate ' Instead of select