+ Reply to Thread
Results 1 to 11 of 11

How to delete cells A2&B2 if B2 is empty.

Hybrid View

chriscmartin How to delete cells A2&B2 if ... 08-29-2014, 09:42 AM
ragulduy Re: How to delete cells A2&B2... 08-29-2014, 09:50 AM
chriscmartin Re: How to delete cells A2&B2... 08-29-2014, 10:30 AM
ragulduy Re: How to delete cells A2&B2... 08-29-2014, 10:42 AM
chriscmartin Re: How to delete cells A2&B2... 08-29-2014, 10:48 AM
ragulduy Re: How to delete cells A2&B2... 08-29-2014, 10:50 AM
Vikas_Gautam Re: How to delete cells A2&B2... 08-29-2014, 11:56 AM
Vikas_Gautam Re: How to delete cells A2&B2... 10-07-2014, 11:59 AM
james8992 Re: How to delete cells A2&B2... 10-09-2014, 10:40 PM
Vikas_Gautam Re: How to delete cells A2&B2... 10-09-2014, 11:59 PM
HaHoBe Re: How to delete cells A2&B2... 10-10-2014, 12:19 AM
  1. #1
    Registered User
    Join Date
    08-29-2014
    Location
    Wales
    MS-Off Ver
    2010
    Posts
    11

    How to delete cells A2&B2 if B2 is empty.

    I have a very long list of data that fills A2:A10000 and some of these have corresponding data in column B.

    What I want is for excel to automatically scan the cells and if B2 = empty cell I want it to delete cells A2&B2.

    I want this to happen all the way through cells B2:B10000 to remove the blanks and corresponding data.


    Im not sure if I can do this with a macro or VBA but any help you can give will be greatly appreciated as im new to this.

    Thanks

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to delete cells A2&B2 if B2 is empty.

    Assuming you mean shift the cells up by delete:
    sub macro_1()
    union(Range("B2:B10000").SpecialCells(xlCellTypeBlanks),Range("B2:B10000").SpecialCells(xlCellTypeBlanks).Offset(0, -1)).Delete xlup
    End Sub

  3. #3
    Registered User
    Join Date
    08-29-2014
    Location
    Wales
    MS-Off Ver
    2010
    Posts
    11

    Re: How to delete cells A2&B2 if B2 is empty.

    Thanks, Will this also delete the cell directly to the left of it column A?

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to delete cells A2&B2 if B2 is empty.

    Why don't you try it and see....

    (the offset(0,-1) part means to move one column to the left, so it should)

  5. #5
    Registered User
    Join Date
    08-29-2014
    Location
    Wales
    MS-Off Ver
    2010
    Posts
    11

    Re: How to delete cells A2&B2 if B2 is empty.

    Thanks, I tried it and get the below error

    Run-time error '1004':
    No cells were fount

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to delete cells A2&B2 if B2 is empty.

    When you say empty, do you mean there is a formula in it that equates to ""? (the special cells method will find blank as in with no formula or value).

    if so, I think you need to loop through the cells
    sub macro_1()
    dim count
    for count = 10000 to 2 step -1
      if Range("B" & count) = "" then Range("A" & count & ":B" & count).delete xlup
    next count
    end sub

  7. #7
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: How to delete cells A2&B2 if B2 is empty.

    Try this..
    Sub DelRow()
    For each cell in Range("B2:B10000")
    if cell.value="" then Cell.select : selection.resize(-1,).delete Xlup
    Next
    End sub
    Last edited by JBeaucaire; 08-29-2014 at 01:20 PM.

  8. #8
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: How to delete cells A2&B2 if B2 is empty.

    Thanks for the feedback and reputation point...
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  9. #9
    Registered User
    Join Date
    10-09-2014
    Location
    Kansas
    MS-Off Ver
    2013
    Posts
    1

    Re: How to delete cells A2&B2 if B2 is empty.

    I have a similar issue but I have more then just columns A & B. I have a table where every other column is Date | Amount. So it goes


    Date 1 | Amount 1| Date 2 | Amount 2 | Date 3 | Amount 3......................

    The dates are always populated, but the amount for that month is sometimes blank. I need to locate where the amount is blank and delete the cell and shift left and also delete the associated date that doesn't have a value and also shift that to the left. So when the amount is blank you are deleting and sifting to the left both where an amount would have been and and also the date cell to the left.

    Before.PNG

    Should look like this

    After.jpg
    Attached Images Attached Images
    Last edited by james8992; 10-09-2014 at 10:49 PM.

  10. #10
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: How to delete cells A2&B2 if B2 is empty.

    Hi,
    Welcome to this forum
    It would have been more sensible if you have provided a raw worksheet, rather than clicking a photo..

    Nevertheless, here is the code you are looking for
    untested though...

    Sub ForJames()
    Application.ScreenUpdating = False
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For r = 2 To lr Step 1
        For c = 2 To lc Step 2
            If IsEmpty(Cells(r, c)) Then _
                Cells(r, c).Select: _
                Selection.Resize(0, -1).Delete Shift:=xlToLeft: _
                c = c - 2
        Next
    Next
    Application.ScreenUpdating = True
    End Sub
    If you require any other adjustment, just give me a Message....
    Last edited by Vikas_Gautam; 10-10-2014 at 12:41 AM.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to delete cells A2&B2 if B2 is empty.

    Hi, james8992,

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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. Replies: 1
    Last Post: 05-26-2014, 01:31 PM
  2. Replies: 2
    Last Post: 07-25-2013, 01:01 PM
  3. [SOLVED] still have empty cell after run Delete empty cells code
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2012, 04:28 PM
  4. Delete Empty Cells With VBA
    By markrennolds in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-30-2010, 02:46 PM
  5. Delete empty cells
    By kingdt in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2007, 04:18 AM

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.6.0 RC 1