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
Solved

optimizing vba

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

808 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