Consulting

Results 1 to 12 of 12

Thread: VbCode changes need review

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    VbCode changes need review

    I found this code in this post. http://www.vbaexpress.com/forum/show...+another+sheet

    [vba]
    Sub FilterAndCopy()
    Dim lastRow As Long, lngRow As Long
    Dim coll As Collection
    Dim collItem As Variant
    Dim copyR As Range
    Application.ScreenUpdating = False
    With Worksheets("Main")
    If .AutoFilterMode = True Then .AutoFilterMode = False
    lastRow = .Range("D" & .Rows.count).End(xlUp).Row

    'Fill collection with unique Descriptions
    Set coll = New Collection
    On Error Resume Next
    For lngRow = 6 To lastRow
    coll.Add .Range("D" & lngRow).Value, .Range("D" & lngRow).Value
    Next 'lngRow
    If Err Then Err.Clear
    On Error Goto 0

    'For each Description copy the data
    For Each collItem In coll
    If WorksheetExists(collItem) Then
    Worksheets(collItem).UsedRange.EntireRow.Delete
    'Show only this Description's data
    .Range("A5").AutoFilter Field:=4, Criteria1:=collItem
    Set copyR = .UsedRange
    copyR.Copy Worksheets(collItem).Range("A5")
    With Worksheets(collItem)
    .UsedRange.Value = .UsedRange.Value
    lastRow = .UsedRange.Row + .UsedRange.Rows.count - 1
    .Rows(lastRow).Copy .Range("A" & lastRow + 1)
    .Rows(lastRow + 1).ClearContents
    .Range("E" & lastRow + 1).Value = WorksheetFunction.Sum(.Range("E6:E" & lastRow))
    .Range("F" & lastRow + 1).Value = WorksheetFunction.Sum(.Range("F6:F" & lastRow))
    .Range("G" & lastRow + 1).Value = WorksheetFunction.Sum(.Range("G6:G" & lastRow))
    End With
    'Show all data
    .Range("A5").AutoFilter
    Else
    MsgBox "Worksheet was not found for description '" & collItem & "'"
    End If
    Next 'collItem
    End With
    Application.ScreenUpdating = True
    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function WorksheetExists(ByVal strSheetName As String) As Boolean
    On Error Resume Next
    WorksheetExists = (Not Worksheets(strSheetName) Is Nothing)
    End Function
    [/vba]

    I keep getting an error and it highlights this

    [vba].Range("A5").AutoFilter Field:=4, Criteria1:=collItem[/vba]

    It wouldn't work for me so I made a minor change that seems to work, but would like to see if anyone see's why it didn't work the original way, otherwise if anyone see's how the change I made can impact something somewhere done the road.

    With changes.

    [vba]Sub FilterAndCopy2()
    Dim lastRow As Long, lngRow As Long
    Dim coll As Collection
    Dim collItem As Variant
    Dim copyR As Range
    Application.ScreenUpdating = False
    With Worksheets("Master")
    If .AutoFilterMode = True Then .AutoFilterMode = False
    lastRow = .Range("D" & .Rows.Count).End(xlUp).Row

    'Fill collection with unique Descriptions
    Set coll = New Collection
    On Error Resume Next
    For lngRow = 3 To lastRow
    coll.Add .Range("D" & lngRow).Value, .Range("D" & lngRow).Value
    Next 'lngRow
    If Err Then Err.Clear
    On Error GoTo 0

    'For each Description copy the data
    For Each collItem In coll
    If WorksheetExists(collItem) Then
    Worksheets(collItem).UsedRange.EntireRow.Delete
    'Show only this Description's data
    Rows("2").AutoFilter Field:=4, Criteria1:=collItem

    Set copyR = .UsedRange
    copyR.Copy Worksheets(collItem).Range("A2")
    With Worksheets(collItem)
    .UsedRange.Value = .UsedRange.Value
    lastRow = .UsedRange.Row + .UsedRange.Rows.Count - 1
    .Rows(lastRow).Copy .Range("A" & lastRow + 1)
    .Rows(lastRow + 1).ClearContents
    End With
    'Show all data
    .Range("A2").AutoFilter
    Else
    MsgBox "Worksheet was not found for description '" & collItem & "'"
    End If
    Next 'collItem
    End With
    Application.ScreenUpdating = True
    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function WorksheetExists(ByVal strSheetName As String) As Boolean
    On Error Resume Next
    WorksheetExists = (Not Worksheets(strSheetName) Is Nothing)
    End Function
    [/vba]

    Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Range A5 is the row where the data begins on the main sheet and the column being filtered by is column 4.......
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    No the data begins in A2 and I want it filtered by Column 4. A5 is from the post I found I made the change to A2 in the script after.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm just saying that you changed it to:
    [VBA]Rows("2").AutoFilter Field:=4, Criteria1:=collItem [/VBA]

    I would try:

    [VBA]
    .Range("A2").AutoFilter Field:=4, Criteria1:=collItem
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    That's what I originally tried but it would fail.
    It gives me a

    Run-time error '1004':
    Autofilter method of Range class failed

    That's when I tried the Row("2") and that worked.

  6. #6
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Now i am using Excel 2007. Could that cause a problem?

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Can you post a sample workbook with the code?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Here is the file. I apologize for the late reply.

  9. #9
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    I also noticed that the macro removes the data in Sheet "Monitor". Any idea why that would do that?

  10. #10
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    bump!

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Someone with 2007 will have to help you get any further with this....I don't have it.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    BUMP!

Posting Permissions

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