Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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/amazon-s3/2.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工作表复制数据,并使用VBScript将其附加到单个excel工作表_Vba_Excel_Vbscript - Fatal编程技术网

Vba 从多个excel工作表复制数据,并使用VBScript将其附加到单个excel工作表

Vba 从多个excel工作表复制数据,并使用VBScript将其附加到单个excel工作表,vba,excel,vbscript,Vba,Excel,Vbscript,情况如下: 我有一个包含数据的excel(.xls)文件。(例如A.xls) 此excel文件中的数据位于单个工作表(表1)中 此文件中的列数是固定的,即8 但是,包含数据的行数可能会不时变化。(此文件由另一个程序不时更新) 现在,我有另一个excel文件(例如B.xls),具有类似类型的数据,但与A.xls的内容不同 B.xls中的列数也是8。但是,包含数据的行数未知 我想复制A.xls,第二行之后的内容(不包括包含列标题的第一行),并将其附加/粘贴到B.xls文件中,而不会在B.xls上过度

情况如下:

  • 我有一个包含数据的excel(.xls)文件。(例如A.xls
  • 此excel文件中的数据位于单个工作表(表1)中
  • 此文件中的列数是固定的,即8
  • 但是,包含数据的行数可能会不时变化。(此文件由另一个程序不时更新)
  • 现在,我有另一个excel文件(例如B.xls),具有类似类型的数据,但与A.xls的内容不同
  • B.xls中的列数也是8。但是,包含数据的行数未知
  • 我想复制A.xls第二行之后的内容(不包括包含列标题的第一行),并将其附加/粘贴到B.xls文件中,而不会在B.xls上过度写入现有数据

    考虑到所有这些细节,我想编写一个vbscript来自动化此任务

    请帮忙


    事先非常感谢。

    它需要大量清理,但类似的东西应该可以用。我会把它整理一下,然后进行编辑

    Sub CopyRows()
      ' Choose the name of the Second Workbook and last column.
      ' It must be in the same directory as your First Workbook.
      secondWorkbook = "B.xls"
      lastColumn = "H"
    
      ' A couple more variables
      currentWorkbook = ThisWorkbook.Name
      Workbooks.Open ThisWorkbook.Path & "\" & secondWorkbook
    
      ' In the First Workbook, find and select the first empty
      ' cell in column A on the first Worksheet.
      Windows(currentWorkbook).Activate
      With Worksheets(1).Columns("A:A")
        Set c = .Find("", LookIn:=xlValues)
        If Not c Is Nothing Then
          ' Select and copy from A2 to the end.
          secondAddress = Replace(c.Address, "$A$", "")
          Range("A2:" & lastColumn & CStr(CInt(secondAddress) - 1)).Select
          Selection.Copy
        End If
      End With
    
      ' Activate the Second Workbook
      Windows(secondWorkbook).Activate
      With Worksheets(1).Columns("A:A")
        Set c = .Find("", LookIn:=xlValues)
        If Not c Is Nothing Then
          ' Select and paste the data from First Workbook
          Range(c.Address).Select
          ActiveSheet.Paste
        End If
      End With
    End Sub
    

    更新:这应该可以解决问题。我第一次也是从错误的工作簿上抄来的。如果您有任何问题,请告诉我。

    它需要大量清理,但类似的东西应该可以工作。我会把它整理一下,然后进行编辑

    Sub CopyRows()
      ' Choose the name of the Second Workbook and last column.
      ' It must be in the same directory as your First Workbook.
      secondWorkbook = "B.xls"
      lastColumn = "H"
    
      ' A couple more variables
      currentWorkbook = ThisWorkbook.Name
      Workbooks.Open ThisWorkbook.Path & "\" & secondWorkbook
    
      ' In the First Workbook, find and select the first empty
      ' cell in column A on the first Worksheet.
      Windows(currentWorkbook).Activate
      With Worksheets(1).Columns("A:A")
        Set c = .Find("", LookIn:=xlValues)
        If Not c Is Nothing Then
          ' Select and copy from A2 to the end.
          secondAddress = Replace(c.Address, "$A$", "")
          Range("A2:" & lastColumn & CStr(CInt(secondAddress) - 1)).Select
          Selection.Copy
        End If
      End With
    
      ' Activate the Second Workbook
      Windows(secondWorkbook).Activate
      With Worksheets(1).Columns("A:A")
        Set c = .Find("", LookIn:=xlValues)
        If Not c Is Nothing Then
          ' Select and paste the data from First Workbook
          Range(c.Address).Select
          ActiveSheet.Paste
        End If
      End With
    End Sub
    

    更新:这应该可以解决问题。我第一次也是从错误的工作簿上抄来的。如果您有问题,请告诉我。

    这是宏录制器可以为您编写的内容。你会拿出不同的方法

    打开录音。打开A.xls和B.xls。在a上向下移动一行。然后按Shift+End→, 然后Shift+End+↓. 然后按Ctrl+C组合键复制数据。切换回B。结束+↓, ↓. 按Ctrl+V组合键进行粘贴。关掉录音

    您可以在Excel中记录

    Alt+T,M,R

    然后是家庭钥匙↑. 停止录音

    看看Excel写了什么

    Selection.End(xlUp).Select
    
    或者,如果您已经录制了“转到”对话框

    Application.Goto Reference:="R1C1"
    
    或者如果您已经录制了Ctrl+Home

    转换为vbscript的步骤

    在excel宏记录器中记录这些步骤。您必须重写它一点,因为它使用了vbs不使用的语法类型

    这在vba中适用(我没有介质9)
    xlRangeAutoFormatAccounting4

    Selection.AutoFormat Format:=xlRangeAutoFormatAccounting4, Number:=True, _
        Font:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
    
    所以首先在vba的对象浏览器中查找常量<代码>xlRangeAutoFormatAccounting4=17

    然后在对象浏览器中向上查看函数,并在底部查看函数定义

    Function AutoFormat([Format As XlRangeAutoFormat = xlRangeAutoFormatClassic1], [Number], [Font], [Alignment], [Border], [Pattern], [Width])
    
    因此vba变成了vbs(vbs在vba中工作)(正如您所看到的,您可以找到正确的方法,而无需通常查找函数)

    所以你的代码变成了

    objXLWs.Range("A3").CurrentRegion.Select.AutoFormat 17, True, True, True,True, True, True
    

    这是宏记录器可以为您编写的内容。你会拿出不同的方法

    打开录音。打开A.xls和B.xls。在a上向下移动一行。然后按Shift+End→, 然后Shift+End+↓. 然后按Ctrl+C组合键复制数据。切换回B。结束+↓, ↓. 按Ctrl+V组合键进行粘贴。关掉录音

    您可以在Excel中记录

    Alt+T,M,R

    然后是家庭钥匙↑. 停止录音

    看看Excel写了什么

    Selection.End(xlUp).Select
    
    或者,如果您已经录制了“转到”对话框

    Application.Goto Reference:="R1C1"
    
    或者如果您已经录制了Ctrl+Home

    转换为vbscript的步骤

    在excel宏记录器中记录这些步骤。您必须重写它一点,因为它使用了vbs不使用的语法类型

    这在vba中适用(我没有介质9)
    xlRangeAutoFormatAccounting4

    Selection.AutoFormat Format:=xlRangeAutoFormatAccounting4, Number:=True, _
        Font:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
    
    所以首先在vba的对象浏览器中查找常量<代码>xlRangeAutoFormatAccounting4=17

    然后在对象浏览器中向上查看函数,并在底部查看函数定义

    Function AutoFormat([Format As XlRangeAutoFormat = xlRangeAutoFormatClassic1], [Number], [Font], [Alignment], [Border], [Pattern], [Width])
    
    因此vba变成了vbs(vbs在vba中工作)(正如您所看到的,您可以找到正确的方法,而无需通常查找函数)

    所以你的代码变成了

    objXLWs.Range("A3").CurrentRegion.Select.AutoFormat 17, True, True, True,True, True, True