copy data from closed workbooks

FROGGER24

Well-known Member
Joined
May 22, 2004
Messages
704
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
I have a folder located at K:\abc\def\ghi\vins reports. Within that folder I have a workbook called Get data macro, using sheet1 starting in cell A2:F, I need to loop through all of the workbooks within this folder ,getting the cell values from the worksheet called VAR,cells F4,A6,D6,C13,C20,C32. This folder will contain several workbooks so the workbook name will be changing, but the sheet name will always be VAR. Thanks for any help
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
seeblue,

I have used the following macro on many occasions (adjusted per your request).


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel


Code:
Option Explicit
Sub GetMyData()
Dim myDir As String, fn As String, sn As String, NR As Long
myDir = "K:\abc\def\ghi\vins reports"
sn = "VAR"
fn = Dir(myDir & "\*.xls")
NR = 1
Do While fn <> ""
  If fn <> ThisWorkbook.Name Then
    With ThisWorkbook.Sheets("Sheet1")
      NR = NR + 1
      
      'worksheet called VAR,cells F4,A6,D6,C13,C20,C32.
      
      With .Range("A" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!F4"
        .Value = .Value
      End With
      With .Range("B" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A6"
        .Value = .Value
      End With
      With .Range("C" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!D6"
        .Value = .Value
      End With
      With .Range("D" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C13"
        .Value = .Value
      End With
      With .Range("E" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C20"
        .Value = .Value
      End With
      With .Range("F" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C32"
        .Value = .Value
      End With
    End With
  End If
  fn = Dir
Loop
End Sub


Then run the "GetMyData" macro.


If the above does not work correctly, then could you please supply a screenshot of one of the "VAR" sheets.

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4 (contains graphic instructions).
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php
 
Last edited:
Upvote 0
Hi
Save the workbook with these macro codes inside the reports folder and run the macro
Code:
Sub Cblu()
Dim z  As Long, e As Long
Dim f As String, b As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Cells(1, 1) = "=cell(""filename"")"
Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
Cells(3, 1).Select
f = Dir(Cells(1, 2) & "*.xls")
    Do While Len(f) > 0
    ActiveCell.Formula = f
    ActiveCell.Offset(1, 0).Select
    f = Dir()
    Loop
z = Cells(Rows.Count, 1).End(xlUp).Row
    For e = 3 To z
        If Cells(e, 1) <> ActiveWorkbook.Name Then
            For a = 1 To 6
            b = Choose(a, "F4", "A6", "D6", "C13", "C20", "C32")
              Cells(2, a + 1) = b
              Cells(1, 3) = "='" & Cells(1, 2) & "[" & Cells(e, 1) & "]Sheet1'!" & b
            Cells(e, a + 1) = Cells(1, 3)
            Next a
        End If
    Next e
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "collating is complete."
End Sub
Ravi
 
Upvote 0
Thank you both for your replies. I have since learned that not all of the workbooks have a worksheet called "VAR". Would it be possible to modify the code to... if worksheet "Var" not found, look for worksheet "Load Inspection"getting values from cells E8,E9,G6,E26,E80 and placing those values in cells A-E
 
Upvote 0
Greetings seeblue,

Given that we are now testing for the existance of one of two sheets, I think it would be easier to open the workbooks, test, get appropriate data, and close. In a throwaway copy of your wb, try:

In a Standard Module:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>    <br><SPAN style="color:#00007F">Sub</SPAN> exa()<br><SPAN style="color:#00007F">Dim</SPAN> FSO <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'<--- FileSystemObject</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> FOL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'<--- Folder</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> FIL <SPAN style="color:#00007F">As</SPAN> Object <SPAN style="color:#007F00">'<--- File</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> wb <SPAN style="color:#00007F">As</SPAN> Workbook<br><SPAN style="color:#00007F">Dim</SPAN> lOpenRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> aryVals<br>    <br>    <SPAN style="color:#007F00">'// Set a reference and get the folder as an object, to access the files    //</SPAN><br>    <SPAN style="color:#007F00">'// collection therein                                                      //</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> FSO = CreateObject("Scripting.FileSystemObject")<br>    <SPAN style="color:#00007F">Set</SPAN> FOL = FSO.GetFolder(ThisWorkbook.Path & "\")<br>    <br>    <SPAN style="color:#007F00">'// Kill updating to speed up code and reduce user's awareness of opening/  //</SPAN><br>    <SPAN style="color:#007F00">'// closing wb's                                                            //</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> FIL <SPAN style="color:#00007F">In</SPAN> FOL.Files<br>        <SPAN style="color:#007F00">'// Ensure ea file is a wb (but not ThisWorkbook) before opening        //</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> FIL.Type = "Microsoft Excel Worksheet" _<br>        And <SPAN style="color:#00007F">Not</SPAN> FIL.Name = ThisWorkbook.Name <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> wb = Workbooks.Open(FIL.Path)<br>                <SPAN style="color:#007F00">'// See if the sheet exists//</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> SheetExists(wb, "Var") <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#007F00">'// if yes, size accordingly//</SPAN><br>                    <SPAN style="color:#00007F">ReDim</SPAN> aryVals(1 <SPAN style="color:#00007F">To</SPAN> 1, 1 <SPAN style="color:#00007F">To</SPAN> 6)<br>                    <SPAN style="color:#00007F">With</SPAN> wb.Worksheets("Var")<br>                        <SPAN style="color:#007F00">'// fill the array with the vals//</SPAN><br>                        aryVals(1, 1) = .Range("F4").Value<br>                        aryVals(1, 2) = .Range("A6").Value<br>                        aryVals(1, 3) = .Range("D6").Value<br>                        aryVals(1, 4) = .Range("C13").Value<br>                        aryVals(1, 5) = .Range("C20").Value<br>                        aryVals(1, 6) = .Range("C32").Value<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                    <SPAN style="color:#007F00">'// See where the next open row is in this wb sheet1...//</SPAN><br>                    lOpenRow = NextRow_Ret(ThisWorkbook.Worksheets("Sheet1"), "A:F")<br>                    <SPAN style="color:#007F00">'// ...and kaplunk the vals in//</SPAN><br>                    ThisWorkbook.Worksheets("Sheet1") _<br>                        .Range("A" & lOpenRow & ":F" & lOpenRow).Value = aryVals<br>                <SPAN style="color:#00007F">ElseIf</SPAN> SheetExists(wb, "Load Inspection") <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">ReDim</SPAN> aryVals(1 <SPAN style="color:#00007F">To</SPAN> 1, 1 <SPAN style="color:#00007F">To</SPAN> 5)<br>                    <SPAN style="color:#00007F">With</SPAN> wb.Worksheets("Load Inspection")<br>                        aryVals(1, 1) = .Range("E8").Value<br>                        aryVals(1, 2) = .Range("E9").Value<br>                        aryVals(1, 3) = .Range("G6").Value<br>                        aryVals(1, 4) = .Range("E26").Value<br>                        aryVals(1, 5) = .Range("E80").Value<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                    lOpenRow = NextRow_Ret(ThisWorkbook.Worksheets("Sheet1"), "A:F")<br>                    <br>                    ThisWorkbook.Worksheets("Sheet1") _<br>                        .Range("A" & lOpenRow & ":E" & lOpenRow).Value = aryVals<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    <SPAN style="color:#007F00">'// if neither sheet exists, tell user//</SPAN><br>                    MsgBox "Neither sheet was found in:" & vbCrLf & _<br>                           FIL.Name & vbCrLf & vbCrLf & _<br>                           "The workbook will now close", 0, ""<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            wb.Close <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br><SPAN style="color:#00007F">Function</SPAN> NextRow_Ret(wks <SPAN style="color:#00007F">As</SPAN> Worksheet, ColStr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> rngSearch <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> rngFound <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> rngSearch = wks.Range(ColStr)<br>            <br>    <SPAN style="color:#00007F">Set</SPAN> rngFound = rngSearch.Find(What:="*", _<br>                                  After:=rngSearch(1, 1), _<br>                                  LookIn:=xlValues, _<br>                                  LookAt:=xlPart, _<br>                                  SearchOrder:=xlByRows, _<br>                                  searchdirection:=xlPrevious)<br>    <SPAN style="color:#00007F">If</SPAN> rngFound <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        NextRow_Ret = 2<br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">If</SPAN> rngFound.Row = 1 <SPAN style="color:#00007F">Then</SPAN><br>        NextRow_Ret = 2<br>    <SPAN style="color:#00007F">Else</SPAN><br>        NextRow_Ret = rngFound.Row + 1<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>    <br><SPAN style="color:#00007F">Function</SPAN> SheetExists(wb <SPAN style="color:#00007F">As</SPAN> Workbook, ShName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> wks <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wks = wb.Worksheets(ShName)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> wks <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> SheetExists = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>

I believe I followed which cells' vals you wanted where, but if anything is out of order, you should be able to adjust what cells' vals are placed in which elements.

Hope that helps,

Mark
 
Upvote 0
seeblue,

I am assuming that there is always data in the following cells, for the respective worksheets:

worksheet called VAR,cells F4,A6,D6,C13,C20,C32

and,

worksheet "Load Inspection"getting values from cells E8,E9,G6,E26,E80




The new macro code has been tested in my environment with two workbooks:
seeblueLI.xls
seeblueVAR.xls


Before the updated macro:


Excel Workbook
ABCDEF
1
2
3
4
Sheet1



After the updated macro:


Excel Workbook
ABCDEF
1
2LI E8LI E9LI G6LI E26LI E80
3VAR F4VAR A6VAR D6VAR C13VAR C20VAR C32
4
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetMyData()

Dim myDir As String, fn As String, sn As String, sn2 As String, NR As Long

myDir = "K:\abc\def\ghi\vins reports"

sn = "VAR"
sn2 = "Load Inspection"
fn = Dir(myDir & "\*.xls")
NR = 1
Do While fn <> ""
  If fn <> ThisWorkbook.Name Then
    With ThisWorkbook.Sheets("Sheet1")
      NR = NR + 1
      
      With .Range("A" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!F4"
        .Value = .Value
      End With
      
      If .Range("A" & NR) = 0 Then GoTo ErrorVar
      
      With .Range("B" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!A6"
        .Value = .Value
      End With
      With .Range("C" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!D6"
        .Value = .Value
      End With
      With .Range("D" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C13"
        .Value = .Value
      End With
      With .Range("E" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C20"
        .Value = .Value
      End With
      With .Range("F" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn & "'!C32"
        .Value = .Value
      End With

      GoTo ErrorVarContinue

ErrorVar:
      
      With .Range("A" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn2 & "'!E8"
        .Value = .Value
      End With
      With .Range("B" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn2 & "'!E9"
        .Value = .Value
      End With
      With .Range("C" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn2 & "'!G6"
        .Value = .Value
      End With
      With .Range("D" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn2 & "'!E26"
        .Value = .Value
      End With
      With .Range("E" & NR)
        .Formula = "='" & myDir & "\[" & fn & "]" & sn2 & "'!E80"
        .Value = .Value
      End With

ErrorVarContinue:

    End With
  End If
  fn = Dir
Loop
End Sub


Then run the updated "GetMyData" macro.
 
Upvote 0
seeblue,

If would help to see your data.

Please supply a screenshot of one of the "VAR", and, one of the "Load Inspection" sheets.

For sheet "Load Inspection" only display cells E8,E9,G6,E26.

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4 (contains graphic instructions).
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top