+ Reply to Thread
Results 1 to 2 of 2

Conditional delete based on cell in same row, diff column

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2009
    Location
    Vancouver BC
    MS-Off Ver
    Excel 2004 Mac
    Posts
    1

    Conditional delete based on cell in same row, diff column

    Hi there,

    I have 2 different parameters (temperature and activity) that I have recorded from 12 different subjects, with each parameter's results displayed in adjacent (12) columns. On occasion, there was no data obtained for one parameter in one subject, therefore this parameter's cell reads NaN - I would like to then delete the cell that carries the other parameter, as the reading was likely not a good one. The problem is, the corresponding cell (the other parameter from the same subject, is 12 columns away (same row). Is there a way to code: if cell reads NaN, delete cell in the same row, 12 columns to the left?

    it would be ideal if I could even clear one cell in addition to those that are missing data in the other parameter. For example, if in column 13, row 5-10 were missing data (or NaN in this case) then it would be ideal to clear row 5-11 in column 1 (the column corresponding to the same subject, but other parameter). This is because, after a gap in recording, the first proper recording is often affected by the gap rather than the body's physiology itself.

    Thanks! There are many many minutes of data, and I am not sure if I could do it all manually

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Conditional delete based on cell in same row, diff column

    See if this meets your need.
    It will ask for the number of the target column in which to clear the cells. Then it will prompt for column number of the sources cells in which to check for "NaN".
    Applicable cells in the first given columns will be cleared.
    The code will automatically offset to the left or right of the source columns (cells to check for "NaN"., so entering the first column number is critical.

    Sub Delete_X()
    Dim c As Range
        
        Dim iCol_1 As Integer, icol_2 As Integer, iVal As Integer
        Dim lRow As Long
        
        iCol_1 = Application.InputBox("Enter the column number of the target cells to clear")
        icol_2 = Application.InputBox("Enter the column number of the source cells to check")
        iVal = iCol_1 - icol_2
    
        lRow = ActiveSheet.Cells(Rows.Count, icol_2).End(xlUp).Row
        
        Application.ScreenUpdating = False
        
        On Error Resume Next
        Range(Cells(1, icol_2), Cells(lRow, icol_2)).Select
        For Each c In Range(Cells(1, icol_2), Cells(lRow, icol_2))
            If c.Value = "NaN" Then
                c.Offset(0, iVal).ClearContents
            End If
        Next c
        
    End Sub
    Note: try this on a COPY of your worksheet first as VBA actions cannot be undone.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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