+ Reply to Thread
Results 1 to 12 of 12

Find the column named "Purple" & delete the whole column if there are no blank cells

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Find the column named "Purple" & delete the whole column if there are no blank cells

    Hi all,

    Okay, I'd like a macro that does 3 things..

    1. Find the last row (cell) of data in the "Customer Number" column. This search should be by the name "Customer Number" rather than by column letter because the column that "Customer Number" will be in can change.

    2. Find the column named "Purple" (also by name for same reason)

    3. If the "Purple" column has no blank cells in those same number of rows as the "Customer Number" column, delete the whole "Purple" column.

    Thanks much

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find the column named "Purple" & delete the whole column if there are no blank ce

    Hello duugg,

    You're full of questions today. Are these column names in row 1?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Find the column named "Purple" & delete the whole column if there are no blank ce

    LOL,

    Yes

    Thanks

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find the column named "Purple" & delete the whole column if there are no blank ce

    Hello Duugg,

    This macro finds the columns, and makes the "Purple" column the same size as "Customer Number" column. The "Purple" column is then checked for any blanks. If any blanks are found then the entire column is deleted.
    Sub Macro1()
    
      Dim Col As Long
      Dim LastCol As Long
      Dim Rng As Range
      Dim Rng1 As Range
      Dim Rng2 As Range
    
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        
        Set Rng = Range("A1", Cells(1, LastCol))
          
        For Each Cell In Rng
          Col = Col + 1
          Select Case Rng.Value
            Case Is = "Customer Number"
              Set Rng1 = Cells(Rows.Count, Col).End(xlUp)
            Case Is = "Purple"
              Set Rng2 = Cells(Rows.Count, Col).End(xlUp)
              Set Rng2 = Resize(Rng1.Rows.Count, 1)
          End Select
        Next Cell
        
        If Rng1 Is Nothing Then
           MsgBox "Customer Number column not found."
           Exit Sub
        End If
        
        If Rng2 Is Nothing Then
           MsgBox "Purple column not found."
           Exit Sub
        End If
        
        On Error Resume Next
          Col = Rng2.SpecialCells(xlCellTypeBlanks).Count
          If Err = 0 Then Rng2.EntireColumn.Delete
          
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Find the column named "Purple" & delete the whole column if there are no blank ce

    Leith,

    I got this error

    Compile error:

    Sub or Function not defined

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find the column named "Purple" & delete the whole column if there are no blank ce

    Hello Duugg,

    It appears I left off a qualifier on the Resize method.
    Sub Macro1()
    
      Dim Col As Long
      Dim LastCol As Long
      Dim Rng As Range
      Dim Rng1 As Range
      Dim Rng2 As Range
    
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        
        Set Rng = Range("A1", Cells(1, LastCol))
          
        For Each Cell In Rng
          Col = Col + 1
          Select Case Rng.Value
            Case Is = "Customer Number"
              Set Rng1 = Cells(Rows.Count, Col).End(xlUp)
            Case Is = "Purple"
              Set Rng2 = Cells(Rows.Count, Col).End(xlUp)
              Set Rng2 = Rng2.Resize(Rng1.Rows.Count, 1)
          End Select
        Next Cell
        
        If Rng1 Is Nothing Then
           MsgBox "Customer Number column not found."
           Exit Sub
        End If
        
        If Rng2 Is Nothing Then
           MsgBox "Purple column not found."
           Exit Sub
        End If
        
        On Error Resume Next
          Col = Rng2.SpecialCells(xlCellTypeBlanks).Count
          If Err = 0 Then Rng2.EntireColumn.Delete
          
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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