简体   繁体   中英

VBA Userform: Get xlFileFormat as input

I created a sub to loop through all worksheets and Save As CSV - but then I wanted to take things a step further and give users an option to define which sheets should be exported and what format to use. My quandary is how best to collect this information from the user.

For the sake of completeness, here's the entire UserForm - but what I'm hoping to see what ideas there are for displaying a readable list of file formats to the users, and then converting those back to a variable of xlFileFormat type. I'm planning to use descriptions and names from the actual enumerations here , but I guess the question is - do I store the options I want to be available in a hidden sheet and then use that table to populate the UserForm with friendly descriptions and then use it as a lookup table to map the selection to a valid enumeration?

The question is greater than my current task since I couldn't find an easy example or answer out there. I could use Select Case for the small amount of options I'm offering, but I wanted something scalable. For the attached code, I'm planning to pass the exportFormat into my Export Sub right after users click OK

Private Sub OKButton_Click()
'translate file format selection to
Select Case ExportFormatList.Value
    Case "CSV"
        exportFormat = xlCSV
End Select


End Sub

Private Sub UserForm_Initialize()
'Empty fields
SheetOptionsList.Value = ""
ExportFormatList.Value = ""

'Fill SheetOptionsListBox
With SheetOptionsList
    .AddItem "Just the active sheet"
    .AddItem "Specific sheets, let me choose!"
    .AddItem "All sheets that can be exported"
End With

'Fill ExportFormatListBox
With ExportFormatList
    .AddItem "CSV"
    .AddItem "Test"
End With

'Set defaults
SheetOptionsList.Value = "Just the active sheet"
ExportFormatList.Value = "CSV"

'Set focus to first field
SheetOptionsList.SetFocus

End Sub

Use a dictionary (you'll need to add a reference to Microsoft Scripting Runtime). Take the friendly names (CSV, Open XML Workbook, Open XML Workbook Macro Enabled, for instance) and set those as the keys, with the enum as the value, like this:

Function getEnums() As Scripting.Dictionary
    
    Set buffer = New Dictionary
    buffer.Add "CSV", xlCSV
    buffer.Add "Open XML Workbook", xlOpenXMLWorkbook
    buffer.Add "Open XML Workbook Macro Enabled", xlOpenXMLWorkbookMacroEnabled
    
    Set getEnums = buffer
    
End Function

Then take the friendly name passed back from your user form and look it up like this:

actualFormat = getEnums("CSV")

You can see it loop the dictionary here:

Sub testEnums()
    
    Set typeEnums = getEnums
        
    For Each k In typeEnums.Keys
        Debug.Print k & " : " & typeEnums(k)
        Next
    
End Sub

You could use Public Enum to set it up, but if you want to keep the spaces in the friendly format names, I think that's out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM