+ Reply to Thread
Results 1 to 3 of 3

Can I Improve This Code?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-25-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    34

    Can I Improve This Code?

    Hey guys,

    I've made a macro that works perfectly, but I'd like to improve the code (for educational purposes).

    I've got a macro that sets a filter to a value in a cell, then copies the range to a sheet with the same name as the filter. I've set up variables for each cell, but I want to loop through the copy rather than repeating it.

    Instead of repeating the code and changing Group1 to Group2, how can I use a loop to do this?

    Thanks,
    Lucas

    P.S Code below

    Dim Group1 As String
    Dim Group2 As String
    Dim Group3 As String
    Dim Group4 As String
    
    Application.ScreenUpdating = False
    
    
    Group1 = Worksheets("Lookup").Cells(3, "E").Value
    Group2 = Worksheets("Lookup").Cells(4, "E").Value
    Group3 = Worksheets("Lookup").Cells(5, "E").Value
    Group4 = Worksheets("Lookup").Cells(6, "E").Value
    
    ' Loop 1
    
        Sheets("PS Group 09-09-13").Select
        Range("A3").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.AutoFilter
        ActiveSheet.Range("$A$3:$AO$77772").AutoFilter Field:=41, Criteria1:= _
            Group1
        Sheets.Add.Name = Group1
        Sheets("PS Group 09-09-13").Select
        Range("A3").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.Copy
        Sheets(Group1).Select
        Range("A1").Select
        ActiveSheet.Paste
        Selection.Columns.AutoFit
        Sheets("PS Group 09-09-13").Select
        Application.CutCopyMode = False
        Range("AO3").Select

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Can I Improve This Code?

    It was challenging to tell what you were really trying to accomplish with all the "Selections" as it gives us no reference. Try this and let me know how close i got:

    Sub test1()
    Dim ws As Worksheet:    Set ws = Sheets("Lookup")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("PS Group 09-09-13")
    Dim icell As Long
    Dim GroupX As String
    
    Application.ScreenUpdating = False
    
    For icell = 3 To 6
        GroupX = ws.Range("E" & icell).Value
        ws2.Range("$A$3:$AO$77772").AutoFilter Field:=41, Criteria1:=GroupX
        Worksheets.Add(After:=Sheets(Worksheets.Count)).Name = GroupX
        ws2.Range("A3:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row).Copy Destination:=Sheets(GroupX).Range("A1")
    Next icell
    
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Registered User
    Join Date
    10-25-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Can I Improve This Code?

    Hey stnkynts,

    Great response mate. Although the code didn't quite work, I wasn't after a functioning bit of code anyway. What you demonstrated perfectly was the syntax of how to set the variables and loop through them. Now I can recreate my code with this technique. Not to mention can appropriate it for other uses!

    Thanks! Thread marked as solved

    Lucas

+ 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. Improve my code: If, or, then
    By Alex532 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2013, 07:19 PM
  2. [SOLVED] To improve Efficiency of code, code running too long
    By andywsw in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2012, 05:54 PM
  3. [SOLVED] Anyone help to improve my code?
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2006, 10:15 AM
  4. Re: Improve code
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM
  5. Improve code
    By Gareth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2005, 11:06 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