Excel VBscript代码中的自动调整行

Excel VBscript代码中的自动调整行,excel,row,vba,Excel,Row,Vba,我正在使用命令 .Columns("A:I").EntireColumn.AutoFit 要在运行循环后自动调整excel vbscript中的列,但在放置命令时出错: Rows(xCount).EntireRow.AutoFit 自动调整图纸中行的高度 您能告诉我如何自动调整电子表格中的所有行吗 这是我的全部代码: Sub SearchFolders() 'UpdatebySUPERtoolsforExcel2016 Dim xFso As Object Dim xFld

我正在使用命令

.Columns("A:I").EntireColumn.AutoFit
要在运行循环后自动调整excel vbscript中的列,但在放置命令时出错:

Rows(xCount).EntireRow.AutoFit
自动调整图纸中行的高度

您能告诉我如何自动调整电子表格中的所有行吗

这是我的全部代码:

Sub SearchFolders()
'UpdatebySUPERtoolsforExcel2016
    Dim xFso As Object
    Dim xFld As Object
    Dim xUpdate As Boolean
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a forlder"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    xStrSearch = "failed"
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set xOut = wsReport
    xRow = 1
    With xOut
        .Cells(xRow, 1) = "Workbook"
        .Cells(xRow, 2) = "Worksheet"
        .Cells(xRow, 8) = "Unit"
        .Cells(xRow, 9) = "Status"
        Set xFso = CreateObject("Scripting.FileSystemObject")
        Set xFld = xFso.GetFolder(xStrPath)
        xStrFile = Dir(xStrPath & "\*.xlsx")
        Do While xStrFile <> ""
            Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
            For Each xWk In xWb.Worksheets
                Set xFound = xWk.UsedRange.Find(xStrSearch, LookIn:=xlValues)
                If Not xFound Is Nothing Then
                    xStrAddress = xFound.Address
                End If
                Do
                    If xFound Is Nothing Then
                        Exit Do
                    Else

                        xCount = xCount + 1
                        xRow = xRow + 1
                        .Cells(xRow, 1) = xWb.Name
                        .Cells(xRow, 2) = xWk.Name
                        .Cells(xRow, 3) = xFound.Address
                        WriteDetails rCellwsReport, xFound

                    End If
                    Set xFound = xWk.Cells.FindNext(After:=xFound)
                Loop While xStrAddress <> xFound.Address
            Next
            xWb.Close (False)
            xStrFile = Dir
        Loop
        .Columns("A:I").EntireColumn.AutoFit
        .Rows(xCount).EntireRow.AutoFit
    End With

    MsgBox xCount & "cells have been found", , "SUPERtools for Excel"
ExitHandler:
    Set xOut = Nothing


    Application.ScreenUpdating = xUpdate
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub

Private Sub WriteDetails(ByRef xReceiver As Range, ByRef xDonor As Range)
  xReceiver.Value = xDonor.Parent.Name
  xReceiver.Offset(, 1).Value = xDonor.Address

  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ' Copy the row of the Donor to the receiver starting from column D.
  ' Since you want to preserve formats, we use the .Copy method
    xDonor.EntireRow.Resize(, 100).Copy xReceiver.Offset(, 2)
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Set xReceiver = xReceiver.Offset(1)

End Sub
子搜索文件夹()
'UpdatebySUPERtoolsforExcel2016
像物体一样变暗
作为对象的Dim xFld
Dim xUpdate为布尔值
暗X等于长
关于错误转到错误处理程序
设置xFileDialog=Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect=False
xFileDialog.Title=“选择文件夹”
如果xFileDialog.Show=-1,则
xStrPath=xFileDialog.SelectedItems(1)
如果结束
如果xStrPath=“”,则退出Sub
xStrSearch=“失败”
xUpdate=Application.screen更新
Application.ScreenUpdating=False
设置xOut=wsReport
xRow=1
带xOut
.Cells(xRow,1)=“工作簿”
.单元格(X行,2)=“工作表”
.单元格(X行,8)=“单位”
.单元格(X行,9)=“状态”
设置xFso=CreateObject(“Scripting.FileSystemObject”)
设置xFld=xFso.GetFolder(xStrPath)
xStrFile=Dir(xStrPath&“\*.xlsx”)
在xStrFile“”时执行此操作
设置xWb=Workbooks.Open(文件名:=xStrPath&“\”&xStrFile,UpdateLinks:=0,ReadOnly:=True,AddToMRU:=False)
对于xWb.工作表中的每个xWk
设置xFound=xWk.UsedRange.Find(xStrSearch,LookIn:=xlValues)
如果不是,那么xFound什么都不是
xStrAddress=xFound.Address
如果结束
做
如果xFound什么都不是,那么
退出Do
其他的
xCount=xCount+1
xRow=xRow+1
.Cells(xRow,1)=xWb.Name
.Cells(xRow,2)=xWk.Name
.Cells(xRow,3)=xFound.Address
WriteDetails rCellwsReport,xFound
如果结束
设置xFound=xWk.Cells.FindNext(后面:=xFound)
在xStrAddress xFound.Address时循环
下一个
xWb.Close(假)
xStrFile=Dir
环
.Columns(“A:I”).entireclumn.AutoFit
.Rows(xCount).EntireRow.AutoFit
以
MsgBox xCount&“已找到单元格”,“SUPERtools for Excel”
出口商:
设置xOut=Nothing
Application.ScreenUpdate=xUpdate
出口接头
错误处理程序:
MsgBox错误说明,VBE感叹号
复出机
端接头
专用子写入详细信息(ByRef xReceiver作为范围,ByRef xDonor作为范围)
xReceiver.Value=xDonor.Parent.Name
xReceiver.Offset(,1).Value=xDonor.Address
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'从D列开始将施主行复制到接收方。
'由于要保留格式,我们使用.Copy方法
xDonor.EntireRow.Resize(,100).复制xReceiver.Offset(,2)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
设置xReceiver=xReceiver.Offset(1)
端接头

您是否缺少前面的


行(xCount).EntireRow.AutoFit
应为
行(xCount).EntireRow.AutoFit

是否有合并单元格?没有合并单元格。谢谢。
。行(xCount)
指的是一行。您将需要类似于
.Range(“A1:A”&xCount)的内容。Rows.EntireRow.AutoFit
。抱歉-我现在无法测试这个。我正在使用xcount运行一个循环,下面是我的代码片段:
xcount=xcount+1 xRow=xRow+1.Cells(xRow,1)=xWb.Name.Cells(xRow,2)=xWk.Name End If Loop而xStrAddress xFound.Address Next xWb.Close(False)xStrFile=Dir Loop.Columns(“A:I”).EntireColumn.AutoFit.Rows(xCount).EntireRow.AutoFit结束于
如果我尝试您的解决方案,看起来只有倒数第二行是正确的自动调整。我真的不知道你想用上面的代码片段实现什么-你能编辑你的原始文章,将其作为正确格式的代码部分包含进来吗