+ Reply to Thread
Results 1 to 2 of 2

Excel vba to copy sheet from current workbook to new Excel sheet ?

  1. #1
    Registered User
    Join Date
    04-16-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    15

    Unhappy Excel vba to copy sheet from current workbook to new Excel sheet ?

    Here's my code. Basically with this code, I have the SharePoint 2010 list items imported to current Excel workbook in new sheet which is sheet4. Since I m using vba macro to do it, I need to feed this macro enabled xlsx file to third party tool. That third party tool doesn't take macro enabled excel file. So I want to write a vba code that would simply copy the existing ListObjects to a new Excel workbook which I can save using xlsx extension and easily provide as an i/p to my third party tool. Below, I have created a Demo xlsx file. I want to copy the sheet4 of current workbook to Sheet1 in Demo xlsx file. How do I do it? Thanks.

    Sub TestMacro()
    Dim objMyList As ListObject
    Dim objWksheet As Worksheet
    Dim strSPServer As String
    Const SERVER As String = "http://abcd/"
    Const LISTNAME As String = "{A486016E-80B2-44C3-8B4A-8394574B9430}" Const VIEWNAME As String = ""
    ' The SharePoint server URL pointing to ' the SharePoint list to import into Excel.
    strSPServer = "http://" & SERVER & "/_vti_bin"
    ' Add a new worksheet to the active workbook.
    Set objWksheet = Worksheets.Add
    ' Add a list range to the newly created worksheet
    ' and populated it with the data from the SharePoint list.
    Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, _ Array(strSPServer, LISTNAME, VIEWNAME), True, , Range("a1"))
    Dim xlApp As Object
    Dim wbExcel As Object
    Dim wb2 As Workbook
    Set xlApp = CreateObject("Excel.Application")
    Set wbExcel = xlApp.Workbooks.Add
    With wbExcel
    .Title = "Demo"
    .SaveAs Filename:="C:\Documents and Settings\shress2\Desktop\Demo.xlsx"
    End With

    xlApp.Visible = True


    Set objMyList = Nothing
    Set objWksheet = Nothing
    End Sub

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Excel vba to copy sheet from current workbook to new Excel sheet ?

    Please use code tags (# icon from 'go advanced') when posting code.

    Please Login or Register  to view this content.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1