abdo meghari
Well-known Member
- Joined
- Aug 3, 2021
- Messages
- 651
- Office Version
- 2019
Hi experts
here are two codes to export data from open file to closed file and sheet to a different sheet .
my question , could make code short by combining as in one instead of Repeat the whole code twice for two closed file name and two different sheets names ?
so I have open file contains sheets TABLE 1& MISSED should export data to two closed files names Elmarghanie Brand & COMPARE REPORTS for sheets REPORT & OUTPUT .
I hope somebody could help for this .
thanks
here are two codes to export data from open file to closed file and sheet to a different sheet .
my question , could make code short by combining as in one instead of Repeat the whole code twice for two closed file name and two different sheets names ?
so I have open file contains sheets TABLE 1& MISSED should export data to two closed files names Elmarghanie Brand & COMPARE REPORTS for sheets REPORT & OUTPUT .
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("TABLE 1").Cells(Rows.Count, 1).End(xlUp).Row
FolderPath = "C:\Users\MY-NAME\Downloads\BRIDGESTONE REPORT\"
FilePath = Dir(FolderPath & "Elmarghanie Brand .xlsm")
If FilePath <> "" Then
Set ExtBk = Workbooks.Open(FolderPath & FilePath)
else 'Exit the sub if not found! Else errors will occur
msgbox "File Elmarghanie Brand .xlsm not found"
exit sub
End If
Application.ScreenUpdating = False
'clear any old values:
with ExtBk.Worksheets("TABLE 1")
.Range("A2:E" & .Cells(.Rows.Count, 1).End(xlUp).Row).Clearcontents
end with
' For i = 2 To lRow
'
' ExtBk.Worksheets("REPORT").Cells(i, 1).Value = IntBk.Worksheets("TABLE 1").Cells(i, 1).Value
'
' Next
'Why do the copy one row at a time? DO it all in once:
ExtBk.Worksheets("REPORT").Range("A2:A"& lRow).Value = IntBk.Worksheets("TABLE 1").Range("A2:A"& lRow).Value
Set Rng1 = IntBk.Worksheets("TABLE 1").Range("B2:E" & lRow)
Set Rng2 = ExtBk.Worksheets("REPORT").Range("C2:F" & lRow)
' Rng1.Copy
' Rng2.PasteSpecial xlPasteValues
'' Range copy / paste is slow. Just set the values:
rng2.Value = Rng1.Value
Application.ScreenUpdating = True
Application.DisplayAlerts = False
ExtBk.Save
ExtBk.Close
Application.DisplayAlerts = True
End Sub
VBA Code:
Sub OpenFilesFromFolder2()
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("MISSED").Cells(Rows.Count, 1).End(xlUp).Row
FolderPath = "C:\Users\MY-NAME\Downloads\BRIDGESTONE REPORT\"
FilePath = Dir(FolderPath & "COMPARE REPORTS.xlsm")
If FilePath <> "" Then
Set ExtBk = Workbooks.Open(FolderPath & FilePath)
else 'Exit the sub if not found! Else errors will occur
msgbox "File COMPARE REPORTS.xlsm not found"
exit sub
End If
Application.ScreenUpdating = False
'clear any old values:
with ExtBk.Worksheets("OUTPUT")
.Range("A2:E" & .Cells(.Rows.Count, 1).End(xlUp).Row).Clearcontents
end with
' For i = 2 To lRow
'
' ExtBk.Worksheets("OUTPUT").Cells(i, 1).Value = IntBk.Worksheets("MISSED").Cells(i, 1).Value
'
' Next
'Why do the copy one row at a time? DO it all in once:
ExtBk.Worksheets("OUTPUT").Range("A2:A"& lRow).Value = IntBk.Worksheets("MISSED").Range("A2:A"& lRow).Value
Set Rng1 = IntBk.Worksheets("MISSED").Range("B2:E" & lRow)
Set Rng2 = ExtBk.Worksheets("OUTPUT").Range("C2:F" & lRow)
' Rng1.Copy
' Rng2.PasteSpecial xlPasteValues
'' Range copy / paste is slow. Just set the values:
rng2.Value = Rng1.Value
Application.ScreenUpdating = True
Application.DisplayAlerts = False
ExtBk.Save
ExtBk.Close
Application.DisplayAlerts = True
End Sub
thanks