+ Reply to Thread
Page 3 of 3 FirstFirst 123
Results 31 to 44 of 44

Thread: Macros for multiple files

  1. #31
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hold on a moment...have a problem with the code I just posted.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  2. #32
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regan

    Try this...has to rewrite the Filter Routine
    Option Explicit
    Sub Combine_Days()
        Dim LR As Long
        Dim dayCol As String
        Dim dataCol As String
        Dim i As Long
        Dim x As Long
        Dim Rng As Range
        Application.ScreenUpdating = False
        dayCol = ColumnLetter(WorksheetFunction.Match("Number of Days", Rows("1:1"), 0))
        dataCol = ColumnLetter(WorksheetFunction.Match("Data", Rows("1:1"), 0))
        With ActiveSheet
            LR = .Range(dayCol & .Rows.Count).End(xlUp).Row
            'filter unwanted records
            .Range(dayCol & "1:" & dayCol & LR).AutoFilter Field:=1, Criteria1:= _
                    "=1-2 days", Operator:=xlOr, Criteria2:="="
            Set Rng = .AutoFilter.Range
            With Rng
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
                If x > 1 Then
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
            End With
            .AutoFilterMode = False
        End With
        LR = Range(dayCol & Rows.Count).End(xlUp).Row
        Set Rng = Range(dayCol & "2:" & dayCol & LR)
        With Rng
            For i = LR To 1 Step -1
                    If Rng(i).Value = "2 days" And Rng(i).Offset(-1, 0) = "1 day" Then
                        Rng(i).Offset(1, 0).EntireRow.Insert
                        Rng(i).EntireRow.Copy
                        Range("A" & Rng(i).Row).EntireRow.Copy
                        Range("A" & Rng(i).Row).Offset(1, 0).PasteSpecial
                        Range(dayCol & Rng(i).Row).Offset(1, 0).Value = "1-2 days"
                        ' allow N/A & LNE records to be processed
                        On Error Resume Next
                        Range(dataCol & Rng(i).Row).Offset(1, 0).Value = Application.WorksheetFunction.Sum _
                                (Range(dataCol & Rng(i).Row).Offset(0, 0).Value, Range(dataCol & Rng(i).Row).Offset(-1, 0).Value)
                        On Error GoTo 0
                        Rng(i).EntireRow.Delete
                        Rng(i).Offset(-1, 0).EntireRow.Delete
                    End If
                Next
            End With
            Application.ScreenUpdating = True
        End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #33
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    It's highlighting 'ColumnLetter' in the following line of code:
    dayCol = ColumnLetter(WorksheetFunction.Match("Number of Days", Rows("1:1"), 0))

    And this is the error message that appears: Compile error: Sub or Function not defined

  4. #34
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regon
    Sorry, I didn't include the Function in the last code I posted. Copy this code to the same module immediately after the Combine_Days routine.
    Function ColumnLetter(ColumnNumber As Long) As String
    ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #35
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    Oh my gosh, you are a genius! Seriously, this is amazing. It works on two thirds of my files - the other files have a different layout though, so this is to be expected. You have already done so much already, so you don't have to work on this too, but I was wondering if I can just amend the code to work on the remaining files.

    In the files, I want to combine the '4-6 times' and '7 or more times' rows into '4 Times or More'
    And combine the '2 times' and '3 times' into '2-3 Times'

    The file has the following headers:
    Grade_Level Gender Number of Times Data Location Time_Frame Location_Code

    I tried amending the code myself, but it didn’t work. Can you tell what I am doing wrong? And again, thank you so much for your help!

    Option Explicit
    Sub Combine_Days()
        Dim LR As Long
        Dim timeCol As String
        Dim dataCol As String
        Dim i As Long
        Dim x As Long
        Dim Rng As Range
        Application.ScreenUpdating = False
        timeCol = ColumnLetter(WorksheetFunction.Match("Number of Times", Rows("1:1"), 0))
        dataCol = ColumnLetter(WorksheetFunction.Match("Data", Rows("1:1"), 0))
        With ActiveSheet
            LR = .Range(timeCol & .Rows.Count).End(xlUp).Row
            'filter unwanted records
            .Range(timeCol & "4-6 times:" & timeCol & LR).AutoFilter Field:=1, Criteria1:= _
                    "=4 Times or More", Operator:=xlOr, Criteria2:="="
            Set Rng = .AutoFilter.Range
            With Rng
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
                If x > 1 Then
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
            End With
            .AutoFilterMode = False
        End With
        LR = Range(timeCol & Rows.Count).End(xlUp).Row
        Set Rng = Range(timeCol & "2:" & timeCol & LR)
        With Rng
            For i = LR To 1 Step -1
                    If Rng(i).Value = "7 or more times" And Rng(i).Offset(-1, 0) = "4-6 times" Then
                        Rng(i).Offset(1, 0).EntireRow.Insert
                        Rng(i).EntireRow.Copy
                        Range("A" & Rng(i).Row).EntireRow.Copy
                        Range("A" & Rng(i).Row).Offset(1, 0).PasteSpecial
                        Range(timeCol & Rng(i).Row).Offset(1, 0).Value = "4 Times or More"
                        ' allow N/A & LNE records to be processed
                        On Error Resume Next
                        Range(dataCol & Rng(i).Row).Offset(1, 0).Value = Application.WorksheetFunction.Sum _
                                (Range(dataCol & Rng(i).Row).Offset(0, 0).Value, Range(dataCol & Rng(i).Row).Offset(-1, 0).Value)
                        On Error GoTo 0
                        Rng(i).EntireRow.Delete
                        Rng(i).Offset(-1, 0).EntireRow.Delete
                    End If
                Next
            End With
            Application.ScreenUpdating = True
        End Sub
        
        Function ColumnLetter(ColumnNumber As Long) As String ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function
    Last edited by reganfoust; 02-08-2012 at 05:33 PM.

  6. #36
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regon
    First thing you GOTTA DO...always use code tags around your code or you'll get hammered by the Moderators (see Rule #3). To add code tags :

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing.
    Select your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced
    to see the [#] button). Highlight your code then press the [#] button. The result will appear like this in the post window:

    your code here ...
    and here ...
    and here
    In the meantime, I'll look at this. I may need a file.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  7. #37
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    So sorry about that - thanks for letting me know. I will definitely do that in the future. Let me know if you need any files etc. to help!

  8. #38
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regan
    Nah, you can't wait until tomorrow. You need to edit your post #35 to include Code Tags. To do so:
    press Edit Post, press Go Advanced to see the [#] button). Highlight your code then press the [#] button. Press Save Changes. The result will appear like this in the post window:

    your code here ...
    and here ...
    and here
    Not my rules Bud...but they make sense.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  9. #39
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    Sorry about that! It should be resolved now.

  10. #40
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regan
    That looks much better! Thanks.
    Of course, I can't test the code but this line jumps out
    With ActiveSheet
            LR = .Range(timeCol & .Rows.Count).End(xlUp).Row
            'filter unwanted records
            .Range(timeCol & "4-6 times:" & timeCol & LR).AutoFilter Field:=1, Criteria1:= _
                    "=4 Times or More", Operator:=xlOr, Criteria2:="="    
        Set Rng = .AutoFilter.Range
            With Rng
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
                If x > 1 Then
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
            End With
            .AutoFilterMode = False
        End With
    Take a look at that line compared to the original code, specifically at
    timeCol & "4-6 times:"  Which is meaningless.
    Whereas the original code was
    .Range(dayCol & "1:"
    Your code should probably be
    .Range(timeCol & "1:"
    Can't guarantee this'll fix it but give it a try. Let me know how you make out.

    FYI: This could probably all be incorporated into one macro that'll work on ALL files. If you're interested, send me a copy of the most recent file. You have my email address.
    Last edited by jaslake; 02-08-2012 at 06:04 PM. Reason: add fyi
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  11. #41
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    The first section (combining 2 times and 3 times into 2-3 Times) worked (yay!), however when I appended the second macro that combines 4-6 times and 7 or more times to 4 Times or More below it, it says that I have a Compile Error: Ambiguous Name detected: ColumnLetter on the second time this appears in the code.

    It would be great if I could have all of this in one macro so that I would only have to run it once. Happy to take a work-around though


    Option Explicit
    Sub Combine_Days1()
        Dim LR As Long
        Dim timeCol As String
        Dim dataCol As String
        Dim i As Long
        Dim x As Long
        Dim Rng As Range
        Application.ScreenUpdating = False
        timeCol = ColumnLetter(WorksheetFunction.Match("Number of Times", Rows("1:1"), 0))
        dataCol = ColumnLetter(WorksheetFunction.Match("Data", Rows("1:1"), 0))
        With ActiveSheet
            LR = .Range(timeCol & .Rows.Count).End(xlUp).Row
            'filter unwanted records
            .Range(timeCol & "1:" & timeCol & LR).AutoFilter Field:=1, Criteria1:= _
                    "=2-3 Times", Operator:=xlOr, Criteria2:="="
            Set Rng = .AutoFilter.Range
            With Rng
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
                If x > 1 Then
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
            End With
            .AutoFilterMode = False
        End With
        LR = Range(timeCol & Rows.Count).End(xlUp).Row
        Set Rng = Range(timeCol & "2:" & timeCol & LR)
        With Rng
            For i = LR To 1 Step -1
                    If Rng(i).Value = "3 times" And Rng(i).Offset(-1, 0) = "2 times" Then
                        Rng(i).Offset(1, 0).EntireRow.Insert
                        Rng(i).EntireRow.Copy
                        Range("A" & Rng(i).Row).EntireRow.Copy
                        Range("A" & Rng(i).Row).Offset(1, 0).PasteSpecial
                        Range(timeCol & Rng(i).Row).Offset(1, 0).Value = "2-3 Times"
                        ' allow N/A & LNE records to be processed
                        On Error Resume Next
                        Range(dataCol & Rng(i).Row).Offset(1, 0).Value = Application.WorksheetFunction.Sum _
                                (Range(dataCol & Rng(i).Row).Offset(0, 0).Value, Range(dataCol & Rng(i).Row).Offset(-1, 0).Value)
                        On Error GoTo 0
                        Rng(i).EntireRow.Delete
                        Rng(i).Offset(-1, 0).EntireRow.Delete
                    End If
                Next
            End With
            Application.ScreenUpdating = True
        End Sub
        
        Function ColumnLetter(ColumnNumber As Long) As String ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function
    
    Sub Combine_Days2()
        Dim LR As Long
        Dim timeCol As String
        Dim dataCol As String
        Dim i As Long
        Dim x As Long
        Dim Rng As Range
        Application.ScreenUpdating = False
        timeCol = ColumnLetter(WorksheetFunction.Match("Number of Times", Rows("1:1"), 0))
        dataCol = ColumnLetter(WorksheetFunction.Match("Data", Rows("1:1"), 0))
        With ActiveSheet
            LR = .Range(timeCol & .Rows.Count).End(xlUp).Row
            'filter unwanted records
            .Range(timeCol & "1:" & timeCol & LR).AutoFilter Field:=1, Criteria1:= _
                    "=4 Times or More", Operator:=xlOr, Criteria2:="="
            Set Rng = .AutoFilter.Range
            With Rng
                x = Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
                If x > 1 Then
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
            End With
            .AutoFilterMode = False
        End With
        LR = Range(timeCol & Rows.Count).End(xlUp).Row
        Set Rng = Range(timeCol & "2:" & timeCol & LR)
        With Rng
            For i = LR To 1 Step -1
                    If Rng(i).Value = "7 or more times" And Rng(i).Offset(-1, 0) = "4-6 times" Then
                        Rng(i).Offset(1, 0).EntireRow.Insert
                        Rng(i).EntireRow.Copy
                        Range("A" & Rng(i).Row).EntireRow.Copy
                        Range("A" & Rng(i).Row).Offset(1, 0).PasteSpecial
                        Range(timeCol & Rng(i).Row).Offset(1, 0).Value = "4 Times or More"
                        ' allow N/A & LNE records to be processed
                        On Error Resume Next
                        Range(dataCol & Rng(i).Row).Offset(1, 0).Value = Application.WorksheetFunction.Sum _
                                (Range(dataCol & Rng(i).Row).Offset(0, 0).Value, Range(dataCol & Rng(i).Row).Offset(-1, 0).Value)
                        On Error GoTo 0
                        Rng(i).EntireRow.Delete
                        Rng(i).Offset(-1, 0).EntireRow.Delete
                    End If
                Next
            End With
            Application.ScreenUpdating = True
        End Sub
        
        Function ColumnLetter(ColumnNumber As Long) As String ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function

  12. #42
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Include the Function Code ONLY once...you have two copies of it.
     Function ColumnLetter(ColumnNumber As Long) As String ' From http://www.craigmurphy.com/blog/?p=150
    ' Works in Excel 2007
        Dim ColNum As Integer
        Dim ColLetters As String
        ColNum = ColumnNumber
        ColLetters = ""
        Do
            ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
            ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
        Loop While ColNum > 0
        ColumnLetter = ColLetters
    End Function
    Get rid of either one...matters not which.

    We'll work on this after you've played with things...perhaps you will have done most of the coding
    It would be great if I could have all of this in one macro so that I would only have to run it once. Happy to take a work-around though
    Last edited by jaslake; 02-08-2012 at 08:47 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  13. #43
    Registered User
    Join Date
    02-06-2012
    Location
    Palo Alto, CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Macros for multiple files

    Well, you're awesome! It worked. You have officially solved all of my problems Thank again for your hard work, patience, and guidance.

  14. #44
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Macros for multiple files

    Hi Regan

    You're welcome...glad I could be of help. If that satisfies your need, I'd appreciate it if you'll please mark your thread as "Solved".

    To mark your thread solved do the following:
    - Go to your first post on the thread
    - Click edit
    - Click Advance
    - Just below the word "Title:" you will see a dropdown with the word No prefix.
    - Change to Solved
    - Click Save
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ 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.2.0