Results 1 to 8 of 8

Thread: [RESOLVED] Delete Adjacent Cell Based on Value

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    49

    Resolved [RESOLVED] Delete Adjacent Cell Based on Value

    Hello all,

    I'm trying to perform a simple operation, this is how it looks logically:

    Scan Range ("D17" & 17 + a)
    IF[D17=""] Then Delete the adjacent cell... [Cell (0,1)] or [Range("E17").Delete]

    I know the coding for this should be simple but I don't have any reference from a past project.

    Thank you

  2. #2
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: Delete Adjacent Cell Based on Value

    Code:
    Sub test()
    Dim a as long
    Dim myR as Range
    Set myR = Range("D16")
    For a = 1 to (whatever max you have for a)
        If myR.offset(a,0).value = ""
            'Delete code here
        End if
    Next a
    End Sub
    I would record a macro of deleting a cell to make sure you have all the options for which way you want cells to shift.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    49

    Re: Delete Adjacent Cell Based on Value

    Ok so I think I want to Clear.Contents instead of deleting the cell. Whats happening though is that the entire range is being selected instead of just the adjacent cell to the "" value.

    Do I need to do something like:

    Code:
    If myR.Offset(a,0).value = "" Then Activate Adjacent cell and Clear.Contents
    
    Check the next row down
    Code Applied:
    Code:
        Dim y As Long
        Dim myR As Range
        Set myR = Range("D16")
        For y = 1 To 250
            If myR.Offset(y, 0).Value = "" Then Selection.ClearContents
        Next y

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    49

    Re: Delete Adjacent Cell Based on Value

    I should probably give some more details about what I'm doing.

    D1= 1
    D2= ""
    D3= 1
    D4= 1
    D5= ""

    E1= Test
    E2= Delete
    E3= Test
    E4= Test
    E5= Delete
    I need to clear out the contents wherever the "Delete" appears, however "Delete" is a random string. I cannot delete the entire row as the data is in a grid, once it works I'm going to replicate the formula then apply it to about half a dozen different fields.

  5. #5
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: Delete Adjacent Cell Based on Value

    I would change
    Code:
    If myR.Offset(y, 0).Value = "" Then Selection.ClearContents
    to:

    Code:
    If myR.Offset(y, 0).Value = "" Then myR.Offset(y,1).ClearContents

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    49

    Re: Delete Adjacent Cell Based on Value

    Thanks! It works!

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    49

    Re: Delete Adjacent Cell Based on Value

    For use with Multiple ranges

    Code:
        Dim y As Long
        Dim myD As Range
        Set myD = Range("D16")
        For y = 1 To 25
            If myD.Offset(y, 0).Value = "" Then myD.Offset(y, 1).ClearContents
        Next y
        
        Dim myG As Range
        Set myG = Range("G16")
        For y = 1 To 25
            If myG.Offset(y, 0).Value = "" Then myG.Offset(y, 1).ClearContents
        Next y
        
        Dim myJ As Range
        Set myJ = Range("J16")
        For y = 1 To 25
            If myJ.Offset(y, 0).Value = "" Then myJ.Offset(y, 1).ClearContents
        Next y

  8. #8
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: [RESOLVED] Delete Adjacent Cell Based on Value

    Could also do it as

    Code:
    Dim y as Long, z as Long
    Dim myD as Range
    Set myD=Range("D16")
    For z = 0 to 2
        For y = 1 to 25
            If myD.Offset(y,z*3).Value = "" Then myD.Offset(y,(Z*3)+1).ClearContents
        Next y
    Next z
    (This works because D-G and G-J are 3 columns apart).

    or

    Code:
    Dim y As Long
        Dim myD As Range
        Set myD = Range("D16")
        Dim myG As Range
        Set myG = Range("G16")
        Dim myJ As Range
        Set myJ = Range("J16")
        For y = 1 To 25
            If myD.Offset(y, 0).Value = "" Then myD.Offset(y, 1).ClearContents
            If myG.Offset(y, 0).Value = "" Then myG.Offset(y, 1).ClearContents
            If myJ.Offset(y, 0).Value = "" Then myJ.Offset(y, 1).ClearContents
        Next y
    This just dumps all the Ifs into a single y For-Next run.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width