+ Reply to Thread
Results 1 to 15 of 15

Sorting and deleting

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Unhappy Sorting and deleting

    Hello,

    I work with 3D Autocad Inventor software, every time we finish a project, we export BOM in excel format. This BOM has all parts even the ones we don`t need, so every time I need manually delete parts that start with P and R letters in their name.
    Is there any way I can automate this process using VB?

    Is this doable?

    Thanks,
    Alex.
    Attached Files Attached Files
    Last edited by achmidt; 11-16-2011 at 02:17 PM.

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Sorting and deleting

    If your names are in column D use this
    Sub aa()
    Dim last As Long
    Dim x As Long
    last = Cells(Rows.Count, "d").End(xlUp).Row
    For x = last To 2 Step -1
    If Cells(x, 4).Value Like "P*" Or Cells(x, 4).Value Like "R*" Then
    Cells(x, 4).EntireRow.Delete
    End If
    Next
    End Sub
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Forum Contributor
    Join Date
    11-04-2011
    Location
    pak
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: Sorting and deleting

    hello,
    do you want to delete complete row which has "P" or "R" in Item, description,material,stock ?

  4. #4
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Quote Originally Posted by shaukat View Post
    hello,
    do you want to delete complete row which has "P" or "R" in Item, description,material,stock ?
    Yes, delete complete row. Script above from Tom was what I needed. Thank you. Looking for how I can sort parts now.... do you know?

  5. #5
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Tom, I`m so sorry I`m a not really familiar with VBA, can you tell me where to add and how to call this sub in my spread sheet?

  6. #6
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Awesome! Thank you so much!!!

  7. #7
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Sorting and deleting

    press Alt + F 11 then insert module past my code and e.g. press F5

  8. #8
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Do you guys know if it is possible to sort automatically 1,2,3 etc after running "aa" sub?

  9. #9
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Sorting and deleting

    I am not sure I understand but if you want have numbers in column A like 1,2,3 etc try this:

    Sub aaa()
    Dim LastD As Long, x As Long
    Dim cell As Variant
    LastD = Cells(Rows.Count, "d").End(xlUp).Row
    
    For Each cell In Range("a2:a" & LastD)
    x = x + 1
    cell.Value = x
    Next
    End Sub

  10. #10
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Quote Originally Posted by tom1977 View Post
    I am not sure I understand but if you want have numbers in column A like 1,2,3 etc try this:

    Sub aaa()
    Dim LastD As Long, x As Long
    Dim cell As Variant
    LastD = Cells(Rows.Count, "d").End(xlUp).Row
    
    For Each cell In Range("a2:a" & LastD)
    x = x + 1
    cell.Value = x
    Next
    End Sub
    Tom please see attached file. After you run the macro ( I modified it a little bit) if you look at Item column you`ll see it starts from #3. I`m wondering how to assign Item #`s so #3 becomes #1 and so on...?
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Quote Originally Posted by tom1977 View Post
    I am not sure I understand but if you want have numbers in column A like 1,2,3 etc try this:

    Sub aaa()
    Dim LastD As Long, x As Long
    Dim cell As Variant
    LastD = Cells(Rows.Count, "d").End(xlUp).Row
    
    For Each cell In Range("a2:a" & LastD)
    x = x + 1
    cell.Value = x
    Next
    End Sub
    sorry wrong excel spreadsheet . the one attached is correct....
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Sorting and deleting

    try this code:
    Sub PARTSLISTBOM()
    '
    ' PARTSLISTBOM Macro
    ' FORMAT PARTS LIST BOM
    ' DELETES P AND R NUMBERS
    ' Keyboard Shortcut: Ctrl+Shift+P
    '
        Columns("A:A").EntireColumn.AutoFit
        Columns("B:B").EntireColumn.AutoFit
        Columns("C:C").EntireColumn.AutoFit
        Columns("D:D").EntireColumn.AutoFit
        Columns("E:E").EntireColumn.AutoFit
        Columns("F:F").EntireColumn.AutoFit
        Columns("G:G").Select
        Selection.Delete Shift:=xlToLeft
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("G1").Select
        Selection.NumberFormat = "General"
        ActiveCell.FormulaR1C1 = "SKID#"
        Range("H1").Select
        Selection.NumberFormat = "General"
        ActiveCell.FormulaR1C1 = "LOCATION"
        Range("H2").Select
        Columns("G:G").EntireColumn.AutoFit
        Columns("H:H").EntireColumn.AutoFit
        Columns("A:A").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.ColumnWidth = 11.57
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "OLD PART"
        Rows("1:1").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.RowHeight = 38.25
        Range("A1:J1").Select
        Columns("J:J").EntireColumn.AutoFit
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        Range("B:B,C:C,E:E").Select
        Range("E2").Activate
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        Range("F4").Select
        ActiveWindow.SmallScroll Down:=-9
        Range("B3").Select
    
    Dim last As Long
    Dim x As Long
    last = Cells(Rows.Count, "e").End(xlUp).Row
    For x = last To 3 Step -1
    If Cells(x, 5).Value Like "P*" Or Cells(x, 5).Value Like "R*" Or Cells(x, 5).Value Like "C*" Then
    Cells(x, 5).EntireRow.Delete
    End If
    Next
    Call aaa
    End Sub
    
    Sub aaa()
    Dim LastD As Long, x As Long
    Dim cell As Variant
    LastD = Cells(Rows.Count, "e").End(xlUp).Row
    
    For Each cell In Range("b3:b" & LastD)
    x = x + 1
    cell.Value = x
    Next
    End Sub

  13. #13
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    Genius!!! Thanks! Tom, at the risk of being a huge pain, do you mind if I`ll ask few more questions?

    1. How to format cells and add borders automatically ? see test3
    2. Is there any way to adjust break view so when I print it prints the entire sheet? see test3

    BOM can be 10 rows long or 200 rows long.
    Please see
    test2.xls - after running macros and VB script
    test3.xls - that what I need to be done in addition.

    Thanks in advance!
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Sorting and deleting

    for borders use this:

    Sub Borders()
        With Range("a1").CurrentRegion.Borders(xlEdgeLeft)
           .Weight = xlThin
        End With
        With Range("a1").CurrentRegion.Borders(xlEdgeTop)
           .Weight = xlThin
        End With
        With Range("a1").CurrentRegion.Borders(xlEdgeBottom)
            .Weight = xlThin
        End With
        With Range("a1").CurrentRegion.Borders(xlEdgeRight)
              .Weight = xlThin
        End With
        With Range("a1").CurrentRegion.Borders(xlInsideVertical)
              .Weight = xlThin
        End With
        With Range("a1").CurrentRegion.Borders(xlInsideHorizontal)
          .Weight = xlThin
        End With
          
    End Sub

  15. #15
    Registered User
    Join Date
    11-15-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Sorting and deleting

    figured out how to do break_page

    Sub page_break()
    '
    ' page_break Macro
    '
        ActiveWindow.View = xlPageBreakPreview
        ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
        ActiveWindow.View = xlNormalView
    End Sub

+ 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