Searching and sorting ranges in excel with toggle buttons

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time.



My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain value.



So when I select the toggle button for 'Opel' for instance, all the named ranges containing 'Opel' are displayed. And when I select 'Opel' and 'Ford. All the ranges containing 'Opel' and 'Ford' are displayed while also being able to sort the ranges in that view.



For the sorting I use the following code:



Sub SorterenOpdrachten()

Dim Detail As Worksheet
Dim I As Long
Dim ListRng As Range
Dim LijstWks As Worksheet
Dim NamedRng As Name
Dim R As Long
Dim Rng As Range
Dim SortWks As Worksheet


'Worksheet declareren als variabele
Set Detail = Worksheets("detail")
Set LijstWks = Worksheets("LijstWks")
Set SortWks = Worksheets("SortWks")

'Startrij voor de lijst instellen = Rij 1 fungeert als "hoofding"
R = 2



'Ranges naar lijst kopiëren - Opdracht en uur
For Each NamedRng In ActiveWorkbook.Names



LijstWks.Cells(R, 1) = NamedRng.Name
LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
R = R + 1
Next NamedRng

'Ranges sorteren in de lijst
R = R - 1
Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)
ListRng.Sort Key1:=ListRng.Cells(1, 2), Order1:=xlAscending


'Ranges kopiëren naar SortWks
R = 1
For I = 1 To ListRng.Rows.Count
Set Rng = ActiveWorkbook.Names(ListRng.Cells(I, 1).Text).RefersToRange
Rng.Copy
SortWks.Cells(R, 1).PasteSpecial Paste:=xlPasteAll
R = R + Rng.Rows.Count
Next I

'Opdrachten naar detail kopiëren
R = 1
Worksheets("SortWks").Range("A1:T499").Copy
Worksheets("detail").Range("A5:T504")

Next intCounter
End Sub


This works fine.



But when I use it in combination with the toggle buttons it is too slow.



For the toggle buttons I use the following code:



Sub Tegels()

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = True
Next nm


If TglOpel Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Opel" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglChevrolet Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Chevrolet" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglFord Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Ford" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglBuick Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Buick" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglDodge Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Dodge" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If


End Sub


Sub CheckTegels()

If TglOpel Then
Call Tegels
Exit Sub
Else
If TglChevrolet Then
Call Tegels
Exit Sub
Else
If TglFord Then
Call Tegels
Exit Sub
Else
If TglBuick Then
Call Tegels
Exit Sub
Else
If TglDodge Then
Call Tegels
Exit Sub
Else

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = False
Next nm


End If
End If
End If
End If
End If
End If
End If
End If
End Sub


Do you have any tips for speeding this process up?



As you can probably tell I am quite new to this. Any help would be greatly appreciated!







share|improve this question














bumped to the homepage by Community♦ yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
    – user109261
    Jan 4 at 13:20










  • You need to post your complete code. As stated previously, you should also format your code.
    – user109261
    Jan 8 at 10:37










  • You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
    – user109261
    Jan 8 at 10:40
















up vote
1
down vote

favorite












I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time.



My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain value.



So when I select the toggle button for 'Opel' for instance, all the named ranges containing 'Opel' are displayed. And when I select 'Opel' and 'Ford. All the ranges containing 'Opel' and 'Ford' are displayed while also being able to sort the ranges in that view.



For the sorting I use the following code:



Sub SorterenOpdrachten()

Dim Detail As Worksheet
Dim I As Long
Dim ListRng As Range
Dim LijstWks As Worksheet
Dim NamedRng As Name
Dim R As Long
Dim Rng As Range
Dim SortWks As Worksheet


'Worksheet declareren als variabele
Set Detail = Worksheets("detail")
Set LijstWks = Worksheets("LijstWks")
Set SortWks = Worksheets("SortWks")

'Startrij voor de lijst instellen = Rij 1 fungeert als "hoofding"
R = 2



'Ranges naar lijst kopiëren - Opdracht en uur
For Each NamedRng In ActiveWorkbook.Names



LijstWks.Cells(R, 1) = NamedRng.Name
LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
R = R + 1
Next NamedRng

'Ranges sorteren in de lijst
R = R - 1
Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)
ListRng.Sort Key1:=ListRng.Cells(1, 2), Order1:=xlAscending


'Ranges kopiëren naar SortWks
R = 1
For I = 1 To ListRng.Rows.Count
Set Rng = ActiveWorkbook.Names(ListRng.Cells(I, 1).Text).RefersToRange
Rng.Copy
SortWks.Cells(R, 1).PasteSpecial Paste:=xlPasteAll
R = R + Rng.Rows.Count
Next I

'Opdrachten naar detail kopiëren
R = 1
Worksheets("SortWks").Range("A1:T499").Copy
Worksheets("detail").Range("A5:T504")

Next intCounter
End Sub


This works fine.



But when I use it in combination with the toggle buttons it is too slow.



For the toggle buttons I use the following code:



Sub Tegels()

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = True
Next nm


