由于某种原因,每当我运行这个程序时,它会产生一个不完整的输出,它似乎是从另一个excel文件的最后一个选定的工作表开始的,而这是它唯一显示的输出。
我试着打开宏代码,一切正常输出,但每当代码关闭输出不完整。所以这里是完整的代码工具。真的需要一些帮助在这里,我不知道这是一个错误造成的这么多数组或有一些不正确的代码。
Option Explicit
Public savepath As String
'This will select the file/folder
Function select_folder2()
Dim Filepicker As FileDialog
Dim mypath As String
Set Filepicker = Application.FileDialog(msoFileDialogFolderPicker)
With Filepicker
.Title = "Select folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
.ButtonName = "Select(&S)"
If .Show = -1 Then
mypath = .SelectedItems(1) & "\"
Else
End
End If
End With
NextCode:
Set Filepicker = Nothing
savepath = mypath
End Function
Sub PrintArray(data As Variant, Cl As Range)
Cl.Resize(UBound(data, 1), UBound(data, 2)) = data
End Sub
Sub excel_report()
Dim strFile As String
Dim strInFold As String
Dim extension As String
Dim ExlApp As Excel.Application
Dim wbk As Workbook
Dim ws As Worksheet, sheetpage As Page
Dim counter As Long
Dim index As Long
Dim sets() As String
Dim pgs As Integer
Dim wspgs As Integer
'count the files in the folder
strInFold = savepath
extension = "*.xls*"
strFile = Dir(strInFold & extension)
Do While strFile <> ""
counter = counter + 1
strFile = Dir
Loop
ReDim sets(counter + 25, 25)
'save values of files into an array
strInFold = savepath
extension = "*.xls*"
strFile = Dir(strInFold & extension)
'show the file location in the array
sets(0, 0) = "File Location"
sets(0, 1) = strInFold
're name the first rows as the title of the values below
sets(1, 0) = "File Name"
sets(1, 1) = "Header Left"
sets(1, 2) = "Header Center"
sets(1, 3) = "Header Right"
sets(1, 4) = "Footer Left"
sets(1, 5) = "Footer Center"
sets(1, 6) = "Footer Right"
sets(1, 7) = "Different First Page Header Left"
sets(1, 8) = "Different First Page Header Center"
sets(1, 9) = "Different First Page Header Right"
sets(1, 10) = "Different First Page Footer Left"
sets(1, 11) = "Different First Page Footer Center"
sets(1, 12) = "Different First Page Footer Right"
sets(1, 13) = "Even page Header Left"
sets(1, 14) = "Even page Header Center"
sets(1, 15) = "Even page Header Right"
sets(1, 16) = "Even page Footer Left"
sets(1, 17) = "Even page Footer Center"
sets(1, 18) = "Even page Footer Right"
sets(1, 19) = "Odd Header Left"
sets(1, 20) = "Odd Header Center"
sets(1, 21) = "Odd Header Right"
sets(1, 22) = "Odd Footer Left"
sets(1, 23) = "Odd Footer Center"
sets(1, 24) = "Odd Footer Right"
're use counter 1 as a row positioniong variable
counter = 2
Do While strFile <> ""
'open excel application
On Error Resume Next
' Check whether excel is running
Set ExlApp = GetObject(, "Excel.Application")
If ExlApp Is Nothing Then
' Word is not running, create new instance
Set ExlApp = CreateObject("Excel.Application")
' For automation to work, excel must be visible
ExlApp.Visible = True
End If
On Error GoTo 0
DoEvents
' open file
Set wbk = ExlApp.Workbooks.Open(strInFold & strFile)
index = 1
pgs = 1
For Each ws In wbk.Worksheets
If ws.Visible = xlSheetHidden Then
ws.Visible = xlSheetVisible
wbk.Save
End If
sets(counter, 0) = strFile & " Sheet " & index
wspgs = ws.PageSetup.Pages.Count
'assign the filename, headers, and footers value from the currently opened file into the array
For Each sheetpage In ws.PageSetup.Pages
' Sheet1.Select
' For pgs = 1 To wspgs
If ws.PageSetup.DifferentFirstPageHeaderFooter = True Then
sets(counter, 7) = ws.PageSetup.FirstPage.LeftHeader.Text
sets(counter, 8) = ws.PageSetup.FirstPage.CenterHeader.Text
sets(counter, 9) = ws.PageSetup.FirstPage.RightHeader.Text
sets(counter, 10) = ws.PageSetup.FirstPage.LeftFooter.Text
sets(counter, 11) = ws.PageSetup.FirstPage.CenterFooter.Text
sets(counter, 12) = ws.PageSetup.FirstPage.RightFooter.Text
' Else
' sets(counter, 1) = wbk.Worksheets(index).PageSetup.LeftHeader
' sets(counter, 2) = wbk.Worksheets(index).PageSetup.CenterHeader
' sets(counter, 3) = wbk.Worksheets(index).PageSetup.RightHeader
' sets(counter, 4) = wbk.Worksheets(index).PageSetup.LeftFooter
' sets(counter, 5) = wbk.Worksheets(index).PageSetup.CenterFooter
' sets(counter, 6) = wbk.Worksheets(index).PageSetup.RightFooter
End If
If ws.PageSetup.OddAndEvenPagesHeaderFooter = True Then
If pgs Mod 2 = 0 Then
sets(counter, 13) = ws.PageSetup.EvenPage.LeftHeader.Text
sets(counter, 14) = ws.PageSetup.EvenPage.CenterHeader.Text
sets(counter, 15) = ws.PageSetup.EvenPage.RightHeader.Text
sets(counter, 16) = ws.PageSetup.EvenPage.LeftFooter.Text
sets(counter, 17) = ws.PageSetup.EvenPage.CenterFooter.Text
sets(counter, 18) = ws.PageSetup.EvenPage.RightFooter.Text
ElseIf pgs Mod 2 = 1 Then
sets(counter, 19) = ws.PageSetup.LeftHeader
sets(counter, 20) = ws.PageSetup.CenterHeader
sets(counter, 21) = ws.PageSetup.RightHeader
sets(counter, 22) = ws.PageSetup.LeftFooter
sets(counter, 23) = ws.PageSetup.CenterFooter
sets(counter, 24) = ws.PageSetup.RightFooter
End If
Else
sets(counter, 1) = wbk.Worksheets(index).PageSetup.LeftHeader
sets(counter, 2) = wbk.Worksheets(index).PageSetup.CenterHeader
sets(counter, 3) = wbk.Worksheets(index).PageSetup.RightHeader
sets(counter, 4) = wbk.Worksheets(index).PageSetup.LeftFooter
sets(counter, 5) = wbk.Worksheets(index).PageSetup.CenterFooter
sets(counter, 6) = wbk.Worksheets(index).PageSetup.RightFooter
End If
pgs = pgs + 1
' Next pgs
Next sheetpage
'move to next row
index = index + 1
counter = counter + 1
Next ws
wbk.Close
strFile = Dir
Loop
'use the printarray sub to print the array (arry) into the "Word" sheet, start with column and row A1
PrintArray sets, ActiveWorkbook.Worksheets("Excel").[A1]
End Sub
1条答案
按热度按时间zc0qhyus1#
将PrintArray插入到sub中的问题是由于您的Array被填满的速度比您想象的要快得多。
只会计算文件的数量,但我想象工作表的数量超过工作簿的数量+25
无论如何,如果你只想打印一次,我把PrintArray放在循环中的假设是错误的,除非在循环中打印是好的,但是你必须跟踪你最后结束的地方,清空/重新填充你的数组。
我无法测试它,所以请让我知道这是否适合你,或者如果你仍然遇到问题。