+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    418

    Delete Column based on Column header

    Hello all,

    I have a spreadsheet with several columns (all column names are in row 1).

    I have 2 of column names. Note that their actual column# might change so the macro would have to look for the column name and NOT the column position.
    (i.e. Cloudy could be in Column "A" today and Column "D" next week.)

    Here are the 2 column names I need to macro...

    >Cloudy
    >Weather


    I would like Excel to macro this...

    1. Look for the bottom-most row with data in the "Cloudy" column and note that row #.

    2. Now, look at the same row # in the "Weather" column and note it.

    3. If that same (noted) row# in the "Weather" column has any data as the bottom-most with data in the "Cloudy" column, then delete the whole "Cloudy" column entirely.

    Thanks much
    Last edited by duugg; 05-01-2009 at 09:54 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    Try this, though I'm not entirely sure what you mean by
    3. If that same (noted) row# in the "Weather" column has any data as the bottom-most with data in the "Cloudy" column, then delete the whole "Cloudy" column entirely.
    Code:
    Sub x()
     
    Dim rFind1 As Range, rfind2 As Range
    
    With Sheet1.Rows(1)
        Set rFind1 = .Find(What:="Cloudy", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        Set rfind2 = .Find(What:="Weather", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rFind1 Is Nothing And Not rfind2 Is Nothing Then
            If Cells(Rows.Count, rFind1.Column).End(xlUp) _
                = Cells(Rows.Count, rfind2.Column).End(xlUp) Then Columns(rfind2.Column).Delete
        End If
    End With
            
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    04-18-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    256

    Thumbs up Re: Delete Column based on Column header

    There is a small change in Stephen's code. Using his code deletes the 'Weather' column instead of the 'Cloudy' column. Just make this small change in the last but 4th line:
    Code:
    = Cells(Rows.Count, rfind2.Column).End(xlUp) Then Columns(rfind1.Column).Delete
    Though even I am not sure about what you mean by:
    3. If that same (noted) row# in the "Weather" column has any data as the bottom-most with data in the "Cloudy" column, then delete the whole "Cloudy" column entirely.
    --Karan--

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    Thank you karan.

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

    Re: Delete Column based on Column header

    Thanks guys,

    Hmmm, nothing happened at all. Not sure why.

    The other thing I forgot to mention is that I would like to macro to do this on the active sheet as well and not sheet 1.

    I changed the line of code from


    Code:
    With Sheet1.Rows(1)
    to

    Code:
    With ActiveSheet.Rows(1)
    Was that part correct?



    As far as the macro, let me try and explain simpler. I realize it may have been a bit confusing (and it still may be)

    Let's say that there is data of some sort in each row of the "cloudy" column from row 2 down to row 100 (row 1 is the header row). Providing there is some sort of data in rows 2 to 100 row of the "weather" column, then delete the entire "cloudy" column.

    However, if there are any blank rows in the "cloudy" column between rows 2 and row 100, then don't even bother checking the "weather" column and leave the "cloudy" column alone.


    I hope that's a bit easier to explain

    Thanks much

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    That doesn't sound much like your original question. See if this works, still not quite sure...
    Code:
    Sub x()
     
    Dim rFind1 As Range, rfind2 As Range
    
    With Rows(1)
        Set rFind1 = .Find(What:="Cloudy", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        Set rfind2 = .Find(What:="Weather", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rFind1 Is Nothing And Not rfind2 Is Nothing Then
            If WorksheetFunction.CountBlank(Cells(1, rFind1.Column).Resize(99)) = 0 And _
                WorksheetFunction.CountBlank(Cells(1, rfind2.Column).Resize(99)) = 0 Then Columns(rFind1.Column).Delete
            End If
        End If
    End With
            
    End Sub

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

    Re: Delete Column based on Column header

    Sorry this one is so confusing. The error I got now says...

    "Compile Error"
    "End if without block if"


    I think this might be the best way to explain it....


    Basically, I just want to delete the "Cloudy" column if there are no blank cells equal to the bottom-most cell with data in the "Weather" column.



    I hope that makes it easier and thanks again. I won't be able to check this out right away but I do look forward to having this one figured out.

    Thanks for your patience.

  8. #8
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    For the error, you just need to remove one of the "End If" statements - my mistake.

    If that doesn't solve the problem, please can you attach a workbook illustrating what you want doing because your descriptions don't seem to me to marry up.

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

    Re: Delete Column based on Column header

    Thanks for all the help. Attached is a sample.

    I ran the "x" macro on sheet1. The "Cloudy" column wasn't deleted even though it had data in it down to the same row as the "Weather" column.

    I then tried running the "x" macro on sheet2, (just for kicks).

    In Sheet2, the "Cloudy" column DOES have blanks. So, in this case, the "Cloudy" column should NOT be deleted, which it wasn't.

    Thanks
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    I assumed, stupidly, that your data was in rows 1 to 100 as you said. The fact that it wasn't makes a difference.
    Code:
    Sub x()
     
    Dim rFind1 As Range, rfind2 As Range, n1 As Long, n2 As Long
    
    With Rows(1)
        Set rFind1 = .Find(What:="Cloudy", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        Set rfind2 = .Find(What:="Weather", LookIn:=xlFormulas, _
                              LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                              SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rFind1 Is Nothing And Not rfind2 Is Nothing Then
        n1 = Cells(Rows.Count, rFind1.Column).End(xlUp).Row
        n2 = Cells(Rows.Count, rfind2.Column).End(xlUp).Row
            If WorksheetFunction.CountBlank(Cells(1, rFind1.Column).Resize(n1)) = 0 And _
                WorksheetFunction.CountBlank(Cells(1, rfind2.Column).Resize(n2)) = 0 Then Columns(rFind1.Column).Delete
        End If
    End With
            
    End Sub

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

    Talking Re: Delete Column based on Column header

    Stephen,

    Thanks soooo much, that worked beautifully!

    My apologies for not making it clearer when I brought up the "100" row thing. In the process of trying to make easier to understand by mentioning a specific row number, I further confused the situation. It's all good now though.

    Thanks Again!

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,714

    Re: Delete Column based on Column header

    Glad we got there in the end!

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.2.0