Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

optimizing vba

Posted on 2006-11-29
3
Medium Priority
?
754 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

885 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