Mình có 1 macro khi chạy ở máy mình thì chạy được, nhưng khi copy sang máy khác thì lại không chạy được, dù đã bật macro về chế độ Low. Nhờ các anh chị giúp đỡ em với, làm sao để copy file có chứa macro sang máy khác vẫn chạy được.
Đoạn code macro của mình:
Public Sub TDNH()
Application.ScreenUpdating = False
On Error Resume Next
Dim Res(), Arr(), i As Long, j As Long, k As Long, Fullpath As String, FilePath As String
Fullpath = "'" & ThisWorkbook.Path & "\[TongHop.xls]THA'!"
Rows(1).End(2) = "=IFERROR(LOOKUP(2,1/(" & Fullpath & "A1:A65536<>""""),ROW(1:65536)),0)"
FilePath = "=" & Fullpath & "A10:EU" & Rows(1).End(2)
With Range("B9").Range("A10:EU" & Rows(1).End(2))
.FormulaArray = FilePath
Res = .Value
.ClearContents
End With
ReDim Preserve Arr(1 To UBound(Res), 1 To 12)
For i = 1 To UBound(Res)
If Res(i, 129) <> Empty Then
k = k + 1
Arr(k, 1) = k
For j = 10 To 13
Arr(k, j - 8) = Res(i, j)
Next
Arr(k, 6) = Res(i, 129)
Arr(k, 7) = Res(i, 2)
Arr(k, 8) = Res(i, 31)
Arr(k, 9) = Res(i, 40) + Res(i, 49) + Res(i, 59) + Res(i, 66)
Arr(k, 10) = Res(i, 91)
Arr(k, 12) = Res(i, 130)
If Res(i, 98) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B3")
If Res(i, 99) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B4")
If Res(i, 100) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B5")
If Res(i, 101) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B6")
If Res(i, 102) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B7")
If Res(i, 103) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B8")
If Res(i, 105) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B9")
If Res(i, 89) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B10")
If Res(i, 89) = 2 Then Arr(k, 11) = Range("Nguyen_nhan!B11")
If Res(i, 106) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B12")
End If
Next
If k Then Range("A10").Resize(k, 12).Value = Arr
Rows(1).End(2).Clear
Application.ScreenUpdating = True
Range("B10:L60000").Select
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Clear
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Add Key:=Range( _
"E10:E60000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Add Key:=Range( _
"D1060000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Danhsach").Sort
.SetRange Range("B10:L60000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Đoạn code macro của mình:
Public Sub TDNH()
Application.ScreenUpdating = False
On Error Resume Next
Dim Res(), Arr(), i As Long, j As Long, k As Long, Fullpath As String, FilePath As String
Fullpath = "'" & ThisWorkbook.Path & "\[TongHop.xls]THA'!"
Rows(1).End(2) = "=IFERROR(LOOKUP(2,1/(" & Fullpath & "A1:A65536<>""""),ROW(1:65536)),0)"
FilePath = "=" & Fullpath & "A10:EU" & Rows(1).End(2)
With Range("B9").Range("A10:EU" & Rows(1).End(2))
.FormulaArray = FilePath
Res = .Value
.ClearContents
End With
ReDim Preserve Arr(1 To UBound(Res), 1 To 12)
For i = 1 To UBound(Res)
If Res(i, 129) <> Empty Then
k = k + 1
Arr(k, 1) = k
For j = 10 To 13
Arr(k, j - 8) = Res(i, j)
Next
Arr(k, 6) = Res(i, 129)
Arr(k, 7) = Res(i, 2)
Arr(k, 8) = Res(i, 31)
Arr(k, 9) = Res(i, 40) + Res(i, 49) + Res(i, 59) + Res(i, 66)
Arr(k, 10) = Res(i, 91)
Arr(k, 12) = Res(i, 130)
If Res(i, 98) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B3")
If Res(i, 99) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B4")
If Res(i, 100) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B5")
If Res(i, 101) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B6")
If Res(i, 102) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B7")
If Res(i, 103) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B8")
If Res(i, 105) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B9")
If Res(i, 89) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B10")
If Res(i, 89) = 2 Then Arr(k, 11) = Range("Nguyen_nhan!B11")
If Res(i, 106) = 1 Then Arr(k, 11) = Range("Nguyen_nhan!B12")
End If
Next
If k Then Range("A10").Resize(k, 12).Value = Arr
Rows(1).End(2).Clear
Application.ScreenUpdating = True
Range("B10:L60000").Select
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Clear
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Add Key:=Range( _
"E10:E60000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Danhsach").Sort.SortFie lds.Add Key:=Range( _
"D1060000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Danhsach").Sort
.SetRange Range("B10:L60000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub