Cleaning up VBA Code

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
Good Evening,

I have my coding complete, it works but it's a little sluggish and slow. I'm thinking it could be improved so I wanted to share in case anyone is able to assist in the clean up.

Thanks!

Code:
Sub FuturePDI()

   Sheets("Property Segment Data").Range("B2:I18").Clear
   Sheets("Property Segment Data").Range("N4:AC18").Clear
   Sheets("Property Segment Data").Range("B21:I34").Clear
   Sheets("Property Segment Data").Range("N21:AC34").Clear
   Sheets("Property Segment Data").Range("B37:I50").Clear
   Sheets("Property Segment Data").Range("N37:AC50").Clear
   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Sheets("Property Segment Data").Range("B2:I18").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B2").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B2").PasteSpecial xlPasteFormats
         
         Sheets("Property Segment Data").Range("N4:AC18").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N4").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N4").PasteSpecial xlPasteFormats
         
         Sheets("Property Segment Data").Range("B21:I34").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B21").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B21").PasteSpecial xlPasteFormats
         
         Sheets("Property Segment Data").Range("N21:AC34").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N21").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N21").PasteSpecial xlPasteFormats
         
         Sheets("Property Segment Data").Range("B37:I50").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B37").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("B37").PasteSpecial xlPasteFormats
         
         Sheets("Property Segment Data").Range("N37:AC50").Copy
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N37").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Property Segment Data").Range("N37").PasteSpecial xlPasteFormats
         
         wkbSourceBook.Close False
      End If
   End With
      Columns("A").EntireColumn.Hidden = True
      Rows("1").EntireRow.Hidden = True
      Columns("B").ColumnWidth = 23
      Columns("C").ColumnWidth = 28
      Columns("N").ColumnWidth = 28
      Columns("D:L").ColumnWidth = 11
      Columns("O:AC").ColumnWidth = 11
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,
untested but see if this update to your code helps


Code:
Sub FuturePDI()
    Dim wsPropertySegmentData As Worksheet
    Dim wkbSourceBook As Workbook
    Dim PasteCopyRange As Range, Area As Range
    Dim FileName As String
    
    Set wsPropertySegmentData = ThisWorkbook.Worksheets("Property Segment Data")
    
    Set PasteCopyRange = wsPropertySegmentData.Range("B2:I18,N4:AC18,B21:I34,N21:AC34,B37:I50,N37:AC50")
    
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        FileName = .SelectedItems(1)
    End With
    
    On Error GoTo myerror
    With Application
        .ScreenUpdating = False: .EnableEvents = False: .Calculation = xlCalculationManual
    End With
    
    PasteCopyRange.Clear
    
    Set wkbSourceBook = Workbooks.Open(FileName, , True)
    
    For Each Area In PasteCopyRange.Areas
        wkbSourceBook.Sheets("Property Segment Data").Range(Area.Address).Copy
        With Area.Cells(1, 1)
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
        End With
'clear clipboard
        Application.CutCopyMode = False
    Next Area
            
        wkbSourceBook.Close False
            
            
        Columns("A").EntireColumn.Hidden = True
        Rows("1").EntireRow.Hidden = True
        Columns("B").ColumnWidth = 23
        Columns("C").ColumnWidth = 28
        Columns("N").ColumnWidth = 28
        Columns("D:L").ColumnWidth = 11
        Columns("O:AC").ColumnWidth = 11
            
myerror:
        With Application
            .ScreenUpdating = True: .EnableEvents = True: .Calculation = xlCalculationAutomatic
        End With
        If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top