View Single Post
 
Old 08-15-2021, 01:12 AM
laith93 laith93 is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default Split function by comma and hyphen simultaneously

Hello, forums members,

I have this code to delete multiple pages

Code:
Sub DeleteMultiplePages()

'Delete Multiple Pages by displaying Inputbox

  Dim objRange As range
  Dim strPage As String
  Dim objDoc As Document
  Dim nSplitItem As Long

  Application.ScreenUpdating = False
 
  ' Initialize and enter page numbers of pages to be deleted.
  Set objDoc = ActiveDocument
  strPage = InputBox("Enter the page numbers of pages to be deleted: " & vbNewLine & _
            "use comma to separate numbers", "Delete Pages", "For example: 1,3")
  nSplitItem = UBound(Split(strPage, ","))

  ' Find specified pages and highlight their contents.
  For nSplitItem = nSplitItem To 0 Step -1
    With ActiveDocument
      Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=Split(strPage, ",")(nSplitItem)
      Set objRange = .Bookmarks("\Page").range
      objRange.Delete
    End With
  Next nSplitItem
 
  Application.ScreenUpdating = True
End Sub
The code works fine, but my problem is that sometimes I want to delete continuous pages range in addition to a certain page, eg: 1-3,5,10-15. Hence, I want to use split function in the previous code: nSplitItem = UBound(Split(strPage, ",")) based on comma and hyphen eg: 1-3,5,10-15, or just comma eg: 1,5,9 or just hyphen eg: 1-10.

Thank you
Reply With Quote