abdo meghari
Well-known Member
- Joined
- Aug 3, 2021
- Messages
- 651
- Office Version
- 2019
Hi guys,
I'm not sure why this problem occurs for me .
the code should clear from row2 for closed file "Bridgestone Stock Sales report(12)" before import data from open file , but will also clear headers for closed file !!
any help to fix this problem,please?
I'm not sure why this problem occurs for me .
the code should clear from row2 for closed file "Bridgestone Stock Sales report(12)" before import data from open file , but will also clear headers for closed file !!
VBA Code:
Sub OpenFilesFromFolder1()
Dim ExtBk As Workbook
Dim IntBk As Workbook
Dim FolderPath As String
Dim FilePath As String
Dim lRow As Long
Dim Rng1 As Range, Rng2 As Range
Set IntBk = ActiveWorkbook
lRow = IntBk.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
FolderPath = "C:\Users\MY-NAME\DESKTOP\BRIDGESTONE REPORT\"
FilePath = Dir(FolderPath & "Bridgestone Stock Sales report(12).xls")
If FilePath <> "" Then
Set ExtBk = Workbooks.Open(FolderPath & FilePath)
Else
MsgBox "File Bridgestone Stock Sales report(2).xlsm not found"
Exit Sub
End If
Application.ScreenUpdating = False
With ExtBk.Worksheets("REPORT")
.Range("A2:D" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
End With
ExtBk.Worksheets("REPORT").Range("A2:A" & lRow).Value = IntBk.Worksheets("Sheet1").Range("A2:A" & lRow).Value
ExtBk.Worksheets("REPORT").Range("D2:D" & lRow).Value = IntBk.Worksheets("Sheet1").Range("D2:D" & lRow).Value
Set Rng1 = IntBk.Worksheets("Sheet1").Range("B2:C" & lRow)
Set Rng2 = ExtBk.Worksheets("REPORT").Range("B2:C" & lRow)
Rng2.Value = Rng1.Value
Application.ScreenUpdating = True
Application.DisplayAlerts = False
ExtBk.Save
ExtBk.Close
Application.DisplayAlerts = True
End Sub