combine two codes as in one for two different sheets whether input or output

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 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 .




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
I hope somebody could help for this .
thanks
 
I just ran code with your sample data & got this result in a test export file.

Test1.xlsx
ABCDEF
21BS 1200R20G580JAP30
32BS 1200R20G580THI
43BS 1200R24G582JAP2
54BS 1200R20R187JAP0
65BS 1200R24G580JAP50
76BS 13R22.5R187JAP
87BS 1400R20R180JAP
98BS 1400R20R180BZJAP
109BS 1400R20VSJJAP22
1110BS 165R13CR624INDO4
1211BS 175/70R13EP150THI
1312BS 185/65R15B250JAP
1413BS 195/75R16C613VJAP
1514BS 195R14C613VJAP
1615BS 195R14CR623THI
1716BS 195R15C613VJAP4
1817BS 205/60R16T005THI0
1918BS 205/70R15CR623THI8
2019BS 205R16CD840THI
2120BS 215/50R17EP300THI
2221BS 215/55R17T005JAP
2322BS 215/60R16ER30JAP8
2423BS 215/65R15T005INDO4
2524BS 215/70R15CR624TR
REPORT
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
strange !!
I no know what my mistake !
seem to work , just question I have to repeat copying macro Sub Test() for the second sheet for the same closed file ?
I mean copy sheet=MISSED and export sheet =OUTPUT for Elmarghanie Brand closed file , because I mentioned in OP two copies sheets for open file & two exporting sheets for Elmarghanie Brand closed file
copy sheets=
TABLE 1& MISSED and export sheets =REPORT & OUPUT
 
Upvote 0
For both files you just pass the required parameter values to the code

something like this may do what you want

VBA Code:
Sub Test()
    Dim Filename As Variant, CopySheet As Variant, ExportSheet As Variant
    Dim i As Long
    
     FolderPath = "C:\Users\MY-NAME\Downloads\BRIDGESTONE REPORT\"
     
     Filename = Array("Elmarghanie Brand .xlsm", "COMPARE REPORTS.xlsm")
     CopySheet = Array("TABLE 1", "MISSED")
     ExportSheet = Array("REPORT", "OUTPUT")
     
     For i = LBound(Filename) To UBound(Filename)
        OpenFilesFromFolder Filename(i), FolderPath, CopySheet(i), ExportSheet(i)
     Next i

Dave
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top