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

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,

Just make a common code & pass the values that change to it as arguments.



Untested but see if this update to your code will do what you want



Place in STANDARD module

Code:
Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
                        ByVal CopyFromSheetName As String, ByVal ExportToSheetName As String)
                                          
    Dim ExtBk           As Workbook, IntBk      As Workbook
    Dim FilePath        As String
    Dim lRow            As Long
    Dim Rng(1 To 2)     As Range
   
    On Error GoTo myerror
    Set IntBk = ActiveWorkbook
   
    lRow = IntBk.Worksheets(CopyFromSheetName).Cells(Rows.Count, 1).End(xlUp).Row
   
    FilePath = Dir(FolderPath & ExportToFileName)
   
    If FilePath <> "" Then
       
        Application.ScreenUpdating = False
        Set ExtBk = Workbooks.Open(FolderPath & FilePath, 0, False)
       
        'clear any old values:
        With ExtBk.Worksheets(CopyFromSheetName)
            .Range("A2:E" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
        End With
       
        'Why do the copy one row at a time? DO it all in once:
        ExtBk.Worksheets(ExportToSheetName).Range("A2:A" & lRow).Value = IntBk.Worksheets(CopyFromSheetName).Range("A2:A" & lRow).Value
        Set Rng(1) = IntBk.Worksheets(CopyFromSheetName).Range("B2:E" & lRow)
        Set Rng(2) = ExtBk.Worksheets(ExportToSheetName).Range("C2:F" & lRow)
       
        Rng(2).Value = Rng(1).Value
       
    Else
   
        'file not found
        Err.Raise 53, , ExportToFileName & Chr(10) & "File Not found"
       
    End If
   
myerror:
    Application.DisplayAlerts = False
    If Not ExtBk Is Nothing Then ExtBk.Close Err = 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

And to call it

Code:
Sub Test()

     Filename = "Elmarghanie Brand .xlsm"
     FolderPath = "C:\Users\MY-NAME\Downloads\BRIDGESTONE REPORT\"
     CopySheet = "TABLE 1"
     ExportSheet = "REPORT"
    
     OpenFilesFromFolder Filename, FolderPath, CopySheet, ExportSheet

End Sub

Dave
 
Upvote 1
Solution
Hi Dave ,
I no know why the error subscript out of range , despite of I make sure the sheets names and I used copy and paste to avoid error for sheets names matching but still shows error !!
 
Upvote 0
Hi Dave ,
you don't have any explanation for my problem ?

Sorry, not had much availability this week

try replacing this this part of code

VBA Code:
'clear any old values:
        With ExtBk.Worksheets(CopyFromSheetName)
            .Range("A2:E" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
        End With

with this

VBA Code:
'clear any old values:
        With ExtBk.Worksheets(ExportToSheetName)
            .Range("A2:E" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
        End With

and see if resolves

Dave
 
Upvote 0
thanks , the error message is gone , but unfortunately doesn't show any data for exported sheet for the closed file .
 
Upvote 0
Is there any data in Column A of the copy sheet?

Dave
 
Upvote 0
here data are in copy sheet for open file
Bridgestone Stock Sales report(2).xls
ABCDE
1ITEMSIZE PATTERN ORIGIN ARRIVED
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
TABLE 1
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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