Solved

optimizing vba

Posted on 2006-11-29
3
697 Views
Last Modified: 2010-04-30
This is only a small fraction of the amount of code I have piled up for this project I am working on.  If someone could please show me how to optimize this code below, and maybe explain some fundamental things that slow up vba code. Thanks in advance!


Sub MasterPOI1()
Dim r As Range
    'Add Worksheet
    Application.ScreenUpdating = False
    Worksheets.Add
    ActiveSheet.Name = "POI"
   
    Sheets("Backup").Select
    Cells.Select
    Selection.Copy
    Sheets("POI").Select
    Cells.Select
    ActiveSheet.Paste
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Selection.Delete Shift:=xlUp
    Cells.Select
    Selection.EntireColumn.Hidden = False
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("G:O").Select
    Selection.Delete Shift:=xlToLeft
    Columns("I:M").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:D").Select
    Selection.Insert Shift:=xlToRight
    Columns("C:C").Select
    Selection.Cut
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight
 
    Columns("A:A").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=1, Criteria1:="="
    Rows("1:1100").Select
    Selection.Delete Shift:=xlUp
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
   
    'add vlookup column to add main categories (u.s. government obligations, asset backed securities)
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("B3").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(ISERROR(VLOOKUP(RC[1],'Security Types'!R1C1:R30C5,5,FALSE)),"""",VLOOKUP(RC[1],'Security Types'!R1C1:R30C5,5,FALSE))"
    Range("B3").Select
    Selection.AutoFill Destination:=Range("B3:B2000"), Type:=xlFillDefault
    Range("B3:B2000").Select
    Columns("B:B").EntireColumn.AutoFit
    Columns("B:B").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

   
End Sub
0
Comment
Question by:tiehaze
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Venkateswarlu Kaipu
ID: 18068399
tiehaze,

Could you please tell what is your requirement?

You want to understand the code? Do you need some have to explain the code?

Regards,
Venkat
0
 

Author Comment

by:tiehaze
ID: 18068629
I was just wondering if there are lines that can be combined in anyway to may the code shorter. I remember someone telling me that if you use .select, it slows down the macro. Things like that
0
 
LVL 5

Accepted Solution

by:
Venkateswarlu Kaipu earned 500 total points
ID: 18068777
Please find below the optimized code

It was not fully optimized because the code is not recorded properly.


Sub MasterPOI1()
    Dim r As Range
    'Add Worksheet
    Application.ScreenUpdating = False
    Worksheets.Add
    ActiveSheet.Name = "POI"
    Sheets("Backup").UsedRange.Copy
    Sheets("POI").Select
    Sheets("POI").Cells(1, 1).PasteSpecial xlPasteAll
    Sheets("POI").Rows("1:1").Delete
    'Sheets("POI").Columns("A:IV").Hidden = False
    Sheets("POI").Columns("A:A").Delete
    Sheets("POI").Columns("G:O").Delete
    Sheets("POI").Columns("I:M").Delete
    Sheets("POI").Columns("D:D").Insert Shift:=xlToRight
    Sheets("POI").Columns("A:A").Insert Shift:=xlToRight
    Columns("A:A").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=1, Criteria1:="="
    Sheets("POI").Rows("1:1100").Delete
    Sheets("POI").Rows("1:1").Insert Shift:=xlDown
    Sheets("POI").Rows("1:1").Insert Shift:=xlDown
    'add vlookup column to add main categories (u.s. government obligations, asset backed securities)
    Sheets("POI").Columns("B:B").Insert Shift:=xlToRight
    Sheets("POI").Range("B3").Formula = "=IF(ISERROR(VLOOKUP(RC[1],'Security Types'!R1C1:R30C5,5,FALSE)),"""",VLOOKUP(RC[1],'Security Types'!R1C1:R30C5,5,FALSE))"
    Range("B3").Select
    Selection.AutoFill Destination:=Range("B3:B2000"), Type:=xlFillDefault
    Range("B3:B2000").Select
    Columns("B:B").EntireColumn.AutoFit
    Columns("B:B").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.ScreenUpdating = True
End Sub
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now