Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 08-15-2021, 05:16 AM
gmayor's Avatar
gmayor gmayor is offline Split function by comma and hyphen simultaneously Windows 10 Split function by comma and hyphen simultaneously Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I started to answer but was distracted by a Skype call, and I see Andrew has been busy in the meantime, but the following alternative should work, using a function to get the page numbers
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
Dim oCol As Collection

    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")

    Set oCol = GetPages(strPage)

    ' Find specified pages and highlight their contents.
    For nSplitItem = oCol.Count To 1 Step -1
        With ActiveDocument
            Selection.GoTo What:=wdGoToPage, which:=wdGoToAbsolute, Count:=Split(strPage, ",")(oCol(spliitem))
            Set objRange = .Bookmarks("\Page").Range
            objRange.Delete
        End With
    Next nSplitItem

    Application.ScreenUpdating = True
End Sub

Private Function GetPages(sRange As String) As Collection
Dim vRange As Variant
Dim oCol As Collection
Dim i As Integer, j As Integer
Dim iStart As Integer, iEnd As Integer
    Set oCol = New Collection
    On Error Resume Next
    vRange = Split(sRange, ",")
    For i = 0 To UBound(vRange)
        Select Case True
            Case InStr(1, vRange(i), "-") > 0
                iStart = CInt(Split(CStr(vRange(i)), "-")(0))
                iEnd = CInt(Split(CStr(vRange(i)), "-")(1))
                For j = iStart To iEnd
                    oCol.Add Trim(j)
                Next j
            Case Else
                oCol.Add vRange(i)
        End Select
    Next i
    Set GetPages = oCol
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
word 19, word vba, word vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split function by comma and hyphen simultaneously Mistake in the help file for String "Split Function"? John 4 Word VBA 6 11-19-2020 06:02 AM
Hyphen until the end of the row abc3132 Word 13 10-31-2019 04:36 AM
Split function in Excel (split the screen) Officer_Bierschnitt Excel 1 07-05-2017 07:02 AM
Split function not working as I expect??!! XmisterIS Word VBA 9 04-08-2014 04:41 PM
Split function by comma and hyphen simultaneously Cross-reference function to update bullet header and number simultaneously dljenks Word 1 01-03-2014 01:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft