Accessing multiple excel workbooks/sheets

bucci35

Active Member
Joined
Jul 6, 2002
Messages
350
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have 15 workbooks that are updated by 15 stores. Weekly I pull them back to my desktop via remote access.
I need to access a few cells within each workbook/sheet and pull them to a blank workbook/sheet/cell.
Each week the names changes on all 15 by the weekending date and always preceded by the store number ex. 05_091606 with an underscore between the store number and the date.
I was wondering if their was a way to create a macro or something in a blank workbook that would access all of these automatically to make life easier?
Any ideas?

Thanks,
Dan
 
Code modified to use the Dir function which will return an empty string if the file does not exist:
Code:
Public Sub PullData()
    Dim wkb As Workbook
    Dim lngStore As Long
    Dim strDate As String
    Dim strName As String
    strDate = Format(Sheets(1).Range("A2").Value, "mmddyy")
    For lngStore = 1 To 15
        strName = "YourWorkBookPath\" & Format(lngStore, "00") & "_" & strDate & ".xls"
        ' Ensure file exists
        If Len(Trim$(Dir(strName))) > 0 Then
            Set wkb = Workbooks.Open(strName, ReadOnly:=True)
            MsgBox wkb.Name & " " & wkb.Sheets("Sheet1").Range("A1").Value
            wkb.Close False
        End If
    Next lngStore
    Set wkb = Nothing
End Sub 'PullData
Good Luck!
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Ok,

This seems to work. It goes the cycle and finds and opens them all, but it doesn't paste the values in my new workbook? any ideas

Thanks
 
Upvote 0
Also,

How can I specify the exact sheet and cell I want it to copy and paste to my master workbook. The sheet and cell will always be the same
 
Upvote 0
Extract your data similarly to the following. Use the workbook object (wkb) to get to the sheets that you want. In the example below, it is retrieving cell C3 from sheet one in the various store files:
Code:
Dim strData As String
strData = wkb.Sheets(1).Range("C3").Value
Then create a new workbook to collect the data. Let's assume that you will put it in the first sheet in Column A. So you will need to keep track of which row you are using also.
Code:
Dim wks As Worksheet
Set wks = Workbooks.Add.Sheets(1)
Below is the code so far:
Code:
Public Sub PullData() 
    Dim wkb As Workbook 
    Dim wksStoreData As Worksheet
    Dim lngStore As Long, lngRow As Long
    Dim strDate As String, strName As String, strData As String

    ' Create consolidated store data sheet
    Set wksStoreData = Workbooks.Add.Sheets(1)
    wksStoreData.Range("A1").Value = "Consolidated Store Data"

    lngRow = 2
    strDate = Format(Sheets(1).Range("C3").Value, "mmddyy") 
    For lngStore = 1 To 15 
        strName = "YourWorkBookPath\" & Format(lngStore, "00") & "_" & strDate & ".xls" 

        ' Ensure file exists 
        If Len(Trim$(Dir(strName))) > 0 Then 

            ' Open file and read cell C3
            Set wkb = Workbooks.Open(strName, ReadOnly:=True) 
            strData = wkb.Sheets("Sheet1").Range("C3").Value 
            wkb.Close False 

            ' Save store data to column A in consolidated store workbook
            wksStoreData.Range("A" & lngRow).Value = strData

            ' Increment the row counter in the consolidated store workbook
            lngRow = lngRow + 1
        End If 
    Next lngStore 
    Set wkb = Nothing 
End Sub 'PullData
The net effect of the above is that you will have a new workbook created which will have the data from individual store sheet1 C3 copied into new workbook sheet1 column A. Good Luck!
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,786
Members
449,125
Latest member
shreyash11

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