Results 1 to 13 of 13

Delete all columns if value in row 2 of that column is x?

Threaded View

  1. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Delete all columns if value in row 2 of that column is x?

    Hi, Vikas_Gautam,

    place an x in 2 cells like C and D and the value in C will stay after your suggestion. Deletion sould either be done from the bottom to the top (or the right to lthe left) or by using a range object and deleting the big range at the end:

    Sub SampleOne()
    Dim cell As Range
    Dim rngBig As Range
    For Each cell In Range("A2:" & Cells(2, Columns.Count).End(xlToLeft).Address(False, False))
      If cell.Value = "x" Then
        If rngBig Is Nothing Then
          Set rngBig = cell
        Else
          Set rngBig = Union(rngBig, cell)
        End If
      End If
    Next cell
    If Not rngBig Is Nothing Then
      rngBig.EntireColumn.Delete
      Set rngBig = Nothing
    End If
    End Sub
    Sub SampleTwo()
    Dim lngCol As Long
    For lngCol = Cells(2, Columns.Count).End(xlToLeft).Column To 1 Step -1
      With Cells(2, lngCol)
        If .Value = "x" Then
          .EntireColumn.Delete
        End If
      End With
    Next lngCol
    End Sub
    [ Edit ]And of course ClearContents will work properly while Delete won´t..[ /Edit]

    Ciao,
    Holger
    Last edited by HaHoBe; 08-14-2014 at 12:11 AM.
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If column contains certain header, delete column--exempt certain columns
    By kestefon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2013, 04:04 PM
  2. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  3. Need help - Delete columns based on column name
    By amitvba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2011, 04:37 PM
  4. Delete columns where the last row in the column = 0
    By Melyssa18 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2010, 01:09 AM
  5. Delete multiple columns by column name no criteria need, just delete them
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2009, 10:40 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