The problem is that the first time a duplicate is encountered, instead of ADDING it to an existing workbook, you're trying to create a new one. Instead, I'd wait until they're all done before closing the workbooks. Then, within the code, only create a workbook if it isn't already open. I'd modify the code to look like: Option Explicit Sub movesheets() Dim Mainrng As Range Dim LR As Double Dim i As Integer Dim Oldwkb As Workbook Dim Newwkb As Workbook Dim srt As String Set Oldwkb = ActiveWorkbook LR = Cells(Rows.Count, 1).End(xlUp).Row ' Set Mainrng = Range("a1").Offset(0, 1).Resize(LR, 1) Application.DisplayAlerts = False i = 2 On Error Resume Next Do Until Cells(i, "B") = "" srt = Cells(i, "C").Value Err.Clear Workbooks(srt & ".xlsx").Activate If (Err.Number <> 0) Then Set Newwkb = Workbooks.Add Else Set Newwkb = ActiveWorkbook End If Oldwkb.Activate Worksheets(Cells(i, "A").Value).Copy Before:=Newwkb.Worksheets(1) If (Newwkb.Name <> srt & ".xlsx") Then Newwkb.SaveAs Oldwkb.Path & "\" & srt & ".xlsx" Else Newwkb.Save End If ' Newwkb.Close True Oldwkb.Activate If (Cells(i, "B").Value = "Move") Then Sheets(Cells(i, "A").Value).Delete i = i + 1 Loop For i = 2 To 100 If (Cells(i, "B").Value = "") Then Exit For Workbooks(Cells(i, "C").Value & ".xlsx").Close savechanges:=True Next i Application.DisplayAlerts = True End Sub
Paul ----------------------------------------- “Do all the good you can, By all the means you can, In all the ways you can, In all the places you can, At all the times you can, To all the people you can, As long as ever you can.” - John Wesley ----------------------------------------- From: Nilkanth Raval <nilkanth.ra...@gmail.com> >To: excel-macros@googlegroups.com; Vaibhav Joshi <v...@vabs.in> >Sent: Thursday, February 5, 2015 1:19 PM >Subject: $$Excel-Macros$$ Move Sheets by Worksheet Index > > > >Hello Sir, > > >I have attached the excel file which contains sheets & worksheet index i.e >Summary > > >In Worksheet Index tab > > >Column A contains the sheet name >Column B contains move or copy sheet >Column C contains Workbook name to be created. > > >I want to move or copy sheet based on table. > > >I wrote the code but this work only if workbook name in Column C is different >but does not work if column C contains the same workbook name. > > >Please help. > > >Thanks, >Nilkanth Raval >+91 900 428 0 428 >+91 808 000 1620 >Think Before You Print.Consider ENVIRONMENT. >“SAVE TREES, SAVE EARTH” > >“All appears to change when we change!” > > -- >Are you =EXP(E:RT) or =NOT(EXP(E:RT)) in Excel? And do you wanna be? It’s >=TIME(2,DO:IT,N:OW) ! Join official Facebook page of this forum @ >https://www.facebook.com/discussexcel > >FORUM RULES > >1) Use concise, accurate thread titles. Poor thread titles, like Please Help, >Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get >quick attention or may not be answered. >2) Don't post a question in the thread of another member. >3) Don't post questions regarding breaking or bypassing any security measure. >4) Acknowledge the responses you receive, good or bad. >5) Jobs posting is not allowed. >6) Sharing copyrighted material and their links is not allowed. > >NOTE : Don't ever post confidential data in a workbook. Forum owners and >members are not responsible for any loss. >--- >You received this message because you are subscribed to the Google Groups "MS >EXCEL AND VBA MACROS" group. >To unsubscribe from this group and stop receiving emails from it, send an >email to excel-macros+unsubscr...@googlegroups.com. >To post to this group, send email to excel-macros@googlegroups.com. >Visit this group at http://groups.google.com/group/excel-macros. >For more options, visit https://groups.google.com/d/optout. > > > -- Are you =EXP(E:RT) or =NOT(EXP(E:RT)) in Excel? And do you wanna be? It’s =TIME(2,DO:IT,N:OW) ! Join official Facebook page of this forum @ https://www.facebook.com/discussexcel FORUM RULES 1) Use concise, accurate thread titles. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get quick attention or may not be answered. 2) Don't post a question in the thread of another member. 3) Don't post questions regarding breaking or bypassing any security measure. 4) Acknowledge the responses you receive, good or bad. 5) Jobs posting is not allowed. 6) Sharing copyrighted material and their links is not allowed. NOTE : Don't ever post confidential data in a workbook. Forum owners and members are not responsible for any loss. --- You received this message because you are subscribed to the Google Groups "MS EXCEL AND VBA MACROS" group. To unsubscribe from this group and stop receiving emails from it, send an email to excel-macros+unsubscr...@googlegroups.com. To post to this group, send email to excel-macros@googlegroups.com. Visit this group at http://groups.google.com/group/excel-macros. For more options, visit https://groups.google.com/d/optout.