Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50079

getting Application_defined or Object_defined error

$
0
0
I am using excel 2007. I am using a macro which copies all visible sheets to new workbook and saves them as text files. I have more than 12 sheets from which i have to copy at least 10 sheets. 4 visible sheets are copies but on the fifth sheet i got an error of Application_defined or Object_defined error. I have tables in other six sheets . please help me solve this problem. This code was working properly since last week. today i again tested it and getting this error. dont know what the problem is :-(



Code:

  Sub day_end_process()

                  'Working in 97-2010
                    Dim FileExtStr As String
                    Dim FileFormatNum As Long
                    Dim Sourcewb As Workbook
                    Dim Destwb As Workbook
                    Dim sh As Worksheet
                    Dim DateString As String
                    Dim FolderName As String
               
                    'Copy every sheet from the workbook with this macro
                    Set Sourcewb = ThisWorkbook
               
                    'Create new folder to save the new files in
                    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
                    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
                    MkDir FolderName
                   
                    'Copy every visible sheet to a new workbook
                    For Each sh In Sourcewb.Worksheets
               
                        'If the sheet is visible then copy it to a new workbook
                        If sh.Visible = -1 Then
                            sh.Copy
               
                            'Set Destwb to the new workbook
                            Set Destwb = ActiveWorkbook
               
                            'Determine the Excel version and file extension/format
                            With Destwb
                                  FileExtStr = ".txt": FileFormatNum = -4158
                            End With
               
                            'Change all cells in the worksheet to values if you want
                            'I get error in this if statement.

                            If Destwb.Sheets(1).ProtectContents = False Then
                                With Destwb.Sheets(1).UsedRange
                                    .Cells.Copy
                                    .Cells.PasteSpecial xlPasteValues
                                    .Cells(1).Select
                                End With
                                Application.CutCopyMode = False
                            End If
                            'Save the new workbook and close it
                            With Destwb
                                .SaveAs FolderName _
                                      & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                                        FileFormat:=FileFormatNum
                                .Close False
                            End With
                        End If
                GoToNextSheet:
                    Next sh
                    MsgBox "You can find the files in " & FolderName
                    Sheets("Main Page").Select
                    With Application
                        .ScreenUpdating = True
                        .EnableEvents = True
                        .Calculation = xlCalculationAutomatic
                    End With
                    Sheets("Main Page").Select
                    ActiveWorkbook.Save
                   
  End Sub


Viewing all articles
Browse latest Browse all 50079

Trending Articles