If TglOpel Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Opel" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglChevrolet Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Chevrolet" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglFord Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Ford" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglBuick Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Buick" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglDodge Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Dodge" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If


End Sub


Sub CheckTegels()

If TglOpel Then
Call Tegels
Exit Sub
Else
If TglChevrolet Then
Call Tegels
Exit Sub
Else
If TglFord Then
Call Tegels
Exit Sub
Else
If TglBuick Then
Call Tegels
Exit Sub
Else
If TglDodge Then
Call Tegels
Exit Sub
Else

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = False
Next nm


End If
End If
End If
End If
End If
End If
End If
End If
End Sub


Do you have any tips for speeding this process up?



As you can probably tell I am quite new to this. Any help would be greatly appreciated!







share|improve this question














bumped to the homepage by Community♦ yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
    – user109261
    Jan 4 at 13:20










  • You need to post your complete code. As stated previously, you should also format your code.
    – user109261
    Jan 8 at 10:37










  • You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
    – user109261
    Jan 8 at 10:40












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time.



My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain value.



So when I select the toggle button for 'Opel' for instance, all the named ranges containing 'Opel' are displayed. And when I select 'Opel' and 'Ford. All the ranges containing 'Opel' and 'Ford' are displayed while also being able to sort the ranges in that view.



For the sorting I use the following code:



Sub SorterenOpdrachten()

Dim Detail As Worksheet
Dim I As Long
Dim ListRng As Range
Dim LijstWks As Worksheet
Dim NamedRng As Name
Dim R As Long
Dim Rng As Range
Dim SortWks As Worksheet


'Worksheet declareren als variabele
Set Detail = Worksheets("detail")
Set LijstWks = Worksheets("LijstWks")
Set SortWks = Worksheets("SortWks")

'Startrij voor de lijst instellen = Rij 1 fungeert als "hoofding"
R = 2



'Ranges naar lijst kopiëren - Opdracht en uur
For Each NamedRng In ActiveWorkbook.Names



LijstWks.Cells(R, 1) = NamedRng.Name
LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
R = R + 1
Next NamedRng

'Ranges sorteren in de lijst
R = R - 1
Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)
ListRng.Sort Key1:=ListRng.Cells(1, 2), Order1:=xlAscending


'Ranges kopiëren naar SortWks
R = 1
For I = 1 To ListRng.Rows.Count
Set Rng = ActiveWorkbook.Names(ListRng.Cells(I, 1).Text).RefersToRange
Rng.Copy
SortWks.Cells(R, 1).PasteSpecial Paste:=xlPasteAll
R = R + Rng.Rows.Count
Next I

'Opdrachten naar detail kopiëren
R = 1
Worksheets("SortWks").Range("A1:T499").Copy
Worksheets("detail").Range("A5:T504")

Next intCounter
End Sub


This works fine.



But when I use it in combination with the toggle buttons it is too slow.



For the toggle buttons I use the following code:



Sub Tegels()

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = True
Next nm


If TglOpel Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Opel" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglChevrolet Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Chevrolet" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglFord Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Ford" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglBuick Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Buick" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglDodge Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Dodge" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If


End Sub


Sub CheckTegels()

If TglOpel Then
Call Tegels
Exit Sub
Else
If TglChevrolet Then
Call Tegels
Exit Sub
Else
If TglFord Then
Call Tegels
Exit Sub
Else
If TglBuick Then
Call Tegels
Exit Sub
Else
If TglDodge Then
Call Tegels
Exit Sub
Else

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = False
Next nm


End If
End If
End If
End If
End If
End If
End If
End If
End Sub


Do you have any tips for speeding this process up?



As you can probably tell I am quite new to this. Any help would be greatly appreciated!







share|improve this question













I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time.



My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain value.



So when I select the toggle button for 'Opel' for instance, all the named ranges containing 'Opel' are displayed. And when I select 'Opel' and 'Ford. All the ranges containing 'Opel' and 'Ford' are displayed while also being able to sort the ranges in that view.



For the sorting I use the following code:



Sub SorterenOpdrachten()

Dim Detail As Worksheet
Dim I As Long
Dim ListRng As Range
Dim LijstWks As Worksheet
Dim NamedRng As Name
Dim R As Long
Dim Rng As Range
Dim SortWks As Worksheet


'Worksheet declareren als variabele
Set Detail = Worksheets("detail")
Set LijstWks = Worksheets("LijstWks")
Set SortWks = Worksheets("SortWks")

'Startrij voor de lijst instellen = Rij 1 fungeert als "hoofding"
R = 2



'Ranges naar lijst kopiëren - Opdracht en uur
For Each NamedRng In ActiveWorkbook.Names



LijstWks.Cells(R, 1) = NamedRng.Name
LijstWks.Cells(R, 2) = NamedRng.RefersToRange.Cells(1, 2)
R = R + 1
Next NamedRng

'Ranges sorteren in de lijst
R = R - 1
Set ListRng = LijstWks.Range("A2").Resize(R - 1, 2)
ListRng.Sort Key1:=ListRng.Cells(1, 2), Order1:=xlAscending


