VBA将Excel范围复制到不同的工作簿

时间:2017-03-30 16:31:19

标签: excel excel-vba copy range paste vba

我试图找到一种方法将一个工作簿中的范围复制到另一个工作簿,在本例中为A6:J21。我认为它会像以下那样......

currentWorksheet = xlWorkBook.Sheets.Item("Command Group")
excelRange = currentWorksheet.Range("A6:J21")
excelDestination = newXlSheet.Range("A6:J21")
excelRange.Copy(excelDestination)

但它在excelRange.Copy(excelDestination)上给我一个错误。

以下代码按预期运行,因此我不确定我在哪里出错..

Dim xRng As Excel.Range = CType(currentWorksheet.Cells(7, 7), Excel.Range)
Console.WriteLine(xRng.ToString)
Dim val As Object = xRng.Value()
testString = val.ToString
Console.WriteLine(testString)
newXlSheet.Cells(1, 1) = testString

3 个答案:

答案 0 :(得分:1)

回答你的问题“为什么B在运行,而不是A”..

在A: currentWorksheet = xlWorkBook.Sheets.Item("Command Group")

首先,您的对象分配缺少SET。其次,您使用Workbook.Sheets.Item()返回Sheets objectSheets object可以表示图表工作表或工作表,因此没有.Range()方法。

您可以通过单步执行此代码来验证这一点:

Dim currentWorksheet As Sheets
Set currentWorksheet = ThisWorkbook.Sheets.Item("Command Group")
excelRange = currentWorksheet.Range("A1:A21")

它会出错,并告诉您找不到该方法。

修复A:确保使用强类型返回工作表对象。

Dim currentWorksheet as Worksheet
Set currentWorksheet = ThisWorkbook.Sheets.Item("Command Group")

要修复未来的代码并简化调试过程,我强烈建议您始终在所有模块的顶部声明Option Explicit

为简洁起见,您可以将代码缩短为:

Dim currentWorksheet as Worksheet
Set currentWorksheet = ThisWorkbook.Sheets("Command Group")

答案 1 :(得分:0)

这应该这样做,如果你遇到问题,请告诉我:

Sub foo()
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")

'Now, copy what you want from x:
x.Sheets("name of copying sheet").Range("A1").Copy

'Now, paste to y worksheet:
y.Sheets("sheetname").Range("A1").PasteSpecial

'Close x:
x.Close

End Sub

或者,您可以:

Sub foo2()
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")

'Now, transfer values from x to y:
y.Sheets("sheetname").Range("A1").Value = x.Sheets("name of copying sheet").Range("A1") 

'Close x:
x.Close

End Sub

Copy from one workbook and paste into another

答案 2 :(得分:0)

参考下面的代码将数据从一个工作表(比如 file1)复制到另一个(比如 file2)。我创建这个文件是为了避免从另一个工作簿复制格式,因为它会导致文件(比如 file1)崩溃。目的是仅将值逐个单元格地从一张工作表复制到另一张工作表。

Sub Copydatafrom_sheets()
    'This will copy sheet cell by cell without selecting the cells.
    'commented items are not used in the code execution
    Dim i As Long
    Dim j As Long
        i = 1
        j = 1
    Application.ScreenUpdating = False
    Dim file1 As Workbook ' defined as workbook
    Dim file2 As Workbook ' defined as workbook
    Dim range1 As Range
    Dim range2 As Range ' not used
    Dim Copied_data As String ' to store data in this string while iterating
    Set file1 = Workbooks.Open("G:\MyProject - Backup\QAQC\Data Combined - 2.xlsx") ' file where orinal data is stored, use your own file names
    Set file2 = Workbooks.Open("G:\MyProject - Backup\QAQC\Test3.xlsm") ' File where it shall be copied
    Set range1 = file1.Sheets("ASC_Table_1").Range("A1:V25944") 'set the range to be copied
    For Each cell In range1
    
        Copied_data = file1.Sheets("ASC_Table_1").Cells(i, j).Value
        'MsgBox (Copied_data)
        file2.Sheets("Sheet2").Cells(i, j) = Copied_data
          If j <= 22 Then j = j + 1
            If j > 22 Then
                 i = i + 1
                 j = 1
            End If
    Application.StatusBar = Format((i / 25994), "Percent")
    Next
    Application.ScreenUpdating = True
    file2.Save 'Optional
    End Sub