Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA将Excel范围复制到其他工作簿_Excel_Copy_Range_Paste_Vba - Fatal编程技术网

VBA将Excel范围复制到其他工作簿

VBA将Excel范围复制到其他工作簿,excel,copy,range,paste,vba,Excel,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) 但是它在excelR

我试图找到一种方法,将一个工作簿中的一个范围(在本例中为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
回答你的问题“为什么B在运行,而不是A”

以一种方式:
currentsheet=xlWorkBook.Sheets.Item(“命令组”)

首先,您缺少对象指定的
SET
。其次,您使用的是
Workbook.Sheets.Item()
,它返回一个
Sheets对象
Sheets对象可以表示图表工作表或工作表,因此没有
.Range()
方法

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

Dim currentWorksheet As Sheets
Set currentWorksheet = ThisWorkbook.Sheets.Item("Command Group")
excelRange = currentWorksheet.Range("A1:A21")
它将出错,并告诉您找不到该方法

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

Dim currentWorksheet as Worksheet
Set currentWorksheet = ThisWorkbook.Sheets.Item("Command Group")
为了修复将来的代码并简化调试过程,我强烈建议始终在所有模块的顶部声明
Option Explicit

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

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

这应该可以,如果您有问题,请告诉我:

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

参考以下代码,将数据从一个工作表(如文件1)复制到另一个工作表(如文件2)。我创建这个文件是为了避免从其他工作簿复制格式,因为它会导致文件(比如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
来自工作表()的子副本数据
'这将在不选择单元格的情况下逐单元格复制工作表单元格。
'注释项不用于代码执行
我想我会坚持多久
Dim j尽可能长
i=1
j=1
Application.ScreenUpdating=False
将文件1作为工作簿“定义为工作簿”
将文件2作为工作簿“定义为工作簿”
变暗范围1作为范围
变光范围2作为“未使用”范围
Dim将_数据复制为字符串,以在迭代时将数据存储在此字符串中
设置file1=Workbooks.Open(“G:\MyProject-Backup\QAQC\Data Combined-2.xlsx”)存储原始数据的文件,使用您自己的文件名
设置file2=Workbooks.Open(“G:\MyProject-Backup\QAQC\Test3.xlsm”)文件,将其复制到该文件中
Set range1=file1.Sheets(“ASC_Table_1”).Range(“A1:V25944”)设置要复制的范围
对于范围1中的每个单元格
复制的单元格(i,j).值
'MsgBox(复制的_数据)
文件2.表(“表2”).单元格(i,j)=复制的数据
如果j 22那么
i=i+1
j=1
如果结束
Application.StatusBar=格式((i/25994),“百分比”)
下一个
Application.ScreenUpdating=True
file2.Save“可选
端接头

只要尝试
excelRange.Copy excelDestination
,假设您将
newXlSheet
设置在某个地方。我对您的意思感到困惑?你不需要说一下要把它放在
newXlSheet
中的哪个范围吗?你可以说
excelDestination=newXlSheet.range(…)
,但我不知道你在哪里声明
newXlSheet
。。。所以,如果您从未声明/设置它,它将在您身上出错,因为VBA不知道这是什么表。将
Option Explicit
添加到代码的最顶端(甚至在
Sub mySub()
上方),这将强制您声明宏中使用的所有变量。
newXlSheet
被声明为其他值。
行出错时,您会遇到什么错误?复制
行?第二个代码块与出现错误的第一个代码块有什么关系?或者你只是用第二个来说明你可以使用
newXlSheet
而不会出现任何问题?