Hi all:
I want to copy data form multiple sheet to master that
Source: Multiple excel file, the same sheets name "G034141", Range("A19:L" & lastrow)
with lastrow = sheets("G034141").Range("A" & Rows.Count).Row - 1
Destinations: Thisworkbook.sheets("master")
This code can copy all data from multiple excel files but in fixed range("A19:L280")
Please help me change code or have another option to do this.
Thanks./.
I want to copy data form multiple sheet to master that
Source: Multiple excel file, the same sheets name "G034141", Range("A19:L" & lastrow)
with lastrow = sheets("G034141").Range("A" & Rows.Count).Row - 1
Destinations: Thisworkbook.sheets("master")
This code can copy all data from multiple excel files but in fixed range("A19:L280")
Please help me change code or have another option to do this.
Thanks./.
Code:
Public Sub DATA()Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim cn As Object, rs As Object, i As Byte, lr As Long, lrG03414 As Long, fso As Object
Set cn = CreateObject("adodb.connection")
Set fso = CreateObject("Scripting.FileSystemObject")
Sheets("master").Range("A1").CurrentRegion.Offset(1).ClearContents
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "G03414", "*.xl*"
.InitialFileName = "G03414*"
.AllowMultiSelect = True
.Show
For i = 1 To .SelectedItems.Count
cn.Open ("provider=Microsoft.ACE.OLEDB.12.0;data source=" & .SelectedItems(i) & ";mode=Read;extended properties=""Excel 12.0;hdr=no"";")
lr = .SelectedItems.Range("A" & Rows.Count).End(3).Row - 1
Set rs = cn.Execute("select '" & fso.GetBaseName(.SelectedItems(i)) & "',f1,f2,f3,f4,f5,f6,f7,f8,f9,val(f10),val(f11),val(f12) from [G034141$A18:L280] ")
lr = Sheets("master").Range("A" & Rows.Count).End(3).Row
If Not rs.EOF Then Sheets("master").Range("A" & lr + 1).CopyFromRecordset rs
rs.Close
cn.Close
Next
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub