Hi Arvind,
Sorry about that. I would have thought that:
wbSource.Close False
ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).UsedRange.Value _
= ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).UsedRange.Value
...would have taken care of that. I admit that in the the test source files created, I simply used =Sheet3!A1:C3 in a block of cells to simulate formula results, and didn't have a problem.
Anyone: Maybe I am missing something?
Anyways, not tested but with test copies of both source and destination file(s), maybe try:
Option Explicit
Sub OverwriteOldSheets()
Dim _
FSO As Object, _
wbSource As Workbook, _
wksSource As Worksheet, _
rngLastFile As Range, _
rngFileList As Range, _
rCell As Range, _
lLRow As Long, _
lIndex As Long
Const START_ROW As Long = 11 '<--- To allow for header row, change to suit.
Set FSO = CreateObject("Scripting.FileSystemObject")
With shtFileNames
Set rngLastFile = .Range("B2:B" & Rows.Count).Find(What:="*", _
After:=.Cells(2, 2), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious)
If rngLastFile Is Nothing Then Exit Sub
Set rngFileList = .Range("B" & START_ROW & ":" & "B" & rngLastFile.Row)
For Each rCell In rngFileList
If Not rCell.Value = vbNullString _
And FSO.FileExists(rCell.Value) Then
'// Bypass any error for the moment, attempt to open the existing //
'// file with the password supplied. //
On Error Resume Next
Set wbSource = Workbooks.Open(Filename:=rCell.Value, _
ReadOnly:=True, _
Password:=rCell.Offset(, 2).Value)
'// In case of failure, advise user of said, clear error and jump //
'// to end of loop //
If Err.Number > 0 Then
MsgBox "Wrong pwd supplied for: " & rCell.Value, 0, vbNullString
Err.Clear
On Error GoTo 0
GoTo NextFile
End If
On Error GoTo 0
With wbSource
'// PLEASE NOTE: //
'// This needs included... //
On Error Resume Next
Set wksSource = .Worksheets(rCell.Offset(, 1).Value)
On Error GoTo 0
If Not wksSource Is Nothing Then
If ShExists(ThisWorkbook, rCell.Offset(, 1).Value) Then
lIndex = ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).Index
Application.DisplayAlerts = False
ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).Delete
Application.DisplayAlerts = True
Else
lIndex = ThisWorkbook.Worksheets.Count + 1
End If
'//Try adding //
wksSource.UsedRange.Value = wksSource.UsedRange.Value
If lIndex = 1 Then
wksSource.Copy Before:=ThisWorkbook.Worksheets(lIndex)
Else
wksSource.Copy After:=ThisWorkbook.Worksheets(lIndex - 1)
End If
wbSource.Close False
'// and skipping... //
'ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).UsedRange.Value _
= ThisWorkbook.Worksheets(rCell.Offset(, 1).Value).UsedRange.Value
Else
wbSource.Close False
MsgBox "No sheet named: " & rCell.Offset(, 1).Value
End If
End With
Else
MsgBox "File: " & rCell.Value & vbCrLf & "does not exist"
End If
NextFile:
Next
End With
End Sub
Function ShExists(WB As Workbook, ShName As String) As Boolean
Dim wks As Worksheet
On Error Resume Next
Set wks = WB.Worksheets(ShName)
On Error GoTo 0
ShExists = CBool(Not wks Is Nothing)
End Function
Does that work?
Mark