vendredi 25 novembre 2016

VBA Userform to select checkboxes and extract data based on their filter

I currently have a worksheet labelled "Register".

I have this code whereby it extracts this worksheet and provides the 'save as' option to a folder.

The worksheet has headers and these headers have filters. The columns: O-U are labelled as follows:

Column O: Marston Green

Column P: Test Engineering

Column Q: West Hartford

Column R: Singapore

Column S: Xiamen

Column T: Neuss

Column U:Dubai

The data within these columns are of True/False only and the data begins on row 6.

I have created a userform interface that when ran allows the user to select a location out of the above via checkboxes and there are also three option buttons: True, False, Both.

What I'm trying to do with this userform interface is as follows:

1)

When you select a location and select True. It will filter True for that location and extract the worksheet.

When you select a location and select False it will filter False for that location extract the worksheet.

When you select a location and select BOTH true and false it will just extract the worksheet.

If one location doesn't have any True/False options and the user selects True/False and presses 'extract' - a notification will come up stating "No true/false options for this location- please amend search".

2)

The extracted worksheet doesn't have filters function on. So there are no filters on the extracted worksheet.

The current coding I have within the userform:

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdClear_Click()
'Clear the form
    For Each ctl In Me.Controls
    If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
        ctl.Value = ""
    ElseIf TypeName(ctl) = "CheckBox" Then
        ctl.Value = False
    End If
   Next ctl
End Sub
Private Sub cmdExtract_Click()
If MsgBox("                      Please Confirm", vbYesNo) = vbNo Then Exit Sub
Application.DisplayAlerts = False
Dim wb As Workbook, InitFileName As String, fileSaveName As String

    InitFileName = ThisWorkbook.Path & "\ Extracted_Register_" & Format(Date, "dd-mm-yyyy")

    Sheets("Register").Copy
     ' or below for more than one sheet
     ' Sheets(Array("Output", "Sheet2", "Sheet3")).Copy

    Set wb = ActiveWorkbook

    fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitFileName, _
    filefilter:="Excel files , *.xlsx")

     With wb
        If fileSaveName <> "False" Then

            .SaveAs fileSaveName
            .Close
        Else
            .Close False
            Exit Sub
        End If
    End With


MsgBox ("Extraction Completed")
Unload Me
Application.DisplayAlerts = True
End Sub

Private Sub UserForm_Click()

End Sub




Aucun commentaire:

Enregistrer un commentaire