+ Reply to Thread
Results 1 to 6 of 6

If then and delete row

Hybrid View

  1. #1
    Registered User
    Join Date
    03-21-2008
    Posts
    18

    If then and delete row

    I am currently in the middle of making a document and trying to get it to recognize it that if the value is greater then 0 it keeps the row and puts the value in that box but if the value is less than 0 it deletes the row.

  2. #2
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378
    Quote Originally Posted by memphis0607
    I am currently in the middle of making a document and trying to get it to recognize it that if the value is greater then 0 it keeps the row and puts the value in that box but if the value is less than 0 it deletes the row.
    By "Puts the value in that box", exactly what are you saying? In that cell? Do you have a text box or a form?

    If Cell A23 (Where does the value reside?) contains a value greater than zero, put the value where? And if it does not have a value greater than zero then delete the entire row?

    How many rows are potentially involved 10's, 100's, 1,000's, etc?
    Thanks!
    Dennis

    I am using Windows 7 and Office 2007, all of my posts are based on this.

  3. #3
    Registered User
    Join Date
    03-21-2008
    Posts
    18
    Example: Cell b2 refers to cell a1. If the value in a1 is greater then 0 then it just gets put into b2. If it isn't greater then 0 then i want it to delete row 2.
    Its a worksheet/workbook in excel.
    Yes that is correct if it does not have a value greater than zero, delete the entire row.
    And maximum 100 rows.

  4. #4
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    I'll get back later -

    On my way home for the evening.

  5. #5
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    I'm Ba...aack

    Quote Originally Posted by memphis0607
    Example: Cell b2 refers to cell a1. If the value in a1 is greater then 0 then it just gets put into b2. If it isn't greater then 0 then i want it to delete row 2.
    Its a worksheet/workbook in excel.
    Yes that is correct if it does not have a value greater than zero, delete the entire row.
    And maximum 100 rows.
    See if you can modify this to suit your needs
    Sub delRows()
    Dim lRow As Long
        lRow = Range("A65536").End(xlUp).Row
        While lRow > 0
            If Cells(lRow, 1).Value > 0 Then
                Cells(lRow, 2).FormulaR1C1 = "=RC[-1]"
            Else
                Rows(lRow).Delete shift:=xlUp
            End If
            lRow = lRow - 1
        Wend
    End Sub

  6. #6
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    Good Morning Memphis0607 - Inside

    Quote Originally Posted by memphis0607
    Example: Cell b2 refers to cell a1. If the value in a1 is greater then 0 then it just gets put into b2. If it isn't greater then 0 then i want it to delete row 2.
    Its a worksheet/workbook in excel.
    Yes that is correct if it does not have a value greater than zero, delete the entire row.
    And maximum 100 rows.
    Good Morning Memphis, Private messages are limited in size, thus this post.

    The code I previously posted has the formula to have the B2 equal to A2. After I read your message again, I created a sample workbook with the corrected formulas and changed the routine to avoid reference errors.

    When you press the button, it will start at the bottom of column A and work its way up. If the cell value is not greater than zero, it will delete the entire row; on the other hand, if the cells value is greater than zero it will place a formula in column B (next row down). The formula is simply equals column A (original row).

    I am attaching that simple file with the macro insertered into a standard module. Press key combination <Alt><F11> to open the VBE Window. If the Project Explorer is not visible press <Ctrl><R> to open it so you can see what the components are in a VBA Project.

    To create your own module, from the main menu select Insert > Module.

    I am unsure of what your total workbook does so I set this to do exactly what I understand that was said above.

    Be sure to try to work your way logically through the steps to see if you understand the routine before you run the macro.

    A good way to see what happens is to make the VBE window small enough to be able to see the worksheet behind it and step through the macro. To step through, just click so the cursor is anywhere within the subroutine and press <F8>. You will see succcessive lines of code hilighted to show what is going to happen next.
    Attached Files Attached Files

+ 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