Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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电子表格_Excel_Vba - Fatal编程技术网

将单元格从excel表格复制到另一个excel电子表格

将单元格从excel表格复制到另一个excel电子表格,excel,vba,Excel,Vba,这听起来可能会让人困惑,所以我会尽力解释。我对VBA很陌生,我对excel也很在行。我试图制作一个电子表格,将某些值填入另一个设置为模板的电子表格中。但是,有多个不同的模板,但需要根据找到的值填写。模板是独立的文件。在无法泄露客户业务信息的情况下,我将使用一个示例。我有模板a.xls、b.xls和c.xls,其中有3个单元格需要来自设置电子表格的信息,即表格中的result1、result2、result3。因此,如果找到“a”,则取result1、result2和result3,复制它们并粘贴

这听起来可能会让人困惑,所以我会尽力解释。我对VBA很陌生,我对excel也很在行。我试图制作一个电子表格,将某些值填入另一个设置为模板的电子表格中。但是,有多个不同的模板,但需要根据找到的值填写。模板是独立的文件。在无法泄露客户业务信息的情况下,我将使用一个示例。我有模板a.xls、b.xls和c.xls,其中有3个单元格需要来自设置电子表格的信息,即表格中的result1、result2、result3。因此,如果找到“a”,则取result1、result2和result3,复制它们并粘贴到a.xls所需的单元格中

我现在使用的代码在数据列中查找值,并返回一个msgbox。我不知道这是否是解决问题的正确方法,我正在网上结结巴巴地浏览示例,所以任何帮助都会很棒

这是到目前为止我的代码

    Sub LookupTableValue()
Dim Table2 As ListObject
Dim FoundCell1, FoundCell2 As Range
Dim LookupValue1, LookupValue2 As String
  LookupValue1 = "a"
  LookupValue2 = "b"
  Set Table2 = ActiveSheet.ListObjects("Table2")
    Set FoundCell1 = Table2.DataBodyRange.Columns(1).Find(LookupValue1, LookAt:=xlWhole)
    Set FoundCell2 = Table2.DataBodyRange.Columns(1).Find(LookupValue2, LookAt:=xlWhole)
    If Not (FoundCell1 Is Nothing) Then
        If FoundCell1.Value = LookupValue1 Then
            MsgBox "Found a"
        End If
    End If
    If Not (FoundCell2 Is Nothing) Then
        If FoundCell2.Value = LookupValue2 Then
            MsgBox "Found b"
        End If
    End If
End Sub
因此,我想我想要的不是MsgBox行,而是打开作为模板的特定xls电子表格,然后将表格中的单元格复制到此模板中,然后另存为另一个文件夹中的new.xls

Pathname=File Path
If FoundCell1.Value = LookupValue1 Then
            MsgBox "Found a"
            Filename=File name depending on the value
            Set workbook = Workbooks.Open(Filename:=Pathname & Filename)
            Copy table content to above workbook. See this [https://stackoverflow.com/questions/31702885/copy-tables-to-another-worksheet][1]
            workbook.SaveAs Filename:=NewFileName, FileFormat:=xlWorkbookNormal
End If
希望这对你有帮助