当前代码显示A2单元格中的列表,但是我希望修改VBA代码,使列表从B2单元格开始?

时间:2017-12-07 02:50:47

标签: excel vba excel-vba

下面显示的VBA代码有助于捕获文件夹和子文件夹中的文件名列表,并在从A2单元格开始的Excel工作表中显示该列表,但我希望以列表开始的方式修改VBA代码来自B2细胞?

Sub MainList()

    Set folder = Application.FileDialog(msoFileDialogFolderPicker)

    If folder.Show <> -1 Then Exit Sub

    xDir = folder.SelectedItems(1)
    Call ListFilesInFolder(xDir, True)
End Sub

Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
    Dim xFileSystemObject As Object
    Dim xFolder As Object
    Dim xSubFolder As Object
    Dim xFile As Object
    Dim rowIndex As Long

    rowIndex = 2

    Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFileSystemObject.GetFolder(xFolderName)

    rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1

    For Each xFile In xFolder.Files
        Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
        rowIndex = rowIndex + 1
    Next xFile

    If xIsSubfolders Then
        For Each xSubFolder In xFolder.SubFolders
            ListFilesInFolder xSubFolder.Path, True
        Next xSubFolder
    End If

    Set xFile = Nothing
    Set xFolder = Nothing
    Set xFileSystemObject = Nothing
End Sub

Function GetFileOwner(ByVal xPath As String, ByVal xName As String)
    Dim xFolder As Object
    Dim xFolderItem As Object
    Dim xShell As Object

    xName = StrConv(xName, vbUnicode)
    xPath = StrConv(xPath, vbUnicode)

    Set xShell = CreateObject("Shell.Application")
    Set xFolder = xShell.Namespace(StrConv(xPath, vbFromUnicode))

    If Not xFolder Is Nothing Then
        Set xFolderItem = xFolder.ParseName(StrConv(xName, vbFromUnicode))
    End If

    If Not xFolderItem Is Nothing Then
        GetFileOwner = xFolder.GetDetailsOf(xFolderItem, 8)
    Else
        GetFileOwner = ""
    End If

    Set xShell = Nothing
    Set xFolder = Nothing
    Set xFolderItem = Nothing
End Function

请让我知道如何进行修改,以便起始单元格将是B2而不是A2。

1 个答案:

答案 0 :(得分:0)

更改说:

的部分
Cells(rowIndex, 1)

Cells(rowIndex, 2).

还有一些推荐阅读: