Execute Macro using Listbox on Userform

goob90

New Member
Joined
Nov 12, 2021
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have a code in my workbook that exports the workbook as a CSV in a specific format. I have multiple sheets in my workbook, one called "Data" and twelve others named for each month. I would like to be able to run the code by having the user select which sheet (or month) they would like to export and only have the chosen sheet exported.

Can anyone assist me with this? And maybe how to change the export file name as the month of the sheet?

VBA Code:
Sub CreateCSV()
    Dim a, b, w, i As Long, ii As Long, n As Long
    a = Sheets("sheet1").Cells(1).CurrentRegion.Value
    ReDim b(1 To UBound(a, 1) * 2)
    For i = 2 To UBound(a, 1)
        ReDim w(1 To 26)
        w(1) = a(i, 2): w(3) = a(i, 3): w(4) = "active"
        w(5) = "-": w(22) = a(i, 4)
        For ii = 8 To 9: w(ii) = "yes": Next
        For ii = 10 To 14: w(ii) = "no": Next
        For ii = 24 To 26: w(ii) = "-": Next
        n = n + 1: b(n) = Join(w, ",")
        If (w(3) Like "MP*") + (w(3) Like "DP*") Then
            w(3) = Left$(w(3), 2) & "B"
            n = n + 1: b(n) = Join(w, ",")
        End If
    Next
    ReDim Preserve b(1 To n)
    Open ThisWorkbook.Path & "\example.csv" For Output As #1
        Print #1, Join(b, vbCrLf);
    Close #1
End Sub


Thanks!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
The the following . . .

VBA Code:
Sub CreateCSV()

    'if user did not select a month, exit sub
    With Me.ListBox1
        If .ListIndex = -1 Then
            MsgBox "Please select a month for which to" & vbCrLf & "export the data, and try again!", vbExclamation
            Exit Sub
        End If
    End With
    
    Dim exportSheetName As String
    exportSheetName = Me.ListBox1.Value

    Dim a, b, w, i As Long, ii As Long, n As Long
    a = Sheets(exportSheetName).Cells(1).CurrentRegion.Value
    'etc
    '
    '
End Sub

Note that it assumes that the code resides within the userform code module. If this is not the case, simply replace Me with the name of your userform. For example, if your userform is called UserForm1, replace . . .

VBA Code:
With Me.ListBox1

with

VBA Code:
With UserForm1.ListBox1

Hope this helps!
 
Upvote 0
Solution
The the following . . .

VBA Code:
Sub CreateCSV()

    'if user did not select a month, exit sub
    With Me.ListBox1
        If .ListIndex = -1 Then
            MsgBox "Please select a month for which to" & vbCrLf & "export the data, and try again!", vbExclamation
            Exit Sub
        End If
    End With
   
    Dim exportSheetName As String
    exportSheetName = Me.ListBox1.Value

    Dim a, b, w, i As Long, ii As Long, n As Long
    a = Sheets(exportSheetName).Cells(1).CurrentRegion.Value
    'etc
    '
    '
End Sub

Note that it assumes that the code resides within the userform code module. If this is not the case, simply replace Me with the name of your userform. For example, if your userform is called UserForm1, replace . . .

VBA Code:
With Me.ListBox1

with

VBA Code:
With UserForm1.ListBox1

Hope this helps!
This is awesome! Thank you so much! Do you know how I can set the export file name to be the sheet name that is selected from the listbox?
 
Upvote 0
In that case, try the following instead . . .

VBA Code:
Open ThisWorkbook.Path & "\" & exportSheetName  & ".csv" For Output As #1
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,144
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top