Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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
Excel 复制工作簿';将列复制到另一工作簿_Excel_Vba - Fatal编程技术网

Excel 复制工作簿';将列复制到另一工作簿

Excel 复制工作簿';将列复制到另一工作簿,excel,vba,Excel,Vba,我正在尝试使用VBA根据列标题将列从一个工作簿复制到另一个工作簿。但是,我得到一个运行时错误“9”:每次下标都超出范围 下面我将回答SO提出的另外两个问题。然而,它失败的代码行似乎适用于另一个有问题的操作[2] Sub Autofill() Dim sourceColumn As Range, target_column As Range 'read csv and convert to excel tx_design_file_name = ThisWorkboo

我正在尝试使用VBA根据列标题将列从一个工作簿复制到另一个工作簿。但是,我得到一个
运行时错误“9”:每次下标都超出范围

下面我将回答SO提出的另外两个问题。然而,它失败的代码行似乎适用于另一个有问题的操作[2]

Sub Autofill()

    Dim sourceColumn As Range, target_column As Range

    'read csv and convert to excel

    tx_design_file_name = ThisWorkbook.Sheets("Cover").Range("E10").Value

    Set tx_design_file_workbook = Workbooks.Open(tx_design_file_name)

    tx_design_file_workbook.SaveAs FileName:=ThisWorkbook.Path & "\Input\data.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

    tx_design_file_workbook.Close savechanges:=False


    Set compt_template = Workbooks.Open(ThisWorkbook.Path & "\Template\CoMPT_Convert_Template.xlsx")


    Set target_column = compt_template.Worksheets(1).Columns("A")


    Set data_excel_workbook = Workbooks.Open(ThisWorkbook.Path & "\Input\data.xlsx")

    Dim aCell As Range


    With data_excel_workbook.Worksheets(1)
        Set aCell = .Rows(1).Find(What:="SiteName", LookIn:=xlValues, LookAt:=xlWhole, _
                                          MatchCase:=False, SearchFormat:=False)

        If Not aCell Is Nothing Then
            aCell.EntireColumn.Cut Destination:=target_column
        Else
            MsgBox "SiteName not found"
        End If
    End With

    output_path_name = ThisWorkbook.Path & "\Output\CoMPT_Convert.xlsx"
    compt_template.SaveAs (output_path_name)
    'target_column.SaveAs (output_path_name)

End Sub


读了你的评论后,我认为你打错了:

Sub Autofill()

    Dim sourceColumn As Range, target_column As Range

    'read csv and convert to excel

    tx_design_file_name = ThisWorkbook.Sheets("Feuil1").Range("E10").Value

    Set tx_design_file_workbook = Workbooks.Open(tx_design_file_name)

    tx_design_file_workbook.SaveAs Filename:=ThisWorkbook.Path & "\Input\data.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

    tx_design_file_workbook.Close savechanges:=False


    Set compt_template = Workbooks.Open(ThisWorkbook.Path & "\Template\CoMPT_Convert_Template.xlsx")


    Set target_column = Workbooks("CoMPT_Convert_Template").Worksheets(1).Range("A" & ActiveSheet.Cells(1048576, 1).End(xlUp).row)


    Set data_excel_workbook = Workbooks.Open(ThisWorkbook.Path & "\Input\data.xlsx")

    Dim aCell As Range


    With data_excel_workbook.Worksheets(1)
        Set aCell = .Rows(1).Find(What:="SiteName", LookIn:=xlValues, LookAt:=xlWhole, _
                                          MatchCase:=False, SearchFormat:=False)

        If Not aCell Is Nothing Then
            aCell.EntireColumn.Cut Destination:=target_column
        Else
            MsgBox "SiteName not found"
        End If
    End With

    output_path_name = ThisWorkbook.Path & "\Output\CoMPT_Convert.xlsx"
    compt_template.SaveAs (output_path_name)
    'target_column.SaveAs (output_path_name)

End Sub

将行更改为此,然后重试:
Set target\u column=compt\u template.工作表(1).列(“A4”)
另外:
范围(“E”和10)
范围(“E10”)
相同,使其更易于阅读。:)另外,您正在将
targetColumn标注为Range
但稍后将其称为
Set target\u column
@M.Schalk已经修改,但当我保存它时,没有任何更改,我正在保存
compt\u模板
是否应该保存
target\u column
?@jamheadart已经修改了code是的,这很有效,但我需要通过A4单元格的值,但这行代码不允许我
Set target\u column=compt\u template.Worksheets(1).Columns(“A4”)
@mp252您是否尝试过
Set target\u column=workbook(“compt\u Convert\u template”).Worksheets(1).Columns(“A”)
是的,但我需要将其放在A4单元格中从A4开始cell@mp252然后执行
Set target\u column=工作簿(“CoMPT\u Convert\u Template”)。工作表(1).Range(“A4”)
请告诉我它是否可以正常工作。不幸的是,我遇到了运行时错误1004,“复制和粘贴区域不能重叠,除非它们的大小和形状相同。”