+ Reply to Thread
Results 1 to 12 of 12

How to Continue with the Code if one of the multiple selections is not made

  1. #1
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Post How to Continue with the Code if one of the multiple selections is not made

    With the below Code, it needs all 3 fields (MY, Order, Quantity) to be entered a value to populate final filtered data. I want the code to have flexibility to run with either 1 ,2 or 3 selections (i.e if Only MY and Order is selected or Just MY or all three) Please help!

    ----------------------------------------------------------------------------------------------------------
    Sub Filter_Data()

    Dim All_Data_sh As Worksheet
    Dim Filter_sh As Worksheet
    Dim Output_sh As Worksheet

    Set All_Data_sh = ThisWorkbook.Sheets("All_Data")
    Set Filter_sh = ThisWorkbook.Sheets("Filter")
    Set Output_sh = ThisWorkbook.Sheets("Output")

    Output_sh.UsedRange.Clear

    All_Data_sh.AutoFilterMode = False

    Dim MY() As String
    Dim Order() As String
    Dim Quantity() As String

    Dim n As Integer
    n = Application.WorksheetFunction.CountA(Filter_sh.Range("A:C")) - 2
    ReDim MY(n) As String
    ReDim Order(n) As String
    ReDim Quantity(n) As String

    Dim i As Integer
    For i = 0 To n
    MY(i) = Filter_sh.Range("A" & i + 2)
    Order(i) = Filter_sh.Range("B" & i + 2)
    Quantity(i) = Filter_sh.Range("C" & i + 2)

    Next i


    All_Data_sh.UsedRange.AutoFilter 1, MY(), xlFilterValues
    All_Data_sh.UsedRange.AutoFilter 2, Order(), xlFilterValues
    All_Data_sh.UsedRange.AutoFilter 3, Quantity(), xlFilterValues

    All_Data_sh.UsedRange.Copy Output_sh.Range("A1")

    All_Data_sh.AutoFilterMode = False

    MsgBox ("Data Filtered")

    End Sub

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,977

    Re: How to Continue with the Code if one of the multiple selections is not made

    Could you set the filters as arguments of the Sub defaulting to True?

    Please Login or Register  to view this content.
    Usage:
    Leave blank for all, or set those to exclude to false
    e.g.
    All: Call Filter_Data
    MY and order only: call Filter_Data(,,False)

  3. #3
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    I made your above changes highlighted in blue and it did not work. I am just starting to learn excel VBA so dont follow much.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,977

    Re: How to Continue with the Code if one of the multiple selections is not made

    The idea is that it has a parameter for each filter which defaults to True, i.e. the filter will be applied. If you specify False for any or all of these when calling Filter_Data from another routine, it won't apply the filter for those.


    What if you run these

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    Thank you ByteMarks! This helps.

  6. #6
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    Can Something like this be done, So that I dont have to create macros for all the different combinations of true, false.

    If MY() ="" Then MY()="*"
    If Order() ="" Then Order()="*"
    If Quantity() ="" Then Quantity()="*"

    I am not sure if the above line of code will do anything but can something like this be done to that if any empty cell is present then it just moves on to the next filter criteria and popullates based on that.

  7. #7
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,977

    Re: How to Continue with the Code if one of the multiple selections is not made

    Does this work for you?

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    The code doesn't throw any error but no filtered result. No filtering happens

  9. #9
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,977

    Re: How to Continue with the Code if one of the multiple selections is not made

    I'm assuming you put the filter criteria in columns ABC of Filter_sh
    Is it possible to attach your workbook?

  10. #10
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    Yes, attached the workbook.
    Eventually i want to have the user the ability to filters for any combinations of the entry. Thanks again ByteMarks for helping me out!
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,977

    Re: How to Continue with the Code if one of the multiple selections is not made

    How about this.

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-07-2023
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    18

    Re: How to Continue with the Code if one of the multiple selections is not made

    This is Perfect. Works like I needed.
    Thank you So much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculate pricing according to selections made
    By wezelo in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-25-2022, 09:36 PM
  2. Way to show total (sum) from selections made by slider
    By sniper1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-22-2020, 08:46 AM
  3. How do you match one cell to the selections made in two drop down menu's?
    By zhunter71 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-10-2018, 03:20 PM
  4. [SOLVED] VBA running slow when selections are made
    By cinstanl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-13-2016, 02:51 PM
  5. Running pre recorded macros from selections made in list box
    By Aestivator1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2015, 08:18 AM
  6. Run different Macros based on what selections have been made in checkbox
    By vetteengr in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-13-2012, 01:13 PM
  7. Replies: 0
    Last Post: 11-19-2005, 11:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1