+ Reply to Thread
Results 1 to 4 of 4

Copy range of cells from excel file on SharePoint

Hybrid View

  1. #1
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    72

    Copy range of cells from excel file on SharePoint

    Background:
    I have a master Excel file that needs updating from various people in various locations. To facilitate the updates and to provide version control, I broke the master excel file into separate smaller files and uploaded them onto SharePoint.

    Desired Outcome:
    My end result would be to have a button/macro for each worksheet that would reach out to the corresponding file on SharePoint, Copy the relevant cell(s), and PasteSpecial(Values) into the master Excel File.

    Progress thus far:
    I am able to directly link my master file to the file on SharePoint to reflect the values:
    A1='SharePointAddress/[FileName.xlsb]Worksheet'!$A$1
    the issue with that is my master file updates automatically upon opening and is time consuming especially when there's only one worksheet (out of many) that needs updating. Can anyone provide guidance on the VBA needed to access the broken down files, copy a specific range and (preferably) Paste Special(values) into the master file?

  2. #2
    Registered User
    Join Date
    05-28-2012
    Location
    Hinckley
    MS-Off Ver
    Excel 2003 / 2010 / 2013
    Posts
    7

    Re: Copy range of cells from excel file on SharePoint

    Hi,

    Try this:-

    Function GetDataFrom(SourceFile As Variant, SourceSheet As String, SourceRange As String, TargetRange As Range, Header As Boolean, UseHeaderRow As Boolean)
    ' 30-Dec-2007, working in Excel 2000-2007
        Dim rsCon As Object
        Dim rsData As Object
        Dim szConnect As String
        Dim szSQL As String
        Dim lCount As Long
    
        ' Create the connection string.
        If Header = False Then
            If Val(Application.Version) < 12 Then
                szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 8.0;HDR=No"";"
            Else
                szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 12.0;HDR=No"";"
            End If
        Else
            If Val(Application.Version) < 12 Then
                szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 8.0;HDR=Yes"";"
            Else
                szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 12.0;HDR=Yes"";"
            End If
        End If
    
        If SourceSheet = "" Then
            ' workbook level name
            szSQL = "SELECT * FROM " & SourceRange$ & ";"
        Else
            ' worksheet level name or range
            szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
        End If
    
        On Error GoTo SomethingWrong
    
        Set rsCon = CreateObject("ADODB.Connection")
        Set rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open szConnect
        rsData.Open szSQL, rsCon, 0, 1, 1
    
        ' Check to make sure we received data and copy the data
        If Not rsData.EOF Then
    
            If Header = False Then
                TargetRange.Cells(1, 1).CopyFromRecordset rsData
            Else
                'Add the header cell in each column if the last argument is True
                If UseHeaderRow Then
                    For lCount = 0 To rsData.Fields.Count - 1
                        TargetRange.Cells(1, 1 + lCount).Value = _
                        rsData.Fields(lCount).Name
                    Next lCount
                    TargetRange.Cells(2, 1).CopyFromRecordset rsData
                Else
                    TargetRange.Cells(1, 1).CopyFromRecordset rsData
                End If
            End If
    
        Else
            MsgBox "No records returned from : " & SourceFile, vbCritical
        End If
    
        ' Clean up our Recordset object.
        rsData.Close
        Set rsData = Nothing
        rsCon.Close
        Set rsCon = Nothing
        Exit Function
    
    SomethingWrong:
        MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, _
               vbExclamation, "Error"
        On Error GoTo 0
    
    End Function
    Then for each set you need to grab enter the following:-

    'Clear Existing Data but not remove Columns to preserve formulas
        Sheets("Data").Select
        Cells.Select
        Selection.ClearContents
        
        '**************** Update Data ****************
        Sheets("Data").Select
        GetDataFrom "//sites.mysite.net/sites/" & "DataFile.xlsb", "sheetname", "A1:z500", Sheets("Data").Range("A1"), True, True
    That should work perfectly...
    Last edited by milenia; 06-13-2017 at 09:40 AM.

  3. #3
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: Copy range of cells from excel file on SharePoint

    Thanks for the suggestion. I copied the code and modified it as necessary to test an initial copy/paste. As it stands now, it hangs up and throws an error at the line:
    rsData.Open szSQL, rsCon, 0, 1, 1
    I've verified the SharePoint link and tested it via windows explorer and it opens that way. I've verified the worksheet names (still Sheet1, Sheet2, etc). Range now is copying cell A1 from the source file to cell A1 of the destination file. Any ideas why it might be hanging up at this point?

  4. #4
    Registered User
    Join Date
    03-15-2010
    Location
    CT
    MS-Off Ver
    Excel 2010
    Posts
    72

    Re: Copy range of cells from excel file on SharePoint

    Ok, So I found a much simpler solution that appears to work thus far. Have a bit more tweaking to do to get it expanded to my full requirements, but below is a sample that works in my test environment:

    Sub GetDataFromClosedWorkbook()
    Dim wb As Workbook
        Application.ScreenUpdating = False ' turn off the screen updating
        
        Set wb = Workbooks.Open("\\SharePointPath\Testing Source File.xlsx", True, True)
        
        Workbooks("Testing Destination File.xlsm").Worksheets("Sheet1").Range("A1:A2").Value = wb.Worksheets("Sheet1").Range("A1:A2").Value
        
        wb.Close False ' close the source workbook without saving any changes
        
        Set wb = Nothing ' free memory
        
        Application.ScreenUpdating = True ' turn on the screen updating
    End Sub
    Last edited by SUPPO_USN; 06-13-2017 at 01:49 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA to select SharePoint Content Type when saving Excel file to SharePoint
    By Luffk73 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2017, 05:22 PM
  2. Saving an excel file to sharepoint
    By ctguards1987 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-04-2017, 09:39 AM
  3. Select file and copy values from SharePoint excel file
    By peetman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-17-2014, 06:26 AM
  4. Replies: 0
    Last Post: 10-08-2013, 10:54 AM
  5. Macro to get the latest file (excel file) from sharepoint sites
    By vigneshwaran in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2012, 09:45 AM
  6. Outlook -->SharePoint--> Excel file, vrs Excel file --> SharePoint -->outlook
    By LiLi1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-07-2011, 09:56 PM
  7. Copy Data from Excel Files (from a Sharepoint) to Access MDB File
    By LordMarcus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2009, 05:59 AM

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