+ Reply to Thread
Results 1 to 11 of 11

Please assit to Optimize the performance

  1. #1
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Post Please assit to Optimize the performance

    Hi Team,
    Can one of you please assist me in optimizing the below MACRO performance. I was able to execute the MACRO and I see output as desired on most of the time however my major challenge is as it is taking a good amount of time to run irrespective of the input data.

    <
    Sub Master_Macro()
    ' test1 Macro
    Dim RowtoTest As Long
    Dim workrange As Range
    Dim cell As Range
    Dim lLastRow As Long

    Worksheets("Pay Rate RPT").Activate
    With Sheets("Pay Rate RPT")

    Range("A1").Select
    ActiveCell.EntireRow.Delete
    ActiveCell.EntireRow.Delete
    ActiveCell.EntireRow.Delete
    ActiveCell.EntireRow.Delete
    Range("A2").Select
    ActiveCell.EntireRow.Delete
    'ActiveCell.EntireRow.Delete
    'ActiveCell.EntireRow.Insert
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Check"
    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Range("B2:B" & lLastRow)
    .FormulaR1C1 = "=LEN(RC[-1])"
    .Value = .Value
    End With

    For RowtoTest = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
    With Cells(RowtoTest, 2)
    If .Value <> "152" _
    Then _
    Rows(RowtoTest).EntireRow.Delete
    End With
    Next RowtoTest

    Range("B1").Select
    ActiveCell.EntireColumn.Delete
    Range("A1").Select

    'Range("B:B").Select
    'Set workrange = Intersect(Selection, ActiveSheet.UsedRange)
    'For Each cell In workrange
    'If ActiveCell.Value <> "152" _
    'Then ActiveCell.EntireRow.Delete
    'Next cell

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 2), Array(11, 1), Array(21, 2), Array(30, 1), Array(45, 1), _
    Array(55, 1), Array(67, 1), Array(87, 1), Array(100, 1), Array(114, 1), Array(126, 1), _
    Array(142, 1)), TrailingMinusNumbers:=True
    Range("A1").Select

    Columns("F:F").Select
    Selection.Cut
    Columns("P:P").Select
    Selection.Insert Shift:=xlToRight
    Range("B1").Select

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Range("L2:L" & lLastRow)
    .FormulaR1C1 = "=RC[-11]&CHAR(45)&RC[-10]&CHAR(45)&RC[-9]&CHAR(45)&TEXT(RC[-8],""MMDDYY"")&CHAR(45)&TEXT(RC[-7],""MMDDYY"")&CHAR(45)&LEFT(RC[-1],3)"
    '.Value = .Value
    End With
    With Range("M2:M" & lLastRow)
    .FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-1],'Prelim RPT'!C19,1,0)),""Not Found"",""Found"")"
    '.Value = .Value
    End With
    End With
    'Naresh
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection.Interior
    Selection.Font.Bold = True
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 16764057
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    With Selection.Font
    .Name = "Frutiger 45 Light"
    .Size = 10
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    Cells.Select
    With Selection.Font
    .Name = "Frutiger 45 Light"
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With

    Range("A:O").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    Range("A1").Select

    Worksheets("Prelim RPT").Activate
    With Sheets("Prelim RPT")

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Range("A1").Select
    ActiveCell.EntireRow.Delete
    ActiveCell.EntireRow.Delete
    Range("A2").Select
    ActiveCell.EntireRow.Delete
    ActiveCell.EntireRow.Delete

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 2), Array(8, 1), Array(27, 2), Array(36, 1), Array(48, 1), _
    Array(60, 1), Array(79, 1), Array(92, 1), Array(108, 1), Array(126, 1), Array(138, 1), _
    Array(144, 1), Array(163, 1), Array(174, 1), Array(190, 1), Array(204, 1), Array(221, 1), _
    Array(239, 1)), TrailingMinusNumbers:=True
    Range("A1").Select

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Range("S2:S" & lLastRow)
    .FormulaR1C1 = "=IF(RC[-2]="""",RC[-18]&CHAR(45)&RC[-17]&CHAR(45)&RC[-16]&CHAR(45)&TEXT(RC[-15],""MMDDYY"")&CHAR(45)&TEXT(RC[-14],""MMDDYY"")&CHAR(45)&LEFT(RC[-8],3),"""")"
    '.Value = .Value
    End With
    With Range("T2:T" & lLastRow)
    '.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-1],'Pay Rate RPT'!C12,1,0)),""Not Found"",""Found"")"
    .FormulaR1C1 = "=IF(RC[-9]<>""TOT"",IF(ISERROR(VLOOKUP(RC[-1],'Pay Rate RPT'!C12,1,0)),""Not Found"",""Found""),"""")"
    '.FormulaR1C1 = "=IF(RC[-9]=.""DIV1"",IF(ISERROR(VLOOKUP(RC[-1],'Pay Rate RPT'!C12,1,0)),""Not Found"",""Found""),"""")"
    '.Value = .Value
    End With

    End With

    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection.Interior
    Selection.Font.Bold = True
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 16764057
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    With Selection.Font
    .Name = "Frutiger 45 Light"
    .Size = 10
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With

    With Selection.Font
    .Name = "Frutiger 45 Light"
    .Size = 10
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    Range("A:U").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
    End With
    Range("A1").Select

    Worksheets("DORS").Activate
    With Sheets("DORS")

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Range("P3:P" & lLastRow)
    .FormulaR1C1 = "=LEFT(RC[-7],FIND(CHAR(10),RC[-7]))"
    .Value = .Value
    End With

    With Range("Q3:Q" & lLastRow)
    .FormulaR1C1 = "=(RC[-9])"
    .Value = .Value
    End With

    Columns("Q:Q").Select
    Selection.TextToColumns Destination:=Range("Q1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True

    Columns("P:S").Select
    Selection.Copy

    Worksheets("Notes").Activate
    With Sheets("Notes")

    Sheets("Notes").Select
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 3)
    ActiveSheet.Outline.ShowLevels RowLevels:=2

    Columns("A:D").Select
    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _
    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    Range("A1").Select
    ActiveSheet.Outline.ShowLevels RowLevels:=2
    Range("A:D").Select

    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(1, 2), _
    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    ActiveSheet.Outline.ShowLevels RowLevels:=2

    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With
    Selection.Columns.AutoFit
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy

    Worksheets("DORS").Activate
    With Sheets("DORS")

    Sheets("DORS").Select
    Range("O2").Select
    ActiveSheet.Paste

    Range("P3").Select
    ActiveCell.FormulaR1C1 = "Sum of EXE QTY"
    With Range("P3:P" & lLastRow)
    .FormulaR1C1 = "=LEFT(RC[-1],SEARCH("" "",RC[-1])-1)"
    .Value = .Value
    End With
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "Symbols"
    Range("O1").Select
    ActiveCell.EntireColumn.Delete

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A:L").Select
    Cells.Select
    Selection.Columns.AutoFit

    With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    End With
    Range("A1:L1").Select
    End With

    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection.Interior
    Selection.Font.Bold = True
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 16764057
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    Worksheets("Cash Exception and Full Match").Activate
    With Sheets("Cash Exception and Full Match")

    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Range("T3").Select
    ActiveCell.FormulaR1C1 = "Vlook up"
    With Range("T4:T" & lLastRow)
    .FormulaR1C1 = "=IF(RC[-13]=""US"",VLOOKUP(RC[-18],'Prelim RPT'!C[-19],1,0),IF(RC[-13]="""","""",IF(RC[-13]=""COUNTRY"",""VLOOKUP"",""Foreign"")))"
    .Value = .Value
    End With

    With Range("U4:u" & lLastRow)
    .FormulaR1C1 = "=IF(RC[-14]=""COUNTRY"",""Comment"","""")"
    .Value = .Value

    End With
    End With

    Worksheets("KIP Open and Pending Items").Activate
    With Sheets("KIP Open and Pending Items")

    ' Range("A1").Select
    ' ActiveCell.EntireRow.Insert

    Range("E1").Select
    ActiveCell.EntireColumn.Delete
    Range("A1").Select
    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "VLOOKUP"

    With Range("H2:H" & lLastRow)
    .FormulaR1C1 = "=VLOOKUP(C1,'Prelim RPT'!C[-7],1,0)"
    .Value = .Value
    Range("H3").Select
    End With
    With Range("I2:I" & lLastRow)
    .FormulaR1C1 = "=IF(RC[-3]=""UNITED STATES"","" "",IF(RC[-3]=""Puerto Rico"",""UIT"",IF(RC[-3]="""","""",""Foreign Security"")))"
    .Value = .Value
    Range("A1").Select
    End With
    End With

    Range("A1").Select
    Sheets("DashBoard").Select
    Range("A1").Select

    MsgBox "Macro Completed - Naresh Maram"
    End With
    End With
    End Sub
    >

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Please assit to Optimize the performance

    Sorry, but your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Please assit to Optimize the performance

    Hi Team,
    Can one of you please assist me in optimizing the below MACRO performance. I was able to execute the MACRO and I see output as desired on most of the time however my major challenge is as it is taking a good amount of time to run irrespective of the input data.


    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    03-10-2017
    Location
    USA
    MS-Off Ver
    office 2016
    Posts
    393

    Re: Please assit to Optimize the performance

    Ill let an expert give better advise but I see your doing a lot of .activate and .select in the code. That will take up time (i did it a lot until someone pointed out how not to). Some of the code seems to be doing the same thing multiple times
    Please Login or Register  to view this content.
    without seeing an example i cant tell why that is necessary. For example why not just select the range and .clearcontents instead of deleting which takes more time like this

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    im assuming its on sheet 1 and that your row has an end, like at z... then the next command your deleting a2.entirerow so thats doing the same thing as the first... i think?
    Last edited by kevinu; 06-20-2018 at 06:14 PM.

  5. #5
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Please assit to Optimize the performance

    Thanks for your Kevinu, In above .Delete, I was actually deleting the whole row itself and nit just the data. Am sure each of the steps in my code would have a much better steps which am not aware of....
    Thanks for your tip though, will certainly keep in mind...
    Let me wait for a further more intense sokution to see rwmarkable change in time taken to execute than now...Thanks you once again.

  6. #6
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Please assit to Optimize the performance

    like this to delete rows 1 to 6
    Please Login or Register  to view this content.
    replace lines like this with a selection or active cell reference
    Please Login or Register  to view this content.
    with just this
    Please Login or Register  to view this content.
    But to more accurately address your speed issues with this much code - I suggest you run the macro and press escape before it finishes a few times then tell us where in the macro it is usually gets stuck.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  7. #7
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Please assit to Optimize the performance

    Thanks for your time Scottiex, will certainly apply your suggestions and will try see where exactly is it taking time and will seek your assistance.
    It's really pleasure to have someone like you who always ready to help.
    Thanks for your time.

  8. #8
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Please assit to Optimize the performance

    Hi Scottiex,
    I tried to esc while macro was running and most of the time I see code stopped in below - I suppose this is what taking major run time.
    Is there any way to run the below code quickly PLEASE

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Please assit to Optimize the performance

    So it looks like you are telling row 1 to Bold and colour the cells and make everything font "Frutiger 45 Light" and size 10 is that all?
    or are you clearing out existing fonts etc?

    try this

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-15-2013
    Location
    Hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Please assit to Optimize the performance

    Hi Scottiex,
    Seems your second tip also did worked,
    Can you please have a quick look at below and
    help me with any better alternate please
    I have similar vlookups too that I might amend, might see further change in run time

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Please assit to Optimize the performance

    If you change those formula from

    this format
    Please Login or Register  to view this content.
    to this one using the letters like it shows in excel
    Please Login or Register  to view this content.
    then they will be more readable.

    But that formula doesn't look like it would be slow.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to optimize your VBA possible?
    By alexgoaga in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2017, 05:37 AM
  2. Replies: 2
    Last Post: 02-26-2016, 03:15 AM
  3. current moth MTD performance with last year same month same days performance
    By satyanarayana in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-03-2015, 03:36 AM
  4. Replies: 4
    Last Post: 11-23-2014, 05:54 PM
  5. VBA For loop, optimize speed and performance
    By britzer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-04-2014, 08:56 AM
  6. [SOLVED] PLease assit me with an IF then statement.
    By Chaos247 in forum Excel General
    Replies: 6
    Last Post: 08-30-2012, 03:54 AM
  7. How to optimize
    By jonelamora in forum Access Programming / VBA / Macros
    Replies: 0
    Last Post: 07-04-2012, 02:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1