Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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
VbScript-我有10个excel文件。要将所有10个文件合并到一个excel中吗_Vbscript - Fatal编程技术网

VbScript-我有10个excel文件。要将所有10个文件合并到一个excel中吗

VbScript-我有10个excel文件。要将所有10个文件合并到一个excel中吗,vbscript,Vbscript,VbScript-我有10个excel文件。希望将所有10个文件合并到一个excel中。下面的代码需要花费大量时间。是否有更快的方法来完成这些工作。我是VbScript的新手,因此无法优化代码 Sub proc_A Dim Totfiles(2) 'array of 3 files Dim FinalFile Totfiles(0)="C:\POC4.xls" Totfiles(1)="C:\POC5.xls" Totfiles(2)="C:\POC6.xls" 'Totfiles(3)="D

VbScript-我有10个excel文件。希望将所有10个文件合并到一个excel中。下面的代码需要花费大量时间。是否有更快的方法来完成这些工作。我是VbScript的新手,因此无法优化代码

Sub proc_A
Dim Totfiles(2) 'array of 3 files
Dim FinalFile

Totfiles(0)="C:\POC4.xls"
Totfiles(1)="C:\POC5.xls"
Totfiles(2)="C:\POC6.xls"
'Totfiles(3)="D:\FileD.xls"
FinalFile= "C:\Alerts1.xls"

Call MergeFile(Totfiles,FinalFile)

End Sub    

Function MergeFile(Totfiles,FinalFile)

Dim RowNumber,rowcount
Dim wbookA,wbookD
Dim wsheetA,wsheetD
Dim FinalFileCount  ' To store the row count of final file
'Dim FirstFlag 'To check if the Column name has been copied to final File
'FirstFlag=0  ' IF value is 1 then the Column names will not be copied

set xapp=createobject("excel.application")

For mainloop=0 to Ubound(Totfiles)


  If (mainloop=0) Then  ' This loop is for first file only as it copies Column name
    set wbookA= xapp.workbooks.open(Totfiles(mainloop))
    set wsheetA=wbookA.sheets(1)

    rowcnt=wsheetA.usedrange.rows.count
    colcnt=wsheetA.usedrange.columns.count
    set wbookD= xapp.workbooks.open(FinalFile)
    set wsheetD=wbookD.sheets(1)
    For i=1 to rowcnt
      For j=1 to colcnt

        wsheetD.cells(i,j).value=wsheetA.cells(i,j).value
        wbookD.save

      Next
    Next

  End if

  If (mainloop>0) Then ' This loop is for rest of files. it doesnt copies Column name

aaa= Totfiles(mainloop)
    set wbookB= xapp.workbooks.open(Totfiles(mainloop))
    set wsheetB=wbookB.sheets(1)
    'reading the value from cell

    rowcntB=wsheetB.usedrange.rows.count
    colcntB=wsheetB.usedrange.columns.count
    RowNumber=1
    rowcount=0
    set wbookD= xapp.workbooks.open(FinalFile)
    set wsheetD=wbookD.sheets(1)
    call wsheetD.cells.clear
      Do while(Trim(wsheetD.cells(RowNumber,3).value) <> "")
        log.Message(Trim(wsheetD.cells(RowNumber,3).value))
        RowNumber=RowNumber+1
        rowcount=rowcount+1
      Loop 

    FinalFileCount=rowcount

    for i=1 to rowcntB
      for j=1 to colcntB
        k=FinalFileCount+i
        wsheetD.cells(k,j).value=wsheetB.cells(i+1,j).value
        wbookD.save
      Next
    Next

  End IF

Next  ' End For for main Loop
wbookD.save
wbookD.Close
wbookB.close
set wsheetD =nothing
set wbookD =nothing
set wsheetB =nothing
set wbookB =nothing
set xapp =nothing
MergeFile=True
End Function
子过程A
Dim Totfiles(2)包含3个文件的数组
模糊最终文件
Totfiles(0)=“C:\POC4.xls”
Totfiles(1)=“C:\POC5.xls”
Totfiles(2)=“C:\POC6.xls”
'Totfiles(3)=“D:\field.xls”
FinalFile=“C:\Alerts1.xls”
调用合并文件(Totfiles,FinalFile)
端接头
函数合并文件(Totfiles,FinalFile)
昏暗的行数,行数
暗淡的wbookA,wbookD
昏暗的天气
Dim FinalFileCount'存储最终文件的行数
“Dim FirstFlag”检查列名是否已复制到最终文件
“FirstFlag=0”如果值为1,则不会复制列名
设置xapp=createobject(“excel.application”)
对于mainloop=0到Ubound(Totfiles)
如果(mainloop=0),则“此循环仅用于第一个文件,因为它复制列名
设置wbookA=xapp.workbooks.open(Totfiles(mainloop))
设置wsheetA=wbookA.sheets(1)
rowcnt=wsheetA.usedrange.rows.count
colcnt=wsheetA.usedrange.columns.count
设置wbookD=xapp.workbooks.open(最终文件)
设置wsheetD=wbookD.sheets(1)
对于i=1到rowcnt
对于j=1到colcnt
wsheetD.cells(i,j).value=wsheetA.cells(i,j).value
wbookD.save
下一个
下一个
如果结束
如果(mainloop>0),则“此循环用于其余文件。它不复制列名
aaa=Totfiles(主循环)
设置wbookB=xapp.workbooks.open(Totfiles(mainloop))
设置wsheetB=wbookB.sheets(1)
'从单元格中读取值
rowcntB=wsheetB.usedrange.rows.count
colcntB=wsheetB.usedrange.columns.count
行数=1
行计数=0
设置wbookD=xapp.workbooks.open(最终文件)
设置wsheetD=wbookD.sheets(1)
呼叫wsheetD.cells.clear
执行while(修剪(wsheetD.cells(RowNumber,3.value)”)
log.Message(Trim(wsheetD.cells(RowNumber,3.value))
RowNumber=RowNumber+1
rowcount=rowcount+1
环
FinalFileCount=行计数
对于i=1到rowcntB
对于j=1到colcntB
k=最终文件数+i
wsheetD.cells(k,j).value=wsheetB.cells(i+1,j).value
wbookD.save
下一个
下一个
如果结束
主回路的“下一个”结束
wbookD.save
wbookD,关闭
wbookB.close
设置wsheetD=nothing
设置wbookD=nothing
设置wsheetB=nothing
设置wbookB=nothing
设置xapp=nothing
MergeFile=True
端函数

将它们全部导出到CSV文件。连接CSV文件。有人能帮我优化代码吗?