Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 03-04-2020, 03:15 AM
gmayor's Avatar
gmayor gmayor is offline VBA - copy sheet from excel Windows 10 VBA - copy sheet from excel Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
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

It is basically a matter of defining the additional bookmark names in the main macro and repeating the marked section for each of them
Code:
Sub CopyWorksheet()
'Graham Mayor - https://www.gmayor.com - Last updated - 04 Mar 2020
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim bStarted As Boolean, bOpen As Boolean
Dim strWB As String
Dim strBM As String
Dim strSheet As String

    strWB = BrowseForFile("Select Workbook", True)

    If strWB = "" Then
        Beep
        GoTo lbl_Exit
    End If
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err Then
        Set xlApp = CreateObject("Excel.Application")
        bStarted = True
    End If
    On Error GoTo lbl_Exit
    xlApp.Visible = True

    For Each xlBook In xlApp.Workbooks
        If xlBook.Name = strWB Then
            bOpen = True
            Exit For
        End If
    Next xlBook
    If Not bOpen Then Set xlBook = xlApp.Workbooks.Open(FileName:=strWB)

'Repeat this section to define each bookmark and the sheet/range you wish to copy to it
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    strBM = "BookmarkName1"    ' the name of the bookmark
    strSheet = "Sheet1"      'the name of the sheet to copy
    
    Set xlSheet = xlBook.Sheets(strSheet)
    xlSheet.UsedRange.Copy
    If ActiveDocument.Bookmarks.Exists(strBM) = False Then
        MsgBox "Bookmark " & strBM & " does not exist?"
    Else
        XLSheetToBM strBM
    End If
 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    xlApp.DisplayAlerts = False
    xlApp.DisplayAlerts = True
    If bStarted = True Then
        xlBook.Close 0
        xlApp.Quit
    End If
lbl_Exit:
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlSheet = Nothing
    Exit Sub
End Sub
__________________
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA - copy sheet from excel How to copy excel sheet withe HEADER and Paste into new sheet? cloudforgiven Excel Programming 6 01-05-2017 07:30 PM
copy checkbox string and paste it in excel sheet MOHAMMEDSALMAN Excel Programming 7 10-29-2015 03:50 AM
How to copy all data from a website to excel sheet? Plz plz plz plz plz help...urgent Sam123 Excel 0 07-19-2014 02:12 AM
VBA - copy sheet from excel Find Results in excel copy the rows to another sheet khalidfazeli Excel 2 02-06-2013 09:38 AM
How to Copy data from Outlook mail and Paste it in a Excel sheet? padhu1989 Outlook 0 09-11-2012 04:07 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:58 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