Sub PullDatafomClosedWB_V6()
'
' This macro will get the first alphabetical sheet name from a closed xlsx workbook and then get data from that sheet name.
' It works with numbers, spaces and ' Workbook remains closed the entire time.
'
' Apostrophe in file name is now handled :)
'
'
' Turn Settings off
Application.ScreenUpdating = False ' Turn Screen Updating off
Application.Calculation = xlCalculationManual ' Turn AutoCalculation off
Application.EnableEvents = False ' Turn EnableEvents off
'
Dim MyCell As Range
Dim DestinationSheetName As String, SourceDirectory As String, SourcefileName As String, SourceSheetName As String
Dim DestinationRow As Long
Dim conexion As Object, objCat As Object
'
DestinationSheetName = "Product" ' <--- Set this to the Destination Sheet Name
DestinationRow = 1 ' <--- Set this to the top row for the results
SourceDirectory = ActiveWorkbook.Path & "\New\" ' <--- Set this to the folder name that contains the source files
SourcefileName = Dir(SourceDirectory & "*.xlsx") ' Save source file name
'
Set conexion = CreateObject("adodb.connection")
Set objCat = CreateObject("ADOX.Catalog")
'
Do While SourcefileName <> ""
DestinationRow = DestinationRow + 1
'
conexion.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & SourceDirectory & SourcefileName & "; Extended Properties=""Excel 12.0; HDR=YES"";"
'
Set objCat.ActiveConnection = conexion
SourceSheetName = Replace(objCat.Tables(0).Name, "$", "")
SourceSheetName = Replace(SourceSheetName, "'", "")
SourceSheetName = Replace(SourceSheetName, "-", "")
conexion.Close
'
SourcefileName = Replace(SourcefileName, "'", "''") ' replace any apostrophe in file name with a double apostrophe
SourcefileName = Replace(SourcefileName, "-", "''")
'
Set MyCell = ThisWorkbook.Sheets(DestinationSheetName).Range("A" & DestinationRow)
MyCell.Offset(, 0).Resize(1, 22).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G25:G46" & ")"
MyCell.Offset(, 22).Resize(1, 13).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G49:G61" & ")"
MyCell.Offset(, 35).Resize(1, 7).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G64:G70" & ")"
MyCell.Offset(, 42).Resize(1, 21).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G73:G93" & ")"
MyCell.Offset(, 63).Resize(1, 6).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G96:G101" & ")"
MyCell.Offset(, 69).Resize(1, 4).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G104:G107" & ")"
MyCell.Offset(, 73).Resize(1, 30).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G110:G139" & ")"
MyCell.Offset(, 103).Resize(1, 5).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G142:G146" & ")"
MyCell.Offset(, 108).Resize(1, 9).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G149:G157" & ")"
MyCell.Offset(, 117).Resize(1, 4).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G160:G163" & ")"
MyCell.Offset(, 121).Resize(1, 17).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G166:G182" & ")"
MyCell.Offset(, 138).Resize(1, 4).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G185:G188" & ")"
MyCell.Offset(, 142).Resize(1, 14).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G191:G204" & ")"
MyCell.Offset(, 156).Resize(1, 2).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G207:G208" & ")"
MyCell.Offset(, 158).Resize(1, 2).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G211:G212" & ")"
MyCell.Offset(, 160).Resize(1, 11).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G215:G225" & ")"
MyCell.Offset(, 171).Resize(1, 14).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G228:G241" & ")"
MyCell.Offset(, 185).Resize(1, 2).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G244:G245" & ")"
MyCell.Offset(, 187).Resize(1, 9).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G248:G256" & ")"
MyCell.Offset(, 196).Resize(1, 36).FormulaArray = "=Transpose('" & SourceDirectory & "[" & SourcefileName & "]" & SourceSheetName & "'!G259:G294" & ")"
'
SourcefileName = Dir
Loop
'
With ThisWorkbook.Sheets(DestinationSheetName).UsedRange
.Value = .Value ' Remove formulas, leaving just the values
End With
'
Set objCat = Nothing
Set conexion = Nothing
'
' Turn Settings back on
Application.EnableEvents = True ' Turn EnableEvents back on
Application.Calculation = xlCalculationAutomatic ' Turn AutoCalculation back on
Application.ScreenUpdating = True ' Turn Screen Updating back on
End Sub