+ 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
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    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
    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!)

  2. #2
    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

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

    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

  4. #4
    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

    Sorry Leith,

    I got Runtime error 13 Type mismatch

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

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

    Hello Duugg,

    I have had such bad luck answering your posts. Which line has the error?

  6. #6
    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,

    Line 25

    Thanks

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

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

    Hello Duugg,

    Thanks, found the problem. I used Rng instead of Cell. Here is the corrected and working code...
    Sub Macro1()
    
      Dim Cell As Range
      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 Cell.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