+ Reply to Thread
Results 1 to 7 of 7

If a specific row contains a word, then clear specific content in corresponding column.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2014
    Location
    Uppsala, Sweden
    MS-Off Ver
    2010
    Posts
    43

    If a specific row contains a word, then clear specific content in corresponding column.

    Hi,

    First post on the forum, so let's see how it goes.

    I have a row (will always be row 3) where each cell contains a day of the week, the days repeats for a year or so, making the row almost 400 cells.

    Like this,
    Mo - Tu - We - Th - Fr - Sa - Su - Mo - Tu - We - Th - Fr - Sa - Su - and so on...

    Though, A3 doesn't have to be "Mo" because the days in this case can change (A3 can start with "Tu"), hence I think I need a macro.

    So if this row contains a weekend, "Sa or "Su" I want all the cells in the column beneath that which contains a specific value to be cleared.

    Example, if "Sa" or "Su" has 3 values in the columns under them, all the values that contain "X" or "Y" has to be cleared.

    Like this:
    Rows (1,2,3...,)
    1----
    2----
    3 Mo - Tu - We - Th - Fr - Sa - Su - .. and so on..
    4 A --- B --- X --- Y --- X --- B --- Y
    5 A --- B --- X --- Y --- X --- X --- X
    6 A --- B --- X --- Y - --X --- Y --- C

    After the macro it should be:

    1----
    2----
    3 Mo - Tu - We - Th - Fr - Sa - Su
    4 A --- B --- X --- Y --- X --- B ---
    5 A --- B --- X --- Y --- X --- ---
    6 A --- B --- X --- Y - --X --- --- C


    I would appreciate help on this.

    Notice the two examples in the excel file.

    Thanks in advance,
    -Johan
    Attached Files Attached Files
    Last edited by johansoder; 06-18-2014 at 12:16 PM. Reason: Attached file

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,622

    Re: If a specific row contains a word, then clear specific content in corresponding column

    The Morse Code is nice but it would make it a little easier to work with if you posted a sample workbook with some typical data.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    53

    Re: If a specific row contains a word, then clear specific content in corresponding column

    This should work for you

    Sub cleanWeekends()
    Dim x As Integer
    
    With Sheets(1) 'change if using a different sheet
        For x = 1 To 500 'assuming max 500 columns
            If LCase(.Cells(3, x)) = "sa" Or LCase(.Cells(3, x)) = "su" Then
    
                If LCase(.Cells(4, x)) = "x" Or LCase(.Cells(4, x)) = "y" Then
                    .Cells(4, x).ClearContents
                End If
    
                If LCase(.Cells(5, x)) = "x" Or LCase(.Cells(5, x)) = "y" Then
                    .Cells(5, x).ClearContents
                End If
    
                If LCase(.Cells(6, x)) = "x" Or LCase(.Cells(6, x)) = "y" Then
                    .Cells(6, x).ClearContents
                End If
                
            End If
        Next x
    End With
    
    End Sub
    Cheers
    Diberlee
    Last edited by diberlee; 06-18-2014 at 06:36 AM. Reason: tidy up code

  4. #4
    Registered User
    Join Date
    06-17-2014
    Location
    Uppsala, Sweden
    MS-Off Ver
    2010
    Posts
    43

    Re: If a specific row contains a word, then clear specific content in corresponding column

    I attached an example file, sorry for forgeting that.

    I have to go on a meeting now, but will try your code, Diberlee, when I get back. Thanks

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,622

    Re: If a specific row contains a word, then clear specific content in corresponding column

    Just to build on the solution provided, which works with the sample provided:

    Option Explicit
    
    Sub cleanWeekends()
    Dim lLC As Long
    Dim lLR As Long
    Dim x As Long
    Dim y As Long
    
    Application.ScreenUpdating = False
    With ActiveSheet
        ' determine last column of data
        lLC = .Cells(3, .Columns.Count).End(xlToLeft).Column
        ' determine last row of data
        lLR = .Cells(.Rows.Count, 1).End(xlUp).Row
        
        ' loop through columns
        For x = 1 To lLC
            ' Saturday or Sunday?
            If LCase(.Cells(3, x)) = "sa" Or LCase(.Cells(3, x)) = "su" Then
                ' loop through rows
                For y = 4 To lLR
                    ' check contents for X and Y
                    If LCase(.Cells(y, x)) = "x" Or LCase(.Cells(y, x)) = "y" Then
                        .Cells(y, x).ClearContents
                    End If
                Next 'y
            End If
        Next 'x
    End With
    Application.ScreenUpdating = True
    End Sub

    This caters for any number of columns and rows.

    Could probably be quicker using an array but I'll leave that enhancement for someone else


    Regards, TMS

  6. #6
    Registered User
    Join Date
    06-17-2014
    Location
    Uppsala, Sweden
    MS-Off Ver
    2010
    Posts
    43

    Re: If a specific row contains a word, then clear specific content in corresponding column

    Thanks guys, works like a charm. Will give rep!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,622

    Re: If a specific row contains a word, then clear specific content in corresponding column

    You're welcome. Thanks for the rep.

+ 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. [SOLVED] Clear contents of specific cells on the same row if a specific cell on that row is empty
    By djfscouse in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2014, 01:48 PM
  2. Move row data to a specific sheet if column contains specific word
    By Kennethc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-06-2014, 06:55 PM
  3. [SOLVED] Clear all data below header row in specific column
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2014, 04:50 PM
  4. Delete rows or Clear contents below a specific word is found in Column A
    By kishoremcp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2012, 04:08 PM
  5. [SOLVED] Move cells containing specific content into a specific column
    By mb5555 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2012, 01:46 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