想要使用我们可以输入路径的文本框然后在按钮上单击数据填充

时间:2015-08-25 15:39:44

标签: excel-vba vba excel

我有一个vba宏,它目前的工作方式是在excel表中提取每个文件的文件名和修改日期。但问题是我需要每次都去宏并更新路径。我想要一个文本框,我可以在其中输入路径,然后在按钮上单击文件名,修改日期将提取到Excel工作表。以下是我使用的当前代码:

Sub FileT()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim tempString As String


newString = Left(originalString, 1)

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("C:\Users\rd4470\Documents\Test Folder")
i = 1

For Each objFile In objFolder.Files
tempString = Left(objFile.Name, 1)
If tempString = "T" Then

    Cells(i + 1, 3) = objFile.Name

    Cells(i + 1, 4) = objFile.DateLastModified

    i = i + 1
End If

    Next objFile

End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用路径的文本框值。 objFSO.GetFolder(TextBox1.Text)

Sub FileT()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim tempString As String

    newString = Left(originalString, 1)

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFolder = objFSO.GetFolder(TextBox1.Text)
    i = 1

    For Each objFile In objFolder.Files

        tempString = Left(objFile.Name, 1)
        If tempString = "T" Then

            Cells(i + 1, 3) = objFile.Name

            Cells(i + 1, 4) = objFile.DateLastModified

            i = i + 1
        End If

    Next objFile

End Sub

答案 1 :(得分:0)

您可以使用输入框

sFldr = Application.InputBox("Enter path",,,,,,,2)
...
If Len(Dir(sFldr, vbDirectory)) > 0 Then
    Set objFolder = objFSO.GetFolder(sFldr)
    ...
End If