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)
 
never mind, i found solution that works for me. when i create new page it will write name of page to new row of sheet "calc 1". this will delete whole row with name of selected page.

VBA Code:
Public Sub RemoveMatchingRows()

Dim rngFound As Range
Dim sht1 As Worksheet

Set sht1 = ThisWorkbook.Worksheets("calc 1")
With sht1.Range("A1:A500")
    Set rngFound = .Find(MultiPage1.Pages(Me.MultiPage1.Value).name, LookIn:=xlValues)
    If Not rngFound Is Nothing Then
        While Not rngFound Is Nothing
            sht1.Rows(rngFound.Row).EntireRow.Delete
            Set rngFound = .FindNext
        Wend
    End If
End With
End Sub
 
Upvote 0
Solution

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

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