Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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/5/excel/23.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
循环浏览文件夹中存储的.xml文件,并使用VBA对其进行格式化_Vba_Excel - Fatal编程技术网

循环浏览文件夹中存储的.xml文件,并使用VBA对其进行格式化

循环浏览文件夹中存储的.xml文件,并使用VBA对其进行格式化,vba,excel,Vba,Excel,因此,我的文件夹中有大约100个excel文件以.xml格式保存,我还编写了一个VBA代码,用于格式化文件夹中所有工作簿(文件)的每个工作表的布局设置。但问题是,代码在所有工作簿的最后一个工作表上都不起作用,它在其余工作表上工作得很好,即直到每个工作簿的最后一个工作表为止。 代码如下: Sub LoopAllExcelFilesInFolder() Dim wb As Workbook Dim sht As Worksheet Dim myPath As String Dim myFile A

因此,我的文件夹中有大约100个excel文件以.xml格式保存,我还编写了一个VBA代码,用于格式化文件夹中所有工作簿(文件)的每个工作表的布局设置。但问题是,代码在所有工作簿的最后一个工作表上都不起作用,它在其余工作表上工作得很好,即直到每个工作簿的最后一个工作表为止。 代码如下:

Sub LoopAllExcelFilesInFolder()

Dim wb As Workbook
Dim sht As Worksheet
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xml"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)

    ' added this line, loop through all worksheets in current wb
    For Each sht In wb.Worksheets

        'Change the layout
        Application.PrintCommunication = False
        With sht.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        Application.PrintCommunication = True
        ActiveSheet.PageSetup.PrintArea = ""
        Application.PrintCommunication = False
        With sht.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = Application.InchesToPoints(0.7)
            .RightMargin = Application.InchesToPoints(0.7)
            .TopMargin = Application.InchesToPoints(0.75)
            .BottomMargin = Application.InchesToPoints(0.75)
            .HeaderMargin = Application.InchesToPoints(0.3)
            .FooterMargin = Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintComments = xlPrintNoComments
            .PrintQuality = 600
            .CenterHorizontally = False
            .CenterVertically = False
            .Orientation = xlLandscape
            .Draft = False
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
            .PrintErrors = xlPrintErrorsDisplayed
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
    Next sht

    'Save and Close Workbook
    wb.Close SaveChanges:=True

    'Get next file name
    myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
Sub-LoopAllExcelFilesInFolder()
将wb设置为工作簿
将sht变暗为工作表
将myPath设置为字符串
将myFile设置为字符串
Dim myExtension作为字符串
Dim FldrPicker As FILE对话框
'优化宏速度
Application.ScreenUpdating=False
Application.EnableEvents=False
Application.Calculation=xlCalculationManual
'从用户检索目标文件夹路径
Set FldrPicker=Application.FileDialog(msoFileDialogFolderPicker)
用FldrPicker
.Title=“选择目标文件夹”
.AllowMultiSelect=False
如果.Show-1,则转到下一个代码
myPath=.SelectedItems(1)和“\”
以
"如果取消,
下一个代码:
myPath=myPath
如果myPath=”“,则转到重置设置
'目标文件扩展名(必须包含通配符“*”)
myExtension=“*.xml”
'具有结束扩展名的目标路径
myFile=Dir(myPath&myExtension)
'循环浏览文件夹中的每个Excel文件
当我的文件“”时执行此操作
'将变量设置为等于打开的工作簿
设置wb=Workbooks.Open(文件名:=myPath&myFile)
'添加此行,循环浏览当前wb中的所有工作表
对于wb.工作表中的每个sht
'更改布局
Application.PrintCommunication=False
使用sht.PageSetup
.PrintTitleRows=“”
.PrintTitleColumns=“”
以
Application.PrintCommunication=True
ActiveSheet.PageSetup.PrintArea=“”
Application.PrintCommunication=False
使用sht.PageSetup
.LeftHeader=“”
.CenterHeader=“”
.RightHeader=“”
.LeftFooter=“”
.CenterFooter=“”
.RightFooter=“”
.LeftMargin=应用程序.InchesToPoints(0.7)
.RightMargin=应用程序.InchesToPoints(0.7)
.TopMargin=应用程序.InchesToPoints(0.75)
.BottomMargin=应用程序的.InchesToPoints(0.75)
.HeaderMargin=应用程序.InchesToPoints(0.3)
.FooterMargin=应用程序.InchesToPoints(0.3)
.printHeaders=False
.PrintGridlines=False
.PrintComments=xlPrintNoComments
.PrintQuality=600
.1=错误
.centervertical=False
.方向=xl
.Draft=False
.PaperSize=xlPaperLetter
.FirstPageNumber=xlAutomatic
.Order=xlDownThenOver
.黑白=假
.Zoom=False
.FitToPagesWide=1
.FitToPagesTall=False
.PrintErrors=xlPrintErrors显示
.OddandEventPageSheaderFooter=False
.DifferentitFirstPageHeaderFooter=False
.scaleWithDochReaderFooter=True
.AlignMarginsHeaderFooter=真
.EvenPage.LeftHeader.Text=“”
.EvenPage.CenterHeader.Text=“”
.EvenPage.rightsheader.Text=“”
.EvenPage.LeftFooter.Text=“”
.EvenPage.CenterFooter.Text=“”
.EvenPage.RightFooter.Text=“”
.FirstPage.LeftHeader.Text=“”
.FirstPage.CenterHeader.Text=“”
.FirstPage.RightHeader.Text=“”
.FirstPage.LeftFooter.Text=“”
.FirstPage.CenterFooter.Text=“”
.FirstPage.RightFooter.Text=“”
以
下一步
'保存并关闭工作簿
wb.Close SaveChanges:=真
'获取下一个文件名
myFile=Dir
环
'任务完成时的消息框
MsgBox“任务完成!”
重置设置:
'重置宏优化设置
Application.EnableEvents=True
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
端接头
请告诉我哪里出错了。
谢谢

删除所有表示

Application.PrintCommunication = False
Application.PrintCommunication = True

到底出了什么问题?是否有错误(如果有,在哪一行)?是不是表现得不像预期的那样?@Mikegrann不,没有错误。它不会格式化每个工作簿的最后一页。因此,没有完全按照我希望的方式添加行
sht。在wb中每个sht的
循环开始后立即激活
。工作表
@dbmitch你是指这种方式吗?工作表sht.activate将
ActiveSheet.PageSetup.PrintArea=“”
更改为
sht.PageSetup.PrintArea=“”
时会发生什么情况