+ Reply to Thread
Results 1 to 11 of 11

Very long macro - Thanks

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Unhappy Very long macro - Thanks

    Hi All

    I used excel for year but only to do the simple things. I'm now getting into macro's, i have this macro that works but it takes an age to run and the screen flickering does my head in.
    I have a sheet call Bene, which in col BO with have CAT , A,B,C,D,E,F,G or H. It will then move all the A to Bene Cat A.

    I have only put this one on as an example but i have the same thing for UK,IBE,FRA,FIN
    and all have 8 sheets to go with them. I have used the same code for all macros and i've even done a macro that does them all together using the same code.

    Can someone please help me in making my macro more efficent
    Sub BENECAT()
     'BENE
    'CAT A
    Sheets("BENE").Select
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT A" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT A").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT A").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'CAT B
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT B" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT B").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT B").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'BENE CAT C
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT C" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT C").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT C").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'BENE CAT D
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT D" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT D").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT D").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'BENE CAT E
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT E" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT E").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT E").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'BENE CAT F
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT F" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT F").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT F").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    Sheets("BENE").Select
    'BENE CAT G
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT G" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT G").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT G").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
            
    Sheets("BENE").Select
    'BENE CAT H
    [BO2].Select
    Do Until ActiveCell = ""
    If ActiveCell = "CAT H" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT H").Select
    [A65536].Select
    Selection.End(xlUp).Select
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Select
    
    ActiveSheet.Paste
    Sheets("BENE").Select
    Else
        End If
    ActiveCell.Offset(1, 0).Select
    Loop
    [a1].Select
    
    Sheets("BENE CAT H").Select
    Cells.Columns.AutoFit
    Cells.Rows.AutoFit
    ActiveCell.Select
      Cells.Select
        Range("AL1").Activate
        Application.CutCopyMode = False
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
      Sheets("control").Select
     End Sub
    Last edited by E3iron; 02-10-2010 at 10:12 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Very long macro

    Hi,

    You'd be better advised to upload the workbook and some notes as to exactly what sheets you are wanting to use and what you're trying to achieve as output.

    It certainly looks as though the code could be simplified immensely. It may not even be necessary to use a loop to progress through cells in turn, a filtering function may be more appropriate. However even if a loop is required cutting out all the .Select and .Activate instructions, which I suspect are not needed, will speed up the code as well as switching off screen updating with Application.ScreenUpdating = False and probably temporarily putting the sheet(s) into manual calculation mode.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    09-09-2009
    Location
    Chatham, ON
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Very long macro

    I agree with Richard, when i first started out with macro's i had lots of
    Cells.Select
        Range("AL1").Activate
    in my code and stuff very similar that would slow it down and go from screen to screen causing it to take longer to run

    simply switching the above code to something along the lines of

    Range("AL1").Value
    or even
    Rows(1).Columns(38).value
    can start to speed up your run time i found.

    playing with stuff like that can slowly start to speed up your run time.

    Richard had a few other ideas that i will also be trying!

  4. #4
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Very long macro

    I have attached part of the spread sheet, in col BO the is cat description either cat A,B,C,D,E,F,G,H.

    I need group these in to there own sheet.
    Attached Files Attached Files

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Very long macro

    Hi,

    As I thought this could be simplified to a few lines of code using a filter rather than looping through cells.

    First name your data range on the BENE sheet with the name 'Data' defined as:
    =OFFSET(BENE!$A$1,0,0,COUNTA(BENE!A:A),71)
    Now use the following macro.

    Sub SplitSheets()
        Dim x As Integer
    
        For x = 1 To Worksheets.Count
            If Sheets(x).Name <> "BENE" Then
                Range("Data").AutoFilter Field:=67, Criteria1:=Right(Sheets(x).Name, 5)
                Range("Data").Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(x).Range("A2")
                Sheet4.ShowAllData
            End If
        Next x
    End Sub
    HTH

  6. #6
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Very long macro

    I've rename the data range on the Bene sheet data and i have created the macro but i'm getting the error 'Range' of object'_Global' failed

  7. #7
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Very long macro

    The number one tip I learned form this forum:
    Dont use ".Select". Change all the ".Select" to ".Activate"

    As an example: from your macro in the first post
     
    Sheets("BENE").Activate
    [BO2].Activate
    Do Until ActiveCell = ""
    If ActiveCell = "CAT A" Then
    ActiveCell.EntireRow.Copy
    Sheets("BENE CAT A").Activate
    [A65536].Activate
    Selection.End(xlUp).Activate
    If ActiveCell <> "" Then ActiveCell.Offset(1, 0).Activate
    I tried running your macro but could not get past:
        Selection.Sort Key1:=Range("AU2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    It generates an error on Excel 2k.
    Regards

    Rick
    Win10, Office 365

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Very long macro

    This the macro I would use, a slight tweak to Richard's:

    Option Explicit                                 'turn on code checking
    
    Sub SplitSheets()
    Dim LR As Long, x As Long                       'declare all variables
    Application.ScreenUpdating = False              'no screen flickering
    Sheets("BENE").Activate                         'start on the correct sheet
    LR = Range("A" & Rows.Count).End(xlUp).Row      'find the last row with data
    
        For x = 1 To Worksheets.Count               'go thru each existing worksheet one at a time
            If Sheets(x).Name <> "BENE" Then        'make sure it's not the data sheet
                Sheets(x).Cells.Clear               'clear existing report on that sheet
                                                    'Filter column BO by the sheetname
                Range("BO:BO").AutoFilter Field:=1, Criteria1:=Right(Sheets(x).Name, 5)
                                                    'Copy only visible data to sheet
                Range("A1:BR" & LR).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(x).Range("A1")
                Sheets(x).Columns.AutoFit           'adjust the appearance of the newly pasted data
            End If
        Next x                                      'next sheet
        
    ActiveSheet.AutoFilterMode = False              'turn off the filter
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 02-09-2010 at 12:59 PM. Reason: Added comments
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Very long macro

    Thanks Jb that works a treat, could you put some text in the code explaining how it works. I would like to understand.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Very long macro

    Comments added above into original macro.

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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