VBA multipage adding and deleting page

pepsimus

New Member
Joined
Sep 3, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi can someone help me with this code. I am trying delete pages in multipage with diferent names and renaming them back.

this code works fine but only when page name is "pozice" + number
when i have pages pozice1, pozice2, pozice3, pozice4 then for example delete page pozice2 it will change name of pozice3 to pozice2 and name of pozice4 to pozice3
VBA Code:
Private Sub btndeletepage_Click()
    Dim i As Integer
    
    
    With Me.multipage1
        If .Pages.Count > 1 Then
            .Pages.Remove .Value
            If .Pages.Count > 0 Then
                For i = .Value To .Pages.Count - 1
                    .Pages(i).Caption = "pozice " & i + 1
                Next i
                
             End If
            Else
            MsgBox " Nemůžeš vymazat všechny pozice "
        End If

    End With
        Application.DisplayAlerts = False
        If i <> 0 Then
        Sheets("výpočet" & i + 1).Delete
        End If
        Application.DisplayAlerts = True
End Sub

but id like to change it so it will delete pages with diferent names.X1, Y2, X3, Y4, Y5 and so on. This code works fine when i delete last page but when i delate page in middle for example Y2 it will make it as X1, X3, Y4 and so on. instead of X1, X2, Y3, Y4

Rich (BB code):
VBA Code:
Private Sub btndeletepage_Click()
    Dim i As Integer
    Dim ci As Integer
    
    With Me.multipage1
        If .Pages.Count > 1 Then
            .Pages.Remove .Value
            If .Pages.Count > 0 Then
              If .Pages(i).Caption = "Obdelník" & i Then
                For i = .Value To .Pages.Count - 1
                    .Pages(i).Caption = "Obdelník " & i + 1
                    Next i
                 ElseIf .Pages(i).Caption = "Mezikruží" & i Then
                    For i = .Value To .Pages.Count - 1
                    .Pages(i).Caption = "Mezikruží " & i + 1
                    Next i
                 End If
               'ci = multipage1.Pages.Count
               Application.DisplayAlerts = False
               'Sheets("calc" & i + 1).Delete
               Application.DisplayAlerts = True
              End If
           Else
            MsgBox " Nemůžeš vymazat všechny pozice "
        End If

    End With
        


End Sub

/CODE]
Can someone help me with this? i think problem is when i click delete page it will lose value of (i)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try setting your deleting loop to loop backwards so that it deletes from the highest numbers first, that way it won't effect the numbers below it.
 
Upvote 0
Deleting pages backwards works fine, but its necessary to make it work from midle as well. do you have any ideas how to make it work?
 
Upvote 0
Please post the current version of the code that you are trying as well as specific examples of where it does not work as you want it to. ie. middle?
 
Upvote 0
here is my code i chaged it little bit, so i dont use my country lanuage so it should work for everyone.

second deletepage button works fine, but ulny for same page names.
 
Upvote 0
Let us know if this does what you want: It will keep the names, only the numbers are changed.

VBA Code:
Private Sub CommandButton1_Click()
    Dim i                       As Long
    Dim j                       As Long
    Dim PageNameAndNumberLength As Long
    Dim PageNameAndNumber       As String
    Dim PageNameOnly            As String
'
    With Me.MultiPage1
        If .Pages.Count > 1 Then
            .Pages.Remove .Value
            If .Pages.Count > 0 Then
                For i = .Value To .Pages.Count - 1
                    PageNameAndNumber = .Pages(i).Caption                           ' Get Name of tab
                    PageNameAndNumberLength = Len(PageNameAndNumber)                ' Get the total length of the Name
'
                    For j = PageNameAndNumberLength To 1 Step -1                    ' Start checking from the right side of the Name for the first non numeric character
                        If Not (IsNumeric(Mid(PageNameAndNumber, j, 1))) Then       '   If non numeric character found then ...
                            PageNameOnly = Left(PageNameAndNumber, j)               '       Save it & all characters to the left
                            Exit For                                                '       Exit loop
                        End If
                    Next
'
                    .Pages(i).Caption = PageNameOnly & i + 1                        ' add new number to the end of PageName
                Next i
             End If
        Else
            MsgBox " Nemùžeš vymazat všechny pozice "
        End If
    End With
End Sub
 
Last edited:
Upvote 0
can i ask you one more question?
i´d like to connect text boxes with cell values dynamicaly, if its possible. now deleting pages works fine and adding new ones as well. but when i delete page in middle connecting with textboxes are mixed up. is it possible to make it when i delete page in middle, cell connections will still corespond ?
here is my example:
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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