huafu_Jackl
Thành viên mới

- Tham gia
- 23/7/25
- Bài viết
- 1
- Được thích
- 0
Chào các bác,
Em có viết phần code VBA để đưa dữ liệu từ nhiều file vào 1 file duy nhất để xử lý dữ liệu, tuy nhiên đến bước chọn file đưa vào thì tất cả các file đều không khả dụng.
Em viết cả 2 phần code để chạy trong cả Mac và Windows, trên Windows chạy bình thường nhưng trên Mac thì chịu trói.
Mong các cao nhân chỉ giáo ạ.

Xin cảm ơn!!
#If Mac Then
Set mydict = New Collection ' D¨´ng Collection cho Mac
#Else
Set mydict = CreateObject("Scripting.Dictionary") ' D¨´ng Dictionary cho Windows
#End If
Set wsTarget = ThisWorkbook.Sheets("NEW LIST")
LastRowTarget = 2
#If Mac Then
Filename = Application.GetOpenFilename("all,*.*", , "select file", , True)
'Filename = Application.GetOpenFilename( _
"Excel Files (*.xlsx),*.xlsx,Excel 97-2004 (*.xls),*.xls,Macro Files (*.xlsm),*.xlsm", _
MultiSelect:=True, _
Title:="Chon file Excel")
If Not IsArray(Filename) Then
Dim scriptResult As String
scriptResult = MacScript("choose file of type {""public.xlsx"",""com.microsoft.excel.xls""} with multiple selections allowed")
If scriptResult <> "" Then
Filename = Split(Replace(scriptResult, "Macintosh HD:", ""), ",")
For i = LBound(Filename) To UBound(Filename)
Filename(i) = Replace(Filename(i), ":", "/")
Next i
End If
End If
#Else
Filename = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
Title:="Select Excel Files", _
MultiSelect:=True)
#End If
If Not IsArray(Filename) Then Exit Sub
For j = UBound(Filename) To LBound(Filename) Step -1
Set wbSource = Workbooks.Open(Filename(j))
If VarType(Filename) = vbBoolean Then Exit Sub
For Each wsSource In wbSource.Sheets
LastRowSource = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
If LastRowSource > 1 Then
Set rngSource = wsSource.Range("A1:B" & LastRowSource)
For Each cell In rngSource.Columns(1).Cells
ValueToCheck = cell.Value
#If Mac Then
If Not CollectionKeyExists(mydict, CStr(ValueToCheck)) And ValueToCheck <> "" Then
mydict.Add Item:=True, key:=CStr(ValueToCheck)
wsTarget.Cells(LastRowTarget, 1).Resize(1, rngSource.Columns.Count).Value = _
rngSource.Rows(cell.Row - rngSource.Row + 1).Value
LastRowTarget = LastRowTarget + 1
End If
#Else
If Not mydict.exists(ValueToCheck) And ValueToCheck <> "" Then
mydict.Add ValueToCheck, True
wsTarget.Cells(LastRowTarget, 1).Resize(1, rngSource.Columns.Count).Value = _
rngSource.Rows(cell.Row - rngSource.Row + 1).Value
LastRowTarget = LastRowTarget + 1
End If
#End If
Next cell
End If
Next wsSource
wbSource.Close SaveChanges:=False
Next j
MsgBox "New list imported successfully!", vbInformation
Sheets("NEW LIST").Range("G5").Value = "V"
End Sub
Em có viết phần code VBA để đưa dữ liệu từ nhiều file vào 1 file duy nhất để xử lý dữ liệu, tuy nhiên đến bước chọn file đưa vào thì tất cả các file đều không khả dụng.
Em viết cả 2 phần code để chạy trong cả Mac và Windows, trên Windows chạy bình thường nhưng trên Mac thì chịu trói.
Mong các cao nhân chỉ giáo ạ.

Xin cảm ơn!!
#If Mac Then
Set mydict = New Collection ' D¨´ng Collection cho Mac
#Else
Set mydict = CreateObject("Scripting.Dictionary") ' D¨´ng Dictionary cho Windows
#End If
Set wsTarget = ThisWorkbook.Sheets("NEW LIST")
LastRowTarget = 2
#If Mac Then
Filename = Application.GetOpenFilename("all,*.*", , "select file", , True)
'Filename = Application.GetOpenFilename( _
"Excel Files (*.xlsx),*.xlsx,Excel 97-2004 (*.xls),*.xls,Macro Files (*.xlsm),*.xlsm", _
MultiSelect:=True, _
Title:="Chon file Excel")
If Not IsArray(Filename) Then
Dim scriptResult As String
scriptResult = MacScript("choose file of type {""public.xlsx"",""com.microsoft.excel.xls""} with multiple selections allowed")
If scriptResult <> "" Then
Filename = Split(Replace(scriptResult, "Macintosh HD:", ""), ",")
For i = LBound(Filename) To UBound(Filename)
Filename(i) = Replace(Filename(i), ":", "/")
Next i
End If
End If
#Else
Filename = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
Title:="Select Excel Files", _
MultiSelect:=True)
#End If
If Not IsArray(Filename) Then Exit Sub
For j = UBound(Filename) To LBound(Filename) Step -1
Set wbSource = Workbooks.Open(Filename(j))
If VarType(Filename) = vbBoolean Then Exit Sub
For Each wsSource In wbSource.Sheets
LastRowSource = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
If LastRowSource > 1 Then
Set rngSource = wsSource.Range("A1:B" & LastRowSource)
For Each cell In rngSource.Columns(1).Cells
ValueToCheck = cell.Value
#If Mac Then
If Not CollectionKeyExists(mydict, CStr(ValueToCheck)) And ValueToCheck <> "" Then
mydict.Add Item:=True, key:=CStr(ValueToCheck)
wsTarget.Cells(LastRowTarget, 1).Resize(1, rngSource.Columns.Count).Value = _
rngSource.Rows(cell.Row - rngSource.Row + 1).Value
LastRowTarget = LastRowTarget + 1
End If
#Else
If Not mydict.exists(ValueToCheck) And ValueToCheck <> "" Then
mydict.Add ValueToCheck, True
wsTarget.Cells(LastRowTarget, 1).Resize(1, rngSource.Columns.Count).Value = _
rngSource.Rows(cell.Row - rngSource.Row + 1).Value
LastRowTarget = LastRowTarget + 1
End If
#End If
Next cell
End If
Next wsSource
wbSource.Close SaveChanges:=False
Next j
MsgBox "New list imported successfully!", vbInformation
Sheets("NEW LIST").Range("G5").Value = "V"
End Sub