Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

optimizing vba

Posted on 2006-11-29
3
Medium Priority
?
741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

715 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