Creating a folder from input in a user form

时间:2018-06-04 17:38:34

标签: vba excel-vba userform create-directory excel

I'm literally a beginner with VBA so don't really know much about it, but i've been tasked with creating folders based on user input into a VBA user form which get output to an excel sheet.

I have three columns the user can input values into: Year, Type, and File Name

Year is a text box they can enter the year into. Type is a list box. File name is a text box

I don't have an issue getting the user input to show up onto the excel sheet, but I can't seem to figure out how to create the folder. Here is my code:

'create functions for checking if folder exists and creating folder
Function FolderExists(ByVal path As String) As Boolean

    FolderExists = False
    Dim fso As New FileSystemObject

    If fso.FolderExists(path) Then FolderExists = True

End Function

Function FolderCreate(ByVal path As String) As Boolean

    FolderCreate = True
    Dim fso As New FileSystemObject

    If Functions.FolderExists(path) Then
        Exit Function
    Else
        On Error GoTo DeadInTheWater
        fso.CreateFolder path
        Exit Function
    End If

DeadInTheWater:
    MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again."
    FolderCreate = False
    Exit Function

End Function

Function CleanName(strName As String) As String
    'will clean part # name so it can be made into valid folder name
    'may need to add more lines to get rid of other characters

    CleanName = Replace(strName, "/", "")
    CleanName = Replace(CleanName, "*", "")

End Function

Private Sub Label1_Click()
End Sub

Private Sub Label2_Click()
End Sub

Private Sub TotMktSeg_Click()
End Sub

Private Sub Label3_Click()
End Sub

Private Sub TextBox_Filename_Change()
End Sub

'create dropdown options in the listbox
Private Sub UserForm_Initialize()

    'Me.ListBox_TotMktSeg.AddItem "Market"
    'Me.ListBox_TotMktSeg.AddItem "Segment"
    'Me.ListBox_TotMktSeg.AddItem "Total"

    With Me.ListBox_TotMktSeg
        .AddItem "Market"
        .AddItem "Segment"
        .AddItem "Total"
    End With

End Sub

Private Sub Year_Change()
End Sub

'when button is clicked, output user input to excel sheet and create a folder
Private Sub Run_Click()

    'Copy input values to sheet.
    Dim lRow As Long
    Dim ws As Worksheet
    Dim strType As String
    Dim strYear As String
    Dim strPath As String
    Dim strFile As String

    Set ws = Worksheets("Sheet1")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    With ws
        .Cells(lRow, 1).value = Me.TextBox_Year.value
        .Cells(lRow, 2).value = Me.ListBox_TotMktSeg.value
        .Cells(lRow, 3).value = Me.TextBox_Filename.value
    End With

    strType = Me.TextBox_Year.value
    strYear = Me.ListBox_TotMktSeg.value
    strFile = Me.TextBox_Filename.value
    strPath = "C:\Users\siope1kd\Documents\HD VB stuff\"

    If Not FolderExists(strPath & strType) Then
        FolderCreate (strPath & "\" & strType & "\" & strYear)
    Else
        If Not FolderExists(strPath & strType & "\" & strYear) Then
            FolderCreate (strPath & strType & "\" & strYear)
        End If
    End If

    'Clear input controls.
    Me.TextBox_Year.value = ""
    Me.ListBox_TotMktSeg.value = ""
    Me.TextBox_Filename.value = ""

End Sub

The main issue is I get

runtime error 424 stating "Object Required"

for the FolderExists Function.

I literally pieced this code together from other sources and cannot figure out the issue here.

Thanks so much for the help.

Keith

0 个答案:

没有答案