+ Reply to Thread
Results 1 to 8 of 8

help with macro - creating new columns/copying/creating new worksheets from default

Hybrid View

  1. #1
    Registered User
    Join Date
    09-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    50

    help with macro - creating new columns/copying/creating new worksheets from default

    have this macro, but the column names have changed in the excel, this macro scrolls by number of colums, how do i change to use column header names instead of number of columns ? also each time its run have to change date in macro, any way to make this selectable in excel before run macro ?


    Selection.AutoFilter
    ActiveWindow.SmallScroll ToRight:=12
    Selection.AutoFilter Field:=15, Criteria1:="=GE*", Operator:=xlAnd, _
    Criteria2:="<>GE Energy - Hydro"
    ActiveWindow.ScrollColumn = 1
    Cells.Select
    Selection.Copy
    Sheets.Add.Name = "Default Data"
    ActiveSheet.Paste
    Columns("A:AZ").Select
    Application.CutCopyMode = False
    Selection.ColumnWidth = 20
    Rows("1:1").Select
    Selection.AutoFilter
    Worksheets("Default").Delete

    ActiveWindow.SmallScroll ToRight:=1
    Selection.AutoFilter Field:=6, Criteria1:=">07/12/2010", Operator:=xlAnd, _
    Criteria2:="<07/19/2010"
    ActiveWindow.SmallScroll Down:=159
    ActiveWindow.ScrollRow = 1
    Selection.AutoFilter Field:=6, Criteria1:=">07/12/2010 0:00", Operator:= _
    xlAnd, Criteria2:="<07/19/2010 0:00"
    Cells.Select
    Range("B1").Activate
    Selection.Copy
    Sheets.Add.Name = "Percent OT"
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWindow.SmallScroll ToRight:=39
    Columns("A:AZ").Select
    Range("AZ1").Activate
    Selection.ColumnWidth = 20
    ActiveWindow.ScrollColumn = 1
    Range("D2").Select
    ActiveWindow.SmallScroll Down:=-6
    Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveWindow.SmallScroll ToRight:=14
    Selection.AutoFilter Field:=18, Criteria1:="<=0", Operator:=xlAnd
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.SmallScroll Down:=-6
    Sheets("Default Data").Select
    Rows("1:1").Select
    Range("B1").Activate
    Selection.AutoFilter
    Rows("1:1").Select
    Range("B1").Activate
    Selection.AutoFilter
    ActiveWindow.SmallScroll ToRight:=2
    Selection.AutoFilter Field:=5, Criteria1:="<>c", Operator:=xlAnd
    Selection.AutoFilter Field:=6, Criteria1:="<07/19/2010", Operator:=xlAnd
    Cells.Select
    Range("C1").Activate
    Selection.Copy
    Sheets.Add.Name = "Overdue"
    ActiveSheet.Paste
    ActiveWindow.SmallScroll ToRight:=45
    Columns("A:AZ").Select
    Range("AZ1").Activate
    Selection.ColumnWidth = 20
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.SmallScroll Down:=-3
    ActiveWindow.SmallScroll ToRight:=6
    Rows("1:1").Select
    Range("G1").Activate
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveWindow.SmallScroll ToRight:=14
    Range("R2").Select
    Selection.Sort Key1:=Range("R2"), Order1:=xlDescending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.SmallScroll Down:=-6
    Range("B2").Select
    Sheets("Default Data").Select
    Rows("1:1").Select
    Range("C1").Activate
    Selection.AutoFilter
    Selection.AutoFilter
    Columns("D:D").Select
    ActiveWindow.SmallScroll ToRight:=13
    Selection.AutoFilter Field:=17, Criteria1:=">07/12/2010", Operator:=xlAnd _
    , Criteria2:="<07/19/2010"
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.SmallScroll Down:=174
    ActiveWindow.ScrollRow = 1
    Cells.Select
    Selection.Copy
    Sheets.Add.Name = "Span"
    ActiveSheet.Paste
    ActiveWindow.ScrollColumn = 24
    ActiveWindow.SmallScroll ToRight:=17
    Columns("A:AZ").Select
    Range("AZ1").Activate
    Selection.ColumnWidth = 20
    ActiveWindow.ScrollColumn = 1
    Sheets("Default Data").Select
    Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    Selection.AutoFilter
    Selection.AutoFilter Field:=5, Criteria1:="<>c", Operator:=xlAnd
    Selection.AutoFilter Field:=6, Criteria1:=">07/19/2010", Operator:=xlAnd, _
    Criteria2:="<07/26/2010"
    Cells.Select
    Selection.Copy
    Sheets.Add.Name = "Lookahead"
    ActiveSheet.Paste
    ActiveWindow.ScrollColumn = 24
    ActiveWindow.SmallScroll ToRight:=16
    Columns("A:AZ").Select
    Range("AZ1").Activate
    Selection.ColumnWidth = 20
    ActiveWindow.ScrollColumn = 1
    Range("A1").Select

    Sheets("Default Data").Select
    Selection.AutoFilter
    Range("A1").Select
    Sheets("Span").Select
    Range("A1").Select
    Sheets("Overdue").Select
    Range("A1").Select
    Sheets("Percent OT").Select
    Range("A1").Select
    End Sub
    Last edited by vsantoro; 09-16-2010 at 10:08 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: help with macro

    it would help if you uploaded the workbook, or a representative sample, together with details about what columns have changed and how ... have you inserted columns, deleted columns, changed the headings, etc? Ideally, a before and after (current) copy.

    It's difficult to check the working without the structure and some typical data, and it's even harder to make that up.

    I'm guessing that you recorded this macro as you undertook whatever the task was and you've just repeated it each time since. Yes?

    Regards

  3. #3
    Registered User
    Join Date
    09-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    50

    Re: help with macro

    Quote Originally Posted by TMShucks View Post
    it would help if you uploaded the workbook, or a representative sample, together with details about what columns have changed and how ... have you inserted columns, deleted columns, changed the headings, etc? Ideally, a before and after (current) copy.

    It's difficult to check the working without the structure and some typical data, and it's even harder to make that up.

    I'm guessing that you recorded this macro as you undertook whatever the task was and you've just repeated it each time since. Yes?

    Regards

    Hi, this macro just gets repeated , when the xls is generated the data is different and macro is run against it. i uploaded the xls that is generated and the macro should be run against.
    Attached Files Attached Files

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: help with macro

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    09-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    50

    Re: help with macro

    Quote Originally Posted by royUK View Post
    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    sorry about that , will remember that next post. I have edited title so it is more specific.
    Last edited by vsantoro; 09-16-2010 at 10:22 AM.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: help with macro - creating new columns/copying/creating new worksheets from defau

    Sorry it's taken a while to reply; I've spent some time working on your current code.

    With regard to filtering based on the column headings, one approach is the following function which will accept a column header and a range and return a column number using the MATCH function.

    Function fMatchCol(sSearch As String, rSearchRange As Range) As Long
    ' UDF to find filter column based on column header
    Dim AWF As WorksheetFunction
    Set AWF = Application.WorksheetFunction
    Dim lMatchCol As Long
    lMatchCol = 0
    On Error Resume Next
    lMatchCol = AWF.Match(sSearch, rSearchRange, 0)
    On Error GoTo 0
    fMatchCol = lMatchCol
    End Function

    The subroutine following is a simple test of the function:

    Private Sub TestMatch()
    ' Test fMatchCol function
    Debug.Print fMatchCol("Creator Company", Rows("1:1"))
    MsgBox fMatchCol("Creator Company", Rows("1:1"))
    End Sub

    An example of how this is used:

    ' Filter on Column 7 (G) = Due By
    lFilterField1 = fMatchCol("Due By", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:=">07/12/2010 0:00", _
            Operator:=xlAnd, _
            Criteria2:="<07/19/2010 0:00"
    End With

    As I said, I've amended the code; see below. I have removed all the scrolling and most of the selecting and generally tidied it up. I have adjusted the filter columns where it was, or seemed, obvious but I couldn't guess at some of them.

    Finally, rather than hard coding dates in the body of the code, I would put them on a "control sheet" and initialise variables as the first stage of the macro execution. Alternatively, you could define some variables at the beginning (top) of your code and change the values there once. That would save ploughing through all the code ... too easy to miss something.

    The comments serve to "self document" the code

    Hope this helps.

    Regards


    '
    ' Amended code
    '
    
    Sub Test()
    '
    ' Improved/amended code
    '
    ' help with macro - creating new columns/copying/creating new worksheets from default
    ' http://www.excelforum.com/excel-prog...m-default.html
    '
    
    Dim lFilterField1 As Long
    Dim lFilterField2 As Long
    
    Debug.Print "Test Start " & Now()
    Application.ScreenUpdating = False
    
    ' Step 1 - Filter and copy
    ' Filter on Column 13 (M) = Creator Company
    lFilterField1 = fMatchCol("Creator Company", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:="=GE*", _
            Operator:=xlAnd, _
            Criteria2:="<>GE Energy - Hydro"
    End With
    Cells.Copy
    
    ' Step 2 - Create new sheet (Default Data)
    Sheets.Add.Name = "Default Data"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AZ").ColumnWidth = 20
    
    ' Step 3 - Delete original data
    ' Note *** commented out for testing purposes ***
    ' Worksheets("Default").Delete
    
    ' Step 4 - Filter and copy (Default Data)
    ' Filter on Column 7 (G) = Due By
    lFilterField1 = fMatchCol("Due By", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:=">07/12/2010 0:00", _
            Operator:=xlAnd, _
            Criteria2:="<07/19/2010 0:00"
    End With
    Cells.Copy
    
    ' Step 5 - Create new sheet (Percent OT)
    Sheets.Add.Name = "Percent OT"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AZ").ColumnWidth = 20
    ' Filter on Column 16 (P) = Delta (in days)
    lFilterField1 = fMatchCol("Delta (in days)", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:="<=0", _
            Operator:=xlAnd
    End With
    
    ' Step 6 - Filter and copy (Default Data)
    Sheets("Default Data").Select
    ' Filter on Column 5? (?) = ?
    ' Filter on Column 7 (G) = Due By
    lFilterField1 = fMatchCol("x?x", Rows("1:1"))
    lFilterField1 = 5   ' on the basis I have no idea what should be filtered
    lFilterField2 = fMatchCol("Due By", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:="<>c", _
            Operator:=xlAnd
        .AutoFilter _
            Field:=lFilterField2, _
            Criteria1:="<07/19/2010", _
            Operator:=xlAnd
    End With
    Cells.Copy
    
    ' Step 7- Create new sheet (Overdue)
    Sheets.Add.Name = "Overdue"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AZ").ColumnWidth = 20
    Rows("1:1").AutoFilter
    Range("R2").Sort _
        Key1:=Range("R2"), _
        Order1:=xlDescending, _
        Header:=xlGuess, _
        OrderCustom:=1, _
        MatchCase:=False, _
        Orientation:=xlTopToBottom
    
    ' Step 8 - Filter and copy (Default Data)
    Sheets("Default Data").Select
    ' Filter on Column 17? (?) = ?
    lFilterField1 = fMatchCol("x?x", Rows("1:1"))
    lFilterField1 = 17   ' on the basis I have no idea what should be filtered
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:=">07/12/2010", _
            Operator:=xlAnd, _
            Criteria2:="<07/19/2010"
    End With
    Cells.Copy
    
    ' Step 9 - Create new sheet (Span)
    Sheets.Add.Name = "Span"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AZ").ColumnWidth = 20
    
    ' Step 10 - Filter and copy (Default Data)
    Sheets("Default Data").Select
    ' Filter on Column 5? (?) = ?
    ' Filter on Column 7 (G) = Due By
    lFilterField1 = fMatchCol("x?x", Rows("1:1"))
    lFilterField1 = 5   ' on the basis I have no idea what should be filtered
    lFilterField2 = fMatchCol("Due By", Rows("1:1"))
    With Rows("1:1")
        If Not ActiveSheet.AutoFilterMode Then
            .AutoFilter
        End If
        .AutoFilter _
            Field:=lFilterField1, _
            Criteria1:="<>c", _
            Operator:=xlAnd
        .AutoFilter _
            Field:=lFilterField2, _
            Criteria1:=">07/19/2010", _
            Operator:=xlAnd, _
            Criteria2:="<07/26/2010"
    End With
    Cells.Copy
    
    ' Step 11 - Create new sheet (Lookahead)
    Sheets.Add.Name = "Lookahead"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:AZ").ColumnWidth = 20
    
    ' Step 12 - Select sheet(s)
    Sheets("Default Data").Select
        Range("A1").Select
        If Not ActiveSheet.AutoFilterMode Then
            Range("A1").AutoFilter
        End If
    Sheets("Span").Select
        Range("A1").Select
        If Not ActiveSheet.AutoFilterMode Then
            Range("A1").AutoFilter
        End If
    Sheets("Overdue").Select
        Range("A1").Select
        If Not ActiveSheet.AutoFilterMode Then
            Range("A1").AutoFilter
        End If
    Sheets("Percent OT").Select
        Range("A1").Select
        If Not ActiveSheet.AutoFilterMode Then
            Range("A1").AutoFilter
        End If
    
    Application.ScreenUpdating = True
    Debug.Print "Test End " & Now()
    End Sub
    
    Function fMatchCol(sSearch As String, rSearchRange As Range) As Long
    ' UDF to find filter column based on column header
    Dim AWF As WorksheetFunction
    Set AWF = Application.WorksheetFunction
    Dim lMatchCol As Long
    lMatchCol = 0
    On Error Resume Next
    lMatchCol = AWF.Match(sSearch, rSearchRange, 0)
    On Error GoTo 0
    fMatchCol = lMatchCol
    End Function

    Enjoy!

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: help with macro - creating new columns/copying/creating new worksheets from defau

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: help with macro - creating new columns/copying/creating new worksheets from defau

    @RoyUK Sorry, which bit of code wasn't tagged ?

+ 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