Code Help For Adding Or Replacing Sheets

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,196
Office Version
  1. 2010
Platform
  1. Windows
Earlier this week I asked for a code to add a sheet to multiple workbooks. This works fine but now I learned I need to replace the sheet already with an updated one. Is there away to edit this to replace the sheet or remove the sheet and I can run the original code again?

Code:
Public Sub FixAllFiles()
FixAllFilesInDir "C:\Order\"
End Sub


Private Sub FixAllFilesInDir(ByVal pvDir)
Dim FSO, oFolder, oFile, oRX
Dim sTxt As String, sFile As String
Dim wbSrc As Workbook, wbTarg As Workbook
On Error GoTo errGetFiles
Set wbSrc = ActiveWorkbook   'source wb with the jpeg.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(pvDir)
If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
For Each oFile In oFolder.Files
  If InStr(oFile.Name, ".xlsx") > 0 Then            'import file here
      sFile = oFile
      Workbooks.Open sFile
      Set wbTarg = ActiveWorkbook
      
           'copy the jpg sheet to new workbook
        wbSrc.Activate
        wbSrc.ActiveSheet.Copy After:=wbTarg.Sheets(1)
        Filename = Dir(folder & "*.xlsx", vbNormal)
        wbTarg.Close True
        
  End If
Next
endit:
Set oFile = Nothing
Set oFolder = Nothing
Set FSO = Nothing
MsgBox "Done"
Exit Sub
errGetFiles:
MsgBox Err.Description, , Err
Resume endit
Resume
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Execute this macro to delete the sheet.
Place the macro in the same book with the other macro.
Before executing the macro, you must select the sheet that was copied.

Code:
Sub Delete_Sheet()
    Dim FSO As Object, oFolder As Object, oFile As Object
    Dim pvDir As String, sName As String
    Dim wbSrc As Workbook, wbTarg As Workbook
    
    Application.ScreenUpdating = False
    
    pvDir = "C:\Order\"
    
    Set wbSrc = ActiveWorkbook   'source wb with the jpeg.
    sName = wbSrc.ActiveSheet.Name
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = FSO.GetFolder(pvDir)
    If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
    
    For Each oFile In oFolder.Files
        If InStr(oFile.Name, ".xlsx") > 0 Then
            Set wbTarg = Workbooks.Open(oFile)
            On Error Resume Next
            wbTarg.Sheets(sName).Delete
            wbTarg.Close True
            On Error GoTo 0
        End If
    Next
    
    MsgBox "End"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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