Results 1 to 12 of 12

Why is code in .xlsm slower than .xls in excel 2007

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Why is code in .xlsm slower than .xls in excel 2007

    Hello all. I need to know what I can do to speed up my code. The following code runs in 12 seconds if the file is .xls but when converted to xlsm, it takes 5 min and 10 seconds to process 18 columns and 12 rows of data.

    Sub CleanAndSort()
    
    Dim rng As Range, nRow As Long
    'Dim wbTemplate As Workbook, wbTmp As Workbook
    
        With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        End With
    
    With Sheets("Import Template")
        .UsedRange.Clear
        .Activate
    End With
    
    With Sheets("Class Roster Template")
        For Each rng In .UsedRange
            rng = WorksheetFunction.Substitute(rng, "/", "")
        Next rng
    End With
    
    'AFS Boot size check
    With Sheets("Class Roster Template")
    For Each rng In .Range("K2", .Range("K2").End(xlDown))
            rng = WorksheetFunction.Substitute(rng, "XS", "S")
        Next rng
    End With
    
    'ALO Boot size check
    With Sheets("Class Roster Template")
    For Each rng In .Range("L2", .Range("L2").End(xlDown))
            rng = WorksheetFunction.Substitute(rng, "M", "L")
        Next rng
    End With
    
    'Belt size check
    With Sheets("Class Roster Template")
    For Each rng In .Range("M2", .Range("M2").End(xlDown))
            rng = WorksheetFunction.Substitute(rng, "XL", "L")
        Next rng
    End With
    
    'Glove size check
    With Sheets("Class Roster Template")
    For Each rng In .Range("N2", .Range("N2").End(xlDown))
            rng = WorksheetFunction.Substitute(rng, "XXL", "XL")
        Next rng
    End With
    
    With Sheets("Class Roster Template")
    For Each rng In .Range("A2", .Range("A2").End(xlDown))
            rng = WorksheetFunction.Substitute(rng, "CG", "")
        Next rng
    End With
    
    With Sheets("Class Roster Template")
        nRow = .UsedRange.Rows.Count
        .Range("A1:A" & nRow).Copy Sheets("Import Template").Range("A1")
        .Range("B1:B" & nRow).Copy Sheets("Import Template").Range("B1")
        .Range("C1:C" & nRow).Copy Sheets("Import Template").Range("C1")
        .Range("D1:D" & nRow).Copy Sheets("Import Template").Range("D1")
        .Range("E1:E" & nRow).Copy Sheets("Import Template").Range("E1")
        .Range("F1:F1" & nRow).Copy Sheets("Import Template").Range("F1")
        .Range("G1:G1" & nRow).Copy Sheets("Import Template").Range("G1")
        .Range("H1:H1" & nRow).Copy Sheets("Import Template").Range("H1")
        .Range("I1:I1" & nRow).Copy Sheets("Import Template").Range("I1")
        .Range("K1:Q" & nRow).Copy Sheets("Import Template").Range("J1")
        
    End With
    
    For Each rng In Range("A2", Range("A2").End(xlDown))
        rng = "CG" & rng
    Next rng
    
    Range("A1:Z1").Select
        With Selection
            .WrapText = False
            .Orientation = 0
            .Columns.AutoFit
            .Rows.AutoFit
            .AddIndent = False
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection.Font
            .Name = "Times New Roman"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
    
    Application.ScreenUpdating = True
    
    Call DeleteBlankRows
    
    End Sub
    Sub DeleteBlankRows()
    
        Cells.Select
        
        Dim Rw As Long, RwCnt As Long, rng As Range
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
    On Error GoTo Exits:
    
    
    
        If Selection.Rows.Count > 1 Then
            Set rng = Selection
        Else
            Set rng = Range(Rows(1), Rows(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row()))
            End If
            RwCnt = 0
            For Rw = rng.Rows.Count To 1 Step -1
                If Application.WorksheetFunction.CountA(rng.Rows(Rw).EntireRow) = 0 Then
                    rng.Rows(Rw).EntireRow.Delete
                    RwCnt = RwCnt + 1
                End If
            Next Rw
    Exits:
            Application.ScreenUpdating = True
            Application.Calculation = xlCalculationAutomatic
                   
    Call DeleteBlankColumns
        
    End Sub
    
    Sub DeleteBlankColumns()
    
        Cells.Select
    
        Dim Col As Long, ColCnt As Long, rng As Range
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
    On Error GoTo Exits:
    
        If Selection.Columns.Count > 1 Then
            Set rng = Selection
        Else
            Set rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Col()))
            End If
            ColCnt = 0
            For Col = rng.Columns.Count To 1 Step -1
                If Application.WorksheetFunction.CountA(rng.Columns(Col).EntireColumn) = 0 Then
                    rng.Columns(Col).EntireColumn.Delete
                    ColCnt = ColCnt + 1
                End If
            Next Col
    Exits:
            Application.ScreenUpdating = True
            Application.Calculation = xlCalculationAutomatic
    
    Call RestoreHeaderNewWorkbook
            
    End Sub
    
    Sub RestoreHeaderNewWorkbook()
    
    Dim x As Long
    Dim NameFile As String
    Dim sFileName As String
    Dim wbTemplate As Workbook
    Dim wbTmp As Workbook
    Dim wbEmail As Workbook
    Dim OutApp As Object
    Dim OutMail As Object
    
    Application.ScreenUpdating = False
    On Error Resume Next
    
    'copies header from temp sheet to import template sheet
        Sheets("Temp").Select
        Range("A1:I1").Select
        Selection.Copy
        Sheets("Import Template").Select
        Range("A1").Select
        ActiveSheet.Paste
        
        Sheets("Temp").Select
        Range("K1:Q1").Select
        Selection.Copy
        Sheets("Import Template").Select
        Range("J1").Select
        ActiveSheet.Paste
        
    ' put cursor back in a1
        Sheets("Import Template").Activate
        ActiveSheet.Range("A1").Select
    
    Set wbTemplate = ActiveWorkbook
    
    NameFile = Application.InputBox("Enter name for file:")
    
    'copy entire sheet to new workbook with no format changes
    wbTemplate.Worksheets("Import Template").Copy
    
    Range("A:Z").Columns.AutoFit
            
    ChDir "C:\Temp\Input Files"
    ActiveWorkbook.SaveAs Filename:=""C:\Temp\Input Files"\" & NameFile & ".csv", FileFormat:=xlCSV, _
    CreateBackup:=False
    
    'The following subroutine sends the last saved version of the active workbook in an e-mail message.
    'Change the mail address and subject in the macro before you run it.
    'Working in 2000-2007
    
    Set wbTmp = ActiveWorkbook
    
      wbTemplate.Sheets("Class Roster Template").Range("J:R").Copy wbTmp.Sheets(1).Range("J1")
      
    ChDir ""C:\Temp\Input Files""
    wbTmp.SaveAs Filename:=""C:\Temp\Input Files"\" & NameFile & " " & ".csv", FileFormat:=xlCSV, _
    CreateBackup:=False
    
    Set wbEmail = ActiveWorkbook
        
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    
        With OutMail
            .To = "yourmail@yahoo.com"
            .CC = ""
            .BCC = ""
              .Subject = NameFile
            .Body = "This email was automatically generated. Warehouse, attached file is for your reference on what is being uploaded."
              .Attachments.Add wbEmail.FullName
            .Display
        End With
           
        'close .csv after it is mailed
        wbTmp.Close SaveChanges:=False
        wbEmail.Close SaveChanges:=False
        
        'delete the emailed file
        Kill ""C:\Temp\Input Files"\" & NameFile & " " & ".csv"
              
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
      
     With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    
     MsgBox "Complete"
     
    End Sub
    Thanks,
    Andrew
    Last edited by drewship; 08-31-2010 at 10:05 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1