O código que estou usando é uma adaptação de um código original criado pelo Mauro Coutinho, não me lembro o post agora...
Mas aqui segue minha adaptação...até funciona com poucas linhas, mas no exemplo a seguir trava o Excel.
Penso que o problema pode ser o laço que elaborei...talvez exista uma maneira mais clean de se fazer isso.
Sub Premissas()
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim FolderName As String, wbName As String, cod_1 As Variant, cod_2 As Variant
Dim wbList As String, sValuePlan1 As String, linha As Single
FolderName = "G:GestãoWCDTestes_Imports"
wbName = Dir(FolderName & "" & "Backup de " & Year(Date) & "_" & "0" & Month(Date) & "_" & "0" & Day(Date) & "_" & "Status Premissas Multi" & ".xlk")
wbList = wbName
wbName = Dir
y = 3
For x = 4 To 2500
cod_1 = Worksheets("Plan1").Cells(y, "E")
cod_2 = GetInfoFromClosedFile(FolderName, wbList, "Status Premissas Multi", "D" & x)
If cod_2 = cod_1 Then
Worksheets("Plan1").Cells(y, "J") = Cells(x, "W")
y = y + 1
x = 3
End If
If x = 2500 Then
x = 3
y = y + 1
End If
If x = 2500 And y = 800 Then
MsgBox "Fim da verificação!"
Exit Sub
End If
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Private Function GetInfoFromClosedFile(ByVal wbPath As String, wbName As String, wsName As String, cellRef As String) As Variant
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim arg As String
GetInfoFromClosedFile = ""
If Right(wbPath, 1) <> "" Then wbPath = wbPath & ""
If Dir(wbPath & "" & wbName) = "" Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & _
wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
Application.EnableEvents = True
Application.ScreenUpdating = True
End Function
Postado : 07/08/2018 8:24 am