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.
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
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:
Sub x()
Sheets("Sheet2").Range("A1:A3").Copy
ActiveWorkbook.Sheets("Sheet3").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub
You need to activate the sheet, else you cannot select cells in it.
Sheets("ExportToPPServer").Activate ' Instead of select
精彩评论