'Ranges kopiëren naar SortWks
R = 1
For I = 1 To ListRng.Rows.Count
Set Rng = ActiveWorkbook.Names(ListRng.Cells(I, 1).Text).RefersToRange
Rng.Copy
SortWks.Cells(R, 1).PasteSpecial Paste:=xlPasteAll
R = R + Rng.Rows.Count
Next I

'Opdrachten naar detail kopiëren
R = 1
Worksheets("SortWks").Range("A1:T499").Copy
Worksheets("detail").Range("A5:T504")

Next intCounter
End Sub


This works fine.



But when I use it in combination with the toggle buttons it is too slow.



For the toggle buttons I use the following code:



Sub Tegels()

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = True
Next nm


If TglOpel Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Opel" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglChevrolet Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Chevrolet" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglFord Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Ford" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglBuick Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Buick" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If

If TglDodge Then
For Each nm In Application.Names
If Application.CountIf(Range(nm), "*" & "Dodge" & "*") Then
Range(nm).EntireRow.Hidden = False
Next nm
End If


End Sub


Sub CheckTegels()

If TglOpel Then
Call Tegels
Exit Sub
Else
If TglChevrolet Then
Call Tegels
Exit Sub
Else
If TglFord Then
Call Tegels
Exit Sub
Else
If TglBuick Then
Call Tegels
Exit Sub
Else
If TglDodge Then
Call Tegels
Exit Sub
Else

Dim nm As Name

For Each nm In Application.Names
Range(nm).EntireRow.Hidden = False
Next nm


End If
End If
End If
End If
End If
End If
End If
End If
End Sub


Do you have any tips for speeding this process up?



As you can probably tell I am quite new to this. Any help would be greatly appreciated!









share|improve this question












share|improve this question




share|improve this question








edited Mar 21 at 22:07









Sam Onela

5,88461545




5,88461545









asked Jan 4 at 11:31









Tommy

62




62





bumped to the homepage by Community♦ yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community♦ yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.









  • 1




    You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
    – user109261
    Jan 4 at 13:20










  • You need to post your complete code. As stated previously, you should also format your code.
    – user109261
    Jan 8 at 10:37










  • You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
    – user109261
    Jan 8 at 10:40












  • 1




    You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
    – user109261
    Jan 4 at 13:20










  • You need to post your complete code. As stated previously, you should also format your code.
    – user109261
    Jan 8 at 10:37










  • You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
    – user109261
    Jan 8 at 10:40







1




1




You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
– user109261
Jan 4 at 13:20




You need a RubberDuck. RubberDuck is a VBE add-in project that had a ton of features. Using its Code Formatter will not only improve the readability of your code but will help catch errors when code blocks are not opened and closed properly.
– user109261
Jan 4 at 13:20












You need to post your complete code. As stated previously, you should also format your code.
– user109261
Jan 8 at 10:37




You need to post your complete code. As stated previously, you should also format your code.
– user109261
Jan 8 at 10:37












You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
– user109261
Jan 8 at 10:40




You will get better performance if you unhide all the cells first. Next you should Union the ranges to be hidden and hide them all at once. Here are a few tips that will help Excel VBA Speed and Efficiency
– user109261
Jan 8 at 10:40










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Some basic things. You need your code to be indented consistently. That way you can see the levels of your code. If you had that you would see that on the last line of SorterenOpdrachten is a Next without a For. This won't compile.



In Tegels you have missed your first End If. And the second End If. And your third, fourth and fifth.



You haven't defined tglopel or TglChevrolet






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184272%2fsearching-and-sorting-ranges-in-excel-with-toggle-buttons%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Some basic things. You need your code to be indented consistently. That way you can see the levels of your code. If you had that you would see that on the last line of SorterenOpdrachten is a Next without a For. This won't compile.



    In Tegels you have missed your first End If. And the second End If. And your third, fourth and fifth.



    You haven't defined tglopel or TglChevrolet






    share|improve this answer

























      up vote
      0
      down vote













      Some basic things. You need your code to be indented consistently. That way you can see the levels of your code. If you had that you would see that on the last line of SorterenOpdrachten is a Next without a For. This won't compile.



      In Tegels you have missed your first End If. And the second End If. And your third, fourth and fifth.



      You haven't defined tglopel or TglChevrolet






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Some basic things. You need your code to be indented consistently. That way you can see the levels of your code. If you had that you would see that on the last line of SorterenOpdrachten is a Next without a For. This won't compile.



        In Tegels you have missed your first End If. And the second End If. And your third, fourth and fifth.



        You haven't defined tglopel or TglChevrolet






        share|improve this answer













        Some basic things. You need your code to be indented consistently. That way you can see the levels of your code. If you had that you would see that on the last line of SorterenOpdrachten is a Next without a For. This won't compile.



        In Tegels you have missed your first End If. And the second End If. And your third, fourth and fifth.



        You haven't defined tglopel or TglChevrolet







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Mar 21 at 21:47









        Raystafarian

        5,5131046




        5,5131046






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184272%2fsearching-and-sorting-ranges-in-excel-with-toggle-buttons%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation