+ Reply to Thread
Results 1 to 15 of 15

Thread: Compare two columns and delete cell in one column if cell next to it is blank

  1. #1
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Compare two columns and delete cell in one column if cell next to it is blank

    Hello all,

    I need a macro which will look at two columns, for example B and C, and if there isn't a value in the cell in Column C, clear (or change the value to zero if it's easier) the adjacent cell in column B (i.e no value in C15 would clear B15). The macro would need to do this all the way down columns B and C, and then move on to columns D and E, and do the same thing, because my data is grouped in columns of two, and I have too many columns to filter them all manually.

    I hope that someone can help me with this, I had rarely used Excel before starting my dissertation and i've been thrown in the deep end with it.

    Thank you very much in advance,

    Jacob
    Last edited by jacobhandson; 12-17-2011 at 12:52 PM.

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Hi jacobhandson

    possibly something like...
    Sub ptest()
    Dim xCell As Range
    myarray = VBA.Array("C", "3", "E", "5")
    For xKey = 1 To 3 Step 2
    For Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
      If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = ""
    Next xCell
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  3. #3
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Hi, thanks for replying so promptly. Unfortunately I get the error "Compile error: For without next" and it highlights the 1st line "Sub ptest ()"

    do you know why this might be?

    thanks,

    jacob

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    There was a small line missing. Added it in here -
    Sub ptest()
    Dim xCell As Range
    myarray = VBA.Array("C", "3", "E", "5")
    For xKey = 1 To 3 Step 2
    For Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
      If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = ""
    Next xCell
    Next xKey
    End Sub
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Thanks, unfortunately this has just created another error:

    runtime error 1004: method 'range' of object'_global' failed.

  6. #6
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    There is a typo in this line -
     For Each xCell In Range(myarry(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
    Change it to
     For Each xCell In Range(myarray(xKey) & "1", Cells(Rows.Count, myarray(xKey + 1)).End(xlUp))
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  7. #7
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Thanks again Arlette, but I had noticed that and changed it already. any other suggestions as to why that might not work? it is highlighting that line when the error comes up...

  8. #8
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    I guess it would be better if Pike assisted you himself, since i tried figuring it out but couldn't. You could PM him to expedite a response.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  9. #9
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Hi jacobhandson

    Ive test this code , the other i just typed out

    Option Explicit
    Sub ptest()
        Dim xCell As Range
        Dim myarray
        Dim xkey As Integer
        myarray = VBA.Array("C", 3, "E", 5)
        For xkey = 0 To 3 Step 2
            For Each xCell In Range(myarray(xkey) & "1", Cells(Rows.Count, myarray(xkey + 1)).End(xlUp))
                If IsEmpty(xCell.Value) Then xCell.Offset(0, -1).Value = ""
            Next xCell
        Next xkey
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  10. #10
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Perhaps
    Sub test()
        Dim oneColumn As Range
        For Each oneColumn In Sheet1.Range("C1,E1").Columns
            With oneColumn.EntireColumn
                On Error Resume Next
                Application.Intersect(.Offset(0, -1), .SpecialCells(xlCellTypeBlanks).EntireRow).ClearContents
                On Error GoTo 0
            End With
        Next oneColumn
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  11. #11
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Thanks, the first one (post #9) does what I want for columns B to E (but only to about row 178), is there any chance that you could tell me how to modify this to make it do the same thing all the way up to column YI please, and down to row 366? I assume it's meant to do the latter but it isnt?!

    post #10 doesnt seem to work for me i'm afraid. thanks for the attempt nontheless
    Last edited by jacobhandson; 12-14-2011 at 09:09 AM.

  12. #12
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Are the blank cells truely blank or do they have formulas that evaluate to "" ?
    (eg =IF(A1=1, "cat", "") )
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  13. #13
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    the cells are truely blank

    Thanks,

    Jacob

  14. #14
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    Can anyone help me to change the range of the code from post #9: I need it to iteratively perform the function over the full range of the data without me having to change "C" and "E" to other letters hundreds of times.

    I've been told you can use currentregion property to find the range size without having to us end(xlup).
    and that you can work on each column by using columns.count property eg
    for each col in currentregion.columns
    for each row in currentregion,rows

    but again, i have no clue where to start to do this.

    any help would be greatly appreciated,

    Thanks,

    Jacob
    Last edited by jacobhandson; 12-16-2011 at 07:48 PM.

  15. #15
    Registered User
    Join Date
    11-30-2011
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Compare two columns and delete cell in one column if cell next to it is blank

    problem solved:

     Set rng= Range("B2:YI366")
     
    ' loop through each column in range
     
    For I = 1 To rng.Columns.Count ' perhaps Columns.Count-1 so YJ isnt' included
          ' loop through each cell in column
     
          For Each cl in rng.Columns(I).Cells
     
             If cl.Offset(,1) = "" Then
                 cl.Clear
             End If
          Next cl
    Next I
    thanks for all help

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