mits
mits

Reputation: 926

Remove tab from tabstrip in userform - vba excel

I have a form with several controls. It includes a tabstrip with one tab. I allow users to add tabs, by clicking commandbutton1. So far so well.

Now I also want users to be able to remove the last tab (except for the first one) by clicking commandbutton2. The problem is that, despite the fact that I've been searching for about two hours, I cannot find the right syntax for method "remove" when it comes to tabs. Any help appresiated.

edited:

Making the above question more specific: What's the right syntax for "remove" method when it's parameter is a string-variable and not a specific tab name. (for example removing the current tab)

e.g. It would be great if one of the following would work:

Sub test
    Dim nm As String

    With TabStrip1
        nm = .SelectedItem.Name
        .Tabs.Remove (nm)
    End With
End Sub

or

Sub test
    With TabStrip1
        .Tabs.Remove (.SelectedItem.Name)
    End With
End Sub

Upvotes: 1

Views: 2843

Answers (2)

mits
mits

Reputation: 926

Finally found the way. remove method accepts a tab's caption as an argument and not its name.

In this case the following worked great:

Private Sub CommandButton2_Click()
    Dim nm As String

    With TabStrip1
        nm = .SelectedItem.Caption
        .Tabs.Remove (nm)
    End With
End Sub

or

Private Sub CommandButton2_Click()
    With TabStrip1
        .Tabs.Remove (.SelectedItem.Caption)
    End With
End Sub

Upvotes: 1

Moosli
Moosli

Reputation: 3285

Hi you can Remove a Tab by the TabStrip1.Tabs.Remove Line.

Private Sub CommandButton1_Click() 'Insert the Tap Test
       TabStrip1.Tabs.Add ("Test")
    End Sub

    Private Sub CommandButton2_Click()'Removes the Tap Test
       TabStrip1.Tabs.Remove ("Test")
    End Sub

The Rest you can Handle with a something Like this:

If TabStrip1.Tabs.Count >= 2 Then
    TabStrip1.Tabs.Remove (TabStrip1.Tabs.Count - 1)
End If

You have to check fist if there are more then two Taps. If yes delete the last Tap. The Hint her is, that the Index starts with the Second Tap!

Upvotes: 0

Related Questions