Solved

optimizing vba

Posted on 2006-11-29
3
698 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 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

20 Experts available now in Live!

Get 1:1 Help Now