Consolidate data from different files into one master file not working

Estrella

New Member
Joined
Aug 3, 2012
Messages
17
Hello

I have a changing number of workbooks under a directory and I would like to extract some data from their first sheet called "offer". The data is always going to be from the same cells inside each workbook B12 B4 B15 B14 and B9

In my master sheet, line 1 will be the headers.
Column A should contain the name of each file extracted, and in column B the information extracted from cell B12, in column C the information extracted from B4 etc etc.

I don't know if it matters but every workbook inside the directory is protected and needs a password at opening ( always the same )

I have tried various codes but none are working. Any help would be a life saver!

Many thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Ron,

Your code Merge Excel workbooks on a Mac is almost working for me, except that I cannot set the range successfully.

Instead of
Set SourceRange = .Range("A1:C1")

I need b4 b12 b14 b15, ( so B4 to go in cell C / B12 in cell D etc ) but no matter how I try to write this in the code the macro is only picking up the first cell B4.
Do you know how to write it properly?

Las thing but maybe less easy, each file is protected by a password ( always the same ) the macro stops at every file to ask for the password. Can we include a code line to get around this?

Many thanks :)
 
Upvote 0
Upvote 0
I will post a example today, have you add the password code to the workbook open code ? (see the link that I posted)
 
Upvote 0
OK, try this one

You only must change the password in the workbooks.open line before you test it


You can edit the cells in this part

With Mybook.Worksheets(1)
BaseWks.Range("B" & rnum).Value = .Range("B4").Value
BaseWks.Range("C" & rnum).Value = .Range("B12").Value
BaseWks.Range("D" & rnum).Value = .Range("B14").Value
BaseWks.Range("E" & rnum).Value = .Range("B15").Value
BaseWks.Range("F" & rnum).Value = .Range("B6").Value
End With


Here is the complete code

Option Explicit
Public MyFiles As String


Sub MacMergeCode()
Dim BaseWks As Worksheet
Dim rnum As Long
Dim CalcMode As Long
Dim MySplit As Variant
Dim FileInMyFiles As Long
Dim Mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long

'Add a new workbook that has one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Range("A1").Font.Size = 36
BaseWks.Range("A1").Value = "Please Wait"
rnum = 3

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Clear MyFiles so that it does not return the old data if no files are found.
MyFiles = ""

'Get the files, set the level of folders and extension.
'Here you are looking in a single main-level folder for all .xls, .xlsx, .xlsm,
'and .xlsb files.
Call GetWorkbooksOnMacWithOrWithoutSubfolders(Level:=1, ExtChoice:=5)

'Work with the files if MyFiles is not empty
If MyFiles <> "" Then

MySplit = Split(MyFiles, Chr(10))
For FileInMyFiles = LBound(MySplit) To UBound(MySplit)

Set Mybook = Nothing
On Error Resume Next
Set Mybook = Workbooks.Open(Filename:=MySplit(FileInMyFiles), _
Password:="your password", WriteResPassword:=" your password ", UpdateLinks:=0)
On Error GoTo 0

If Not Mybook Is Nothing Then

On Error Resume Next
BaseWks.Range("A" & rnum) = MySplit(FileInMyFiles)

With Mybook.Worksheets(1)
BaseWks.Range("B" & rnum).Value = .Range("B4").Value
BaseWks.Range("C" & rnum).Value = .Range("B12").Value
BaseWks.Range("D" & rnum).Value = .Range("B14").Value
BaseWks.Range("E" & rnum).Value = .Range("B15").Value
BaseWks.Range("F" & rnum).Value = .Range("B6").Value
End With

rnum = rnum + 1
Mybook.Close savechanges:=False
End If

Next FileInMyFiles
BaseWks.Columns.AutoFit
End If

ExitTheSub:
BaseWks.Range("A1").Value = "Ready"
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub

Function GetWorkbooksOnMacWithOrWithoutSubfolders(Level As Long, ExtChoice As Long)
'Ron de Bruin : 19 April 2012
'Thanks to DJ Bazzie Wazzie (posted on MacScripter) for his great help.
Dim ScriptToRun As String
Dim folderPath As String

On Error Resume Next
folderPath = MacScript("choose folder as string")
If folderPath = "" Then Exit Function
On Error GoTo 0

folderPath = MacScript("tell text 1 thru -2 of " & Chr(34) & folderPath & _
Chr(34) & " to return quoted form of its POSIX Path")
Select Case ExtChoice
Case 1
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & _
Chr(34) & " | egrep -e '\\.xls$' " & Chr(34) & Chr(13)
Case 2
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & _
Chr(34) & " | egrep -e '\\.xlsx$' " & Chr(34) & Chr(13)
Case 3
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & _
Chr(34) & " | egrep -e '\\.xlsm$' " & Chr(34) & Chr(13)
Case 4
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & _
Chr(34) & " | egrep -e '\\.xlsb$' " & Chr(34) & Chr(13)
Case 5
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & Chr(34) & _
" | egrep -e '\\.xls$' -e '\\.xlsx$' -e '\\.xlsm$' -e '\\.xlsb$'" & _
Chr(34) & Chr(13)
Case Else
ScriptToRun = ScriptToRun & "set streamEditorCommand to " & Chr(34) & _
" | egrep -e '\\.xls$' -e '\\.xlsx$' -e '\\.xlsm$' -e '\\.xlsb$'" & _
Chr(34) & Chr(13)
End Select

ScriptToRun = ScriptToRun & _
"set streamEditorCommand to streamEditorCommand & " & _
Chr(34) & " | tr [/:] [:/] " & Chr(34) & Chr(13)
ScriptToRun = ScriptToRun & _
"set streamEditorCommand to streamEditorCommand & " & _
Chr(34) & " | sed -e " & Chr(34) & " & quoted form of (" & _
Chr(34) & " s.:." & Chr(34) & _
" & (POSIX file " & Chr(34) & "/" & Chr(34) & " as string) & " & _
Chr(34) & "." & Chr(34) & " )" & Chr(13)
ScriptToRun = ScriptToRun & "do shell script " & Chr(34) & "find " & _
folderPath & " \\! -name '.*' -maxdepth " & Level & Chr(34) & _
" & streamEditorCommand without altering line endings"

On Error Resume Next
MyFiles = MacScript(ScriptToRun)
On Error GoTo 0
End Function
 
Upvote 0
Hi, I'm new here.

I was wondering if there is any way to edit this code so that rather than pull in individual cells it can pull in specific columns, from only the 3 latest edited files though. I am attempting to pull in columns (H, E, D, C) after row 12. I am currently working on Mac Excel 2010, but eventually need this macro to work for excel 2007.

Thanks, any help would be appreciated.
 
Upvote 0
You could add up or report across similar spreadsheets without writing any code by using MergeXL, for $99.

Just a thought...
 
Upvote 0

Forum statistics

Threads
1,216,083
Messages
6,128,718
Members
449,465
Latest member
TAKLAM

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