Share via

Copying data without opening another excel workbook

Anonymous
2010-07-13T06:51:03+00:00

So I found this nifty code online that transfers data from one excel workbook to another without even opening the other excel document.

It is as follows:

Sub PullInSheet1()

'''''''''''''''''''''''''''''''

'Written By OzGrid Business Applications

'www.ozgrid.com

'Pulls in all data from sheet1 of a closed workbook.

''''''''''''''''''''''''''''''''

Dim AreaAddress As String

'Clear sheet ready for new data

Sheet1.UsedRange.Clear

'Reference the UsedRange Address of Sheet1 _

in the closed Workbook.

    Sheet1.Cells(1, 1) = "= 'C:\Users\ben biggs\Desktop" _

        & "[test.xlsm]Sheet2'!RC"

    'Pass the area Address to a String

    'AreaAddress = Sheet1.Cells(1, 1)

    'With Sheet1.Range(AreaAddress)

            'If the cell in Sheet1 of the closed workbook is not _

            empty the pull in it's content, else put in an Error.

        '.FormulaR1C1 = "=IF('C:\Users\ben biggs\Desktop" _

            & "[test.xlsm]Sheet1'!RC="""",NA(),'C:\Users\ben biggs\Desktop" _

            & "[test.xlsm]Sheet1'!RC)"

        'Delete all Error cells

        'On Error Resume Next

        '.SpecialCells(xlCellTypeFormulas, xlErrors).Clear

        'On Error GoTo 0

        'Change all formulas to Values only

        '.Value = .Value

    'End With

I adapted it to work in a two test workbooks and it worked great.  Then I tried to put it into use with my actual workbooks.  The only difference being that in my actual workbooks, I have some security settings as well as named sheets.

The error seems to be involved when try to use a named sheet instead of the default sheet1 or sheet2.

For example in my first line of code I get an error.

BEBOM.Cells(22, 2) = "= 'C:\Users\ben biggs\Desktop\BOMServerFolder[wwtconsumptionentry.xlsm]cellsend'!RC"

Once again the only difference is "BEBOM" is a named worksheet, cellsend is a named worksheet, but I get the error that the "object variable or with block variable not set."

Please help.

Secondly, when it does work, because I know it will and I've tested it with the test workbooks.  The password box pops up for the workbook.  Is there a code in vba to automatically type the password?

Thanks,

Ben

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

12 answers

Sort by: Most helpful
  1. Anonymous
    2017-07-30T04:54:44+00:00

    Hi

    I have a few  200 excel files which are all of the same format(i.e. 1 worksheets per Excel file which name is "sheet1". The corresponding worksheets from each Excel file are named exactly the same as are the column headers.  i want to copy  them on one sheet. Finally, after searching, I Write following codes:

    Entrance Data For Running Code is:

    1-Directory File

    2-Name Sheets (Name Sheet Should be the same in All excel Files)

    3-Number Of Row

    4-Start Number Of Row

    5-Number Of column

    6-Start Number Of Column

    Sub ReadDataFromAllWorkbooksInFolder()

    Dim FolderName As String, wbName As String, cValue, t As Variant

    Dim wbList() As String, wbCount As Integer, i, j, r, s, k, l, p As Integer

    Dim q As String

    t = Application.InputBox("input Directory Files ")              ' input Directory Excel Files

        FolderName = t

          m = Application.InputBox("input Sheet Name Of Files ")    ' input Sheet Name

          k = InputBox("input number of row ")                      ' input number of row

          l = InputBox("input Start number of row")                 '  input Start number of row

         p = InputBox("input number of Column")                     ' input number of Column

         s = InputBox("input Start number of Column")               '  input Start number of Column

        ' create list of workbooks in foldername' --- Comment

        wbCount = 0

        wbName = Dir(FolderName & "" & "*.xls")

        While wbName <> ""

            wbCount = wbCount + 1

            ReDim Preserve wbList(1 To wbCount)

            wbList(wbCount) = wbName

            wbName = Dir

        Wend

        If wbCount = 0 Then Exit Sub

        ' get values from each workbook' --- Comment

        For i = 0 To wbCount - 1

             For r = 1 To k

              For j = 1 To p

                    q = Cells(r + l - 1, j + s - 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)

                     cValue = GetInfoFromClosedFile(FolderName, wbList(i + 1), (m), (q))

                     Cells(r + i * k + 1, 1).Formula = wbList(i + 1)

                     Cells(r + i * k + 1, j + 1).Formula = cValue

            Next j

             Next r

              Next i

      End Sub

    Private Function GetInfoFromClosedFile(ByVal wbPath As String, _

        wbName As String, wsName As String, cellRef As String) As Variant

    Dim arg As String

        GetInfoFromClosedFile = ""

        If Right(wbPath, 1) <> "" Then wbPath = wbPath & ""

        If Dir(wbPath & "" & wbName) = "" Then Exit Function

        arg = "'" & wbPath & "[" & wbName & "]" & _

            wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)

        On Error Resume Next

        GetInfoFromClosedFile = ExecuteExcel4Macro(arg)

    End Function


    Best Regards

    Mr. Qasem Abbasi

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2010-07-13T07:49:54+00:00

    Now this part of the code is giving me fits

    .FormulaR1C1 = "=IF('C:\Users\ben biggs\Desktop" _

                & "[test.xlsm]Sheet1'!RC="""",NA(),'C:\Users\ben biggs\Desktop" _

                & "[test.xlsm]Sheet1'!RC)"

    My data in the first workbook is in row 22, column 2.  My data in the 2nd workbook is in range of i5 to i11, so I changed the code to:

    FormulaR22C2 = "=IF('C:\Users\ben biggs\Desktop\BOMServerFolder" _

    & "[wwtconsumptionentry.xlsm]sheet1'!R5C9="""",NA(),'C:\Users\ben biggs\Desktop\BOMServerFolder" _

    & "[wwtconsumptionentry.xlsm]sheet1'!R5C5)"

    and it exploded, j/k, I get the error "object doesn't support this property or method"

    0 comments No comments
  3. Anonymous
    2010-07-13T07:19:03+00:00

    There is unprotect command right?  But I don't know how to put that in the middle of that line of code to make it function.

    0 comments No comments
  4. Anonymous
    2010-07-13T07:12:17+00:00

    Hi Bill thanks for the info,

    I actually just got out of my newbie mindset and realized that for vba, the sheets remain named sheet1,sheet2 etc..  So i just referred to them as that and got the first line of code to work.  I also had to the RC to R1C1 because I guess the way the phrase is written it used the RC from earlier in the code (22,2).  

    So solved two problems.

    Now, how would I go about setting up the password thing.  The big problem is that the main workbook is accessing several smaller workbooks across the server that certain people are required to put information into.  The main workbook accesses all of them and places the values into the specified areas.  It would just be inefficient to have the boss man open all of the workbooks but at the same time - the information is highly confidential and I need the password protection.

    Thanks,

    Ben

    0 comments No comments
  5. Anonymous
    2010-07-13T07:00:36+00:00

    Worksheets("BEBOM").Cells rather than BEBOM.Cells

    >Is there a code in vba to automatically type the password?

    You could try using SendKeys.  Otherwise no.

    Is it a big problem to open the workbook? 

    In that case you can specify the password in the Workbooks.Open statement and you can copy the data directly.


    Bill Manville. Excel MVP, Oxford, England. www.manville.org.uk

    0 comments No comments