How to skip certain command bar objects within a loop through the command bar collection

elgatnegre

New Member
Joined
Nov 19, 2014
Messages
7
Hi there!

I'm working in a macro that has a loop throught the command bar collection in order to get the name of each available command bar I have in excel and later do other stuff with it.

I need to skip certain command bar objects while doing the loop (few of them, e.g. I want to skip is the build-in command bar named "Formatting", and I've got their names)

How can I do that?

I've tried something like "If cbar.Name <> "Formatting" Then…" but I'm pretty new with vba and didn't know how to make it work.


This is my code:


Code:
Sub ShowHideCommandBarsGETVBA()


    Dim cbar As CommandBar
    Dim cbarname As Variant
    Dim Row As Long
    Dim textcode1 As String
    Dim textcode2 As String
    Dim textcode3 As String
    Dim textcode4 As String
    Dim textcode5 As String
    Dim textcode6 As String
    Dim textcode7 As String

    Row = 1
    
    For Each cbar In Application.CommandBars

        cbarname = Replace(cbar.Name, " ", "")
        
        textcode1 = "Sub ShowHide" & cbarname & " ()"
        textcode2 = "If Application.CommandBars(" & cbar.Name & ").Visible = True Then"
        textcode3 = "Application.CommandBars(" & cbar.Name & ").Visible = False"
        textcode4 = "Else:"
        textcode5 = "Application.CommandBars(" & cbar.Name & ").Visible = True"
        textcode6 = "End If"
        textcode7 = "End Sub"
        
        Cells(Row, 1) = textcode1
        Cells(Row + 1, 1) = Replace(Replace(textcode2, "(", "("""), ")", """)")
        Cells(Row + 2, 1) = Replace(Replace(textcode3, "(", "("""), ")", """)")
        Cells(Row + 3, 1) = Replace(Replace(textcode4, "(", "("""), ")", """)")
        Cells(Row + 4, 1) = Replace(Replace(textcode5, "(", "("""), ")", """)")
        Cells(Row + 5, 1) = Replace(Replace(textcode6, "(", "("""), ")", """)")
        Cells(Row + 6, 1) = Replace(Replace(textcode7, "(", "("""), ")", """)")
        
    Row = Row + 7


     Next cbar


End If
    
End Sub
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,216,069
Messages
6,128,600
Members
449,460
Latest member
jgharbawi

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