0

This is my code for copying a sheet to new sheet. When I ran the program with breakpoint on Workbooks.Open(path) it was working correctly but when I ran without the breakpoint it simply opened the workbook without creating any sheet.
I have tried my best to rectify the error but I couldn't get the desired result.

Sub CopyCat()    

Dim ws As Worksheet
Dim no As Integer
Set ws1 = ActiveSheet
Dim path As String

temp_name = InputBox("Enter the Sheet No to be Created", "Enter the Value")

For Loop1 = 1 To ws1.UsedRange.Rows.Count
    path = Application.ActiveWorkbook.path & "\" & Application.WorksheetFunction.Trim(Trim(ws1.Cells(Loop1, 1).Value)) & " " & ws1.Cells(Loop1, 2).Value & ".xlsx"

    Set wb1 = Workbooks.Open(path)

    'ListBox1.AddItem wb.Name
    temp_name = "Sheet" & temp_name

    'error1 = CheckSheet(wb1, temp_name)
    'If (error1 <> True) Then
    ws1.Cells(4, 1).Value = "Created" & CStr(Loop1)
    Set ws = wb1.Worksheets(Sheets.Count)

    ws.Copy After:=wb1.Sheets(Sheets.Count)
    Set ws = ActiveSheet
    ws.Name = temp_name

    'Call PageSetting
    wb1.Close SaveChanges:=True
    ws1.Cells(4, 1).Value = "Created Done" & CStr(Loop1)
    'Else
    'wb1.Close SaveChanges:=True
    'End If
Next Loop1

End Sub


Function CheckSheet(ByVal wb As Workbook, ByVal sSheetName As String) As Boolean

Dim oSheet As Excel.Worksheet
Dim bReturn As Boolean

For Each oSheet In wb.Sheets

    If oSheet.Name = sSheetName Then
        bReturn = True
        Exit For
    End If

Next oSheet

CheckSheet = bReturn

End Function
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
Manoj Mohan
  • 633
  • 5
  • 13
  • Do you get any errors? What [error handling mode](http://stackoverflow.com/q/12687105/11683) is set? – GSerg Aug 20 '16 at 16:26
  • No error I got. It just simply opened the sheet while I ran it. But when I copied this module and created a new module with same code it ran perfectly. I don't know why did it happened but it worked. – Manoj Mohan Aug 23 '16 at 05:22
  • It's not resolved yet – Manoj Mohan Sep 08 '16 at 05:35

1 Answers1

-1

This question is a bit vague, so i assumed a few things based on the code you provided.

You want to copy a worksheet from a workbook that runs the macro to another excel file.

All file names are listed in the source worksheet, column A - let's call it "Interface" worksheet.

You will need to add reference to Microsoft Scripting Runtime in your project for the FileSystemObject to work.

Code below isnt wery well written or optimised, yet it works.

Sub CopySht(NamesRange As Range, NameOfSheetToCopy As String)

Dim fso As FileSystemObject, oFile As File, fPath As String, fNamesArr() As Variant, fFolder As Folder
Set fso = New FileSystemObject

Dim InputWb As Workbook, InterfaceWs As Worksheet
Set InputWb = ThisWorkbook
Set InterfaceWs = InputWb.Worksheets("Interface")

Dim SheetToCopy As Worksheet
Set SheetToCopy = InputWb.Worksheets(NameOfSheetToCopy)

Set NamesRange = InterfaceWs.Range(NamesRange.Address)



fNamesArr() = NamesRange.Value

fPath = InputWb.path
Set fFolder = fso.GetFolder(fPath)

Dim i As Integer

For Each oFile In fFolder.Files
    For i = LBound(fNamesArr) To UBound(fNamesArr)
        If oFile.Name = fNamesArr(i, 1) & ".xls" Or oFile.Name = fNamesArr(i, 1) & ".xlsx" Then

            On Error Resume Next
            If Not (Workbooks(oFile.Name) Is Nothing) Then
                Workbooks(oFile.Name).Close SaveChanges:=False
            End If

            Workbooks.Open (oFile.path)

            If Not (CheckSheet(Workbooks(oFile.Name), SheetToCopy.Name)) Then
                SheetToCopy.Copy After:=Workbooks(oFile.Name).Sheets(1)
                Workbooks(oFile.Name).Close SaveChanges:=True
            End If

            If Not (Workbooks(oFile.Name) Is Nothing) Then
                Workbooks(oFile.Name).Close SaveChanges:=False
            End If

        End If
    Next i
Next oFile


End Sub

Function CheckSheet(ByVal wb As Workbook, ByVal sSheetName As String) As Boolean

    Dim oSheet As Excel.Worksheet
    Dim bReturn As Boolean

    For Each oSheet In wb.Sheets

        If oSheet.Name = sSheetName Then

            bReturn = True
            Exit For

        End If

    Next oSheet

    CheckSheet = bReturn

End Function

It doesnt matter if you pass NamesRange as qualified or unqualified range object, as shown below

Sub Wrapper()

    CopySht Range("A1:A6"), "CopyMe"
    'CopySht ThisWorkbook.Worksheets("Interface").Range("A1:A6"), "CopyMe"

End Sub
  • 1
    The question is not vague. The problem is that the OP's code works with a breakpoint and does not work without it. It happens in Excel sometimes. – GSerg Aug 20 '16 at 16:25
  • @GSerg, can you elaborate on that? Why does it happen sometimes? – hstdggsdtgsdafssarf456 Aug 20 '16 at 17:59
  • 1
    Usually because of race conditions related to outside events. The file system is one of the possible sources. Hitting a breakpoint gives the external event enough time to complete so the code can continue without an error. An example would be deleting a file and immediately trying to recreate it, only to find that it's not quite deleted yet so the creation fails. It should not really happen but it does sometimes. – GSerg Aug 20 '16 at 19:50
  • Thanks for the clarification. – hstdggsdtgsdafssarf456 Aug 21 '16 at 12:11