将数据从一个工作表复制到另一个工作表时遇到问题
Having trouble copying data from one Worksheet to another Worksheet
我有一大段代码用于处理和消除质谱数据(工作正常),但随后我有另一个命令从其工作中复制该数据sheet并将其粘贴到与宏 (Sheet1) 相同 sheet。每当我 运行 命令时,它都会将一行代码粘贴到工作 sheet 而不是 proteinGroups 中的信息,并且我从 google 或 Whosebug 尝试的命令的每次迭代都刚刚导致将数据粘贴到新的随机工作簿。
有问题的代码:
Set wb = Workbooks.Open("C:\Users\X241066\Downloads\PGroupTest.xlsm")
myFile = "C:\Users\X241066\Desktop\Pgroup\proteinGroups.xls"
Workbooks.Open myFile
Worksheets("proteinGroups").Copy
Workbooks("ProteinGroups.xls").Close SaveChanges:=True
wb.Activate
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("E1")
Application.CutCopyMode = False
将 UsedRange
复制到另一个工作簿
-
-
关于Worksheet.Copy method page
,声明如下:
如果您未指定之前或之后,Microsoft Excel 会创建一个包含复制的工作表对象的新工作簿。
Option Explicit
Sub CopyProteinGroups()
' Source (Copy FROM (Read))
Dim swb As Workbook
Set swb = Workbooks.Open("C:\Users\X241066\Desktop\Pgroup\proteinGroups.xls")
Dim sws As Worksheet: Set sws = swb.Worksheets("ProteinGroups")
Dim srg As Range: Set srg = sws.UsedRange
' Destination (Copy TO (Write))
Dim dwb As Workbook
Set dwb = Workbooks.Open("C:\Users\X241066\Downloads\PGroupTest.xlsm")
Dim dws As Worksheet: Set dws = dwb.Worksheets("Sheet1")
Dim dfCell As Range: Set dfCell = dws.Range("E1")
' Copy
srg.Copy dfCell
' Save and/or Close
swb.Close SaveChanges:=False ' no need to save; it was only read from
' dwb.Close SaveChanges:=True ' save when you're done; it was written to
End Sub
我有一大段代码用于处理和消除质谱数据(工作正常),但随后我有另一个命令从其工作中复制该数据sheet并将其粘贴到与宏 (Sheet1) 相同 sheet。每当我 运行 命令时,它都会将一行代码粘贴到工作 sheet 而不是 proteinGroups 中的信息,并且我从 google 或 Whosebug 尝试的命令的每次迭代都刚刚导致将数据粘贴到新的随机工作簿。
有问题的代码:
Set wb = Workbooks.Open("C:\Users\X241066\Downloads\PGroupTest.xlsm")
myFile = "C:\Users\X241066\Desktop\Pgroup\proteinGroups.xls"
Workbooks.Open myFile
Worksheets("proteinGroups").Copy
Workbooks("ProteinGroups.xls").Close SaveChanges:=True
wb.Activate
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("E1")
Application.CutCopyMode = False
将 UsedRange
复制到另一个工作簿
关于
Worksheet.Copy method page
,声明如下:
如果您未指定之前或之后,Microsoft Excel 会创建一个包含复制的工作表对象的新工作簿。
Option Explicit
Sub CopyProteinGroups()
' Source (Copy FROM (Read))
Dim swb As Workbook
Set swb = Workbooks.Open("C:\Users\X241066\Desktop\Pgroup\proteinGroups.xls")
Dim sws As Worksheet: Set sws = swb.Worksheets("ProteinGroups")
Dim srg As Range: Set srg = sws.UsedRange
' Destination (Copy TO (Write))
Dim dwb As Workbook
Set dwb = Workbooks.Open("C:\Users\X241066\Downloads\PGroupTest.xlsm")
Dim dws As Worksheet: Set dws = dwb.Worksheets("Sheet1")
Dim dfCell As Range: Set dfCell = dws.Range("E1")
' Copy
srg.Copy dfCell
' Save and/or Close
swb.Close SaveChanges:=False ' no need to save; it was only read from
' dwb.Close SaveChanges:=True ' save when you're done; it was written to
End Sub