Mình muốn lấy dữ liệu vùng B19:I785 vào file MAIN.xlsm
Trong file MAIN.xlsm, tại sheet "Main" nhấn nút "Get DATA" sẽ brown file tới Foder đích để chọn tầm 22 file có cấu trúc giống hệt nhau. Sau đó copy dữ liệu từ 22 file này tới sheet DATA trong file MAIN.xlsm (sheet DATA chưa có sẽ tạo hoặc có rồi sẽ delete tạo lại)
Đây là code mình sưu tầm được:
Code này nó báo lỗi tại dòng BaseWks.Columns.AutoFit => mình đã bôi đỏ
Nhờ các bạn giúp sửa code với ạ, đính kèm file để các bạn dễ hình dung.
Cảm ơn nhiều ạ./.
Trong file MAIN.xlsm, tại sheet "Main" nhấn nút "Get DATA" sẽ brown file tới Foder đích để chọn tầm 22 file có cấu trúc giống hệt nhau. Sau đó copy dữ liệu từ 22 file này tới sheet DATA trong file MAIN.xlsm (sheet DATA chưa có sẽ tạo hoặc có rồi sẽ delete tạo lại)
Đây là code mình sưu tầm được:
Mã:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub
Sub MergeSpecificWorkbooks()
Dim MyPath As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant
' Set application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
SaveDriveDir = CurDir
' Change this to the path\folder location of the files.
ChDirNet "C:\Users\alone"
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True)
If IsArray(FName) Then
' Delete the summary sheet if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("DATA").Delete
On Error GoTo 0
Application.DisplayAlerts = True
' Add a new summary worksheet.
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "DATA"
' Loop through all files in the myFiles array.
For FNum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(FName(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("B19:I785")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If the source range uses all columns then
' skip this file.
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "There are not enough rows in the target worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
' Copy the file name in column A.
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = FName(FNum)
End With
' Set the destination range.
Set destrange = BaseWks.Range("B" & rnum)
' Copy the values from the source range
' to the destination range.
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir
End Sub
Code này nó báo lỗi tại dòng BaseWks.Columns.AutoFit => mình đã bôi đỏ
Nhờ các bạn giúp sửa code với ạ, đính kèm file để các bạn dễ hình dung.
Cảm ơn nhiều ạ./.