I'm writing a code to open a file and fill the files cells with the source file.
I'm encountering a problem that my second doesn't have its cells filled.
However, when the file is already open, it works perfectly.
I added a portion of code to make excel wait a few seconds before filling the second file but it does not work neither.
Have you an idea?
Code
Sub RemplirTableaux()
Dim FilePath$, TargetFilePath$
Const SheetName$ = "Sheet1"
FilePath = "file.xlsx"
TargetFilePath$ = "C:\file.xlsx"
If Not IsWorkBookOpen(TargetFilePath) Then
Workbooks.Open (TargetFilePath)
End If
Application.Wait DateAdd("s", 5, Now())
For i = 7 To 23
If Workbooks(FilePath).Sheets("ACs").Cells(i, "A") = Cells(1, "B") Then
Workbooks(FilePath).Sheets("ACs").Cells(i, "B").Value = Cells(27, "J")
Workbooks(FilePath).Sheets("ACs").Cells(i, "C").Value = Cells(27, "K")
'Another filling statements
End If
Next
End Sub
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Cellscalls refer to the active sheet (which is in the active workbook). Which sheet are you trying to copy from?