+ Reply to Thread
Results 1 to 6 of 6

Deleting columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-25-2011
    Location
    Donegal, Ireland
    MS-Off Ver
    Excel 2013
    Posts
    133

    Deleting columns

    Any one else bored today that can help with this simple issue. I need to be able to delete column 47 (AU) and its entire contents immediately after running this piece of code

    Sub Delete_Rows()
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("A1:AT150"), ActiveSheet.UsedRange)
    For Each cell In rng
    If (cell.Value) = "Grade-B" _
    Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireRow.Delete
    End Sub
    Maybe it could be added to this piece of code it that's any easier.

    I know it's Christmas but if you're bored like me then i'd appreciate your help.

    Regards
    John

  2. #2
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Deleting columns

    Try add

    Columns(47).Delete
    at the end of your code

  3. #3
    Forum Contributor
    Join Date
    04-25-2011
    Location
    Donegal, Ireland
    MS-Off Ver
    Excel 2013
    Posts
    133

    Re: Deleting columns

    Sorry i forgot to mention that the code runs in the second worksheet called 'new' and the column that needs to deleted is in the first worksheet called 'Desktop'

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Deleting columns

    Try this:

    Sub Delete_Rows()
    
        Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet  'may want to specify sheet
        Dim rng As Range, cell As Range, del As Range
        
        Set rng = Intersect(ws.Range("A1:AT150"), ws.UsedRange)
        
        For Each cell In rng
            If cell.Value = "Grade-B" Then
                If del Is Nothing Then
                    Set del = cell
                Else
                    Set del = Union(del, cell)
                End If
            End If
        Next cell
        
        If Not del Is Nothing Then del.EntireRow.Delete
        ws.Columns("AU").Delete
        
    End Sub
    Last edited by berlan; 12-25-2013 at 04:05 PM.

  5. #5
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Deleting columns

    In that case, try this:

    Sub Delete_Rows()
    
        Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("new")
        Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Desktop")
        Dim rng As Range, cell As Range, del As Range
        
        Set rng = Intersect(ws1.Range("A1:AT150"), ws1.UsedRange)
        
        For Each cell In rng
            If cell.Value = "Grade-B" Then
                If del Is Nothing Then
                    Set del = cell
                Else
                    Set del = Union(del, cell)
                End If
            End If
        Next cell
        
        If Not del Is Nothing Then del.EntireRow.Delete
        
        ws2.Columns("AU").Delete
        
    End Sub

  6. #6
    Forum Contributor
    Join Date
    04-25-2011
    Location
    Donegal, Ireland
    MS-Off Ver
    Excel 2013
    Posts
    133

    Re: Deleting columns

    Thanks to all, I used the code berlan provided and it did the trick. Many thx. (rep added)

+ 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. Replies: 12
    Last Post: 01-30-2014, 01:33 AM
  2. Macros - adding columns and deleting columns
    By ds0919 in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 04-22-2013, 07:52 AM
  3. Deleting Columns
    By lazenca in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-28-2008, 10:05 PM
  4. Combining Text from 2 Columns into 1 then Deleting the 2 Columns
    By sleepindogg in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-30-2006, 02:30 PM
  5. deleting columns
    By c_new in forum Excel General
    Replies: 4
    Last Post: 10-26-2005, 10:05 AM

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