+ Reply to Thread
Results 1 to 15 of 15

delete rows

Hybrid View

john_cash delete rows 08-13-2019, 08:28 AM
PaulM100 Re: delete rows 08-13-2019, 09:00 AM
john_cash Re: delete rows 08-13-2019, 09:08 AM
PaulM100 Re: delete rows 08-13-2019, 09:15 AM
john_cash Re: delete rows 08-13-2019, 09:49 AM
PaulM100 Re: delete rows 08-13-2019, 10:21 AM
john_cash Re: delete rows 08-13-2019, 10:30 AM
PaulM100 Re: delete rows 08-13-2019, 10:36 AM
john_cash Re: delete rows 08-13-2019, 11:01 AM
PaulM100 Re: delete rows 08-13-2019, 11:17 AM
john_cash Re: delete rows 08-13-2019, 11:30 AM
PaulM100 Re: delete rows 08-13-2019, 11:40 AM
jolivanes Re: delete rows 08-13-2019, 11:46 AM
john_cash Re: delete rows 08-13-2019, 12:11 PM
john_cash Re: delete rows 08-14-2019, 02:46 AM
  1. #1
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    delete rows

    Hello everyone,
    I ask for help to delete rows:

    An alert "how many rows you want to delete?"
    example 50
    starting from row 5 it eliminates 50 rows

    must re-enter the formulas for the automatic number in column A
    I hope I explained myself
    john
    Attached Files Attached Files

  2. #2
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    This one should do
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7").FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")": Range("A7:A" & Lastrow).FillDown
    End Sub
    Click the * to say thanks.

  3. #3
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Hi poul, thanks you
    An error if I click cancel or click x goes to debug.

  4. #4
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    This one should handle that:
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        On Error Resume Next
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7").FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")": Range("A7:A" & Lastrow).FillDown
        Err.Clear
        On Error GoTo 0
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Hi poul
    I did tests.
    In the attached example I deleted 200 lines and then in cell A6 / A6 the text of cell A is shown
    Attached Files Attached Files

  6. #6
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    You've deleted more rows than the ones containing data and last row stops in B5 now, that's why. Try this, untested:
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        On Error Resume Next
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Or (num > Application.CountA(Range("B6:B"))) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7").FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")": Range("A7:A" & Lastrow).FillDown
        Err.Clear
        On Error GoTo 0
    End Sub

  7. #7
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Now it does not delete any line always appears

    MsgBox ("You have entered an invalid row number")

  8. #8
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    In a cell type =counta(b6:b200000). This should give you the not empty number of cells. In the macro replace b6:b with b6:b200000. Test it by entering a number smaller that the one returned by the formula first and afterwards one bigger.

  9. #9
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    i tried:

    Option Explicit
    
    
    
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        On Error Resume Next
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Or (num > Application.CountA(Range("B6:B20000"))) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7").FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")": Range("A7:A" & Lastrow).FillDown
        Err.Clear
        On Error GoTo 0
    End Sub
    now in the attach does not delete
    Attached Files Attached Files

  10. #10
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    This one will handle the data. Please be aware that will not delete rows if the user imputs a number higher than the rows with data based on B column data. If you want to delete them and to not add data in column A, as B is empty, then remove this part from the code: Or (num > Application.CountA(Range("B6:B200000")))
    Option Explicit
    
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        On Error Resume Next
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Or (num > Application.CountA(Range("B6:B200000"))) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    If Application.CountA(Range("B6:B200000")) Then
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7").FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")": Range("A7:A" & Lastrow).FillDown
    Else: Exit Sub
    End If
        Err.Clear
        On Error GoTo 0
    End Sub

  11. #11
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Hi poul,
    I entered 125 rows
    I tried with delete 123 rows
    the macro deletes 123 rows but does not contain the formula in A7

  12. #12
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: delete rows

    I've replicated your issue and not there should be no more issues.

    Option Explicit
    
    Sub UserInput()
        Dim num As Integer
        Dim Lastrow As Long
        On Error Resume Next
        num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        If (num < 1) Or (num > Application.CountA(Range("B6:B200000"))) Then
            GoTo Whoops:
        End If
        Dim i As Integer
        i = 1
        Do While i <= num
            Rows(6).EntireRow.Delete
            i = i + 1
        Loop
        GoTo Fin:
    Whoops:
            MsgBox ("You have entered an invalid row number")
    Fin:
    If Application.CountA(Range("B6:B200000")) Then
    Range("A6").Value = 1
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    Range("A7:A" & Lastrow).FormulaR1C1 = "=IFERROR(IF(RC[1]<>"""",R[-1]C+1,""""),"""")"
    'Range("A7:A" & Lastrow).FillDown
    Else: Exit Sub
    End If
        Err.Clear
        On Error GoTo 0
    End Sub
    Last edited by PaulM100; 08-13-2019 at 11:45 AM.

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,705

    Re: delete rows

    Sub Maybe()
    Dim a As Long
    a = InputBox("How Many Rows Do you want to delete?", "Deleting Rows.")
    Rows(6).Resize(a).Delete
        With Range("A6:A" & Cells(Rows.Count, 2).End(xlUp).Row)
            .Formula = "=Row(A1)"
            .Value = .Value
        End With
    End Sub

  14. #14
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Poul, I think is now correct.
    to be sure I have to try it after the summer holidays.
    Greetings and thanks
    john

  15. #15
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    492

    Re: delete rows

    Hi poul,
    I tried to change the inputboxes with userform but here I don't know how to insert num in the inputbox.

    num = InputBox(Prompt:="Delete Rows?", Title:="Enter the number of Rows", Default:="How Many?")
        
        UserForm1.Show
        
        If (num < 1) Or (num > Application.CountA(Range("B6:B200000"))) Then 
            GoTo Whoops:

    I attach example.
    john
    Attached Files Attached Files
    Last edited by john_cash; 08-14-2019 at 03:04 AM.

+ 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. Rows delete formula as per range of cell values with counter to delete # rows
    By qureshi2016 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-03-2017, 09:35 AM
  2. Delete All Rows Below Criteria in Col A / Then Delete Specific Rows in Col E
    By LennyC91 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2016, 08:22 AM
  3. [SOLVED] how to delete delete rows with criteria and keeping formula
    By Bremmah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2013, 07:09 AM
  4. [SOLVED] Delete blank rows between data rows, shift rows up, then repeat
    By excelactuary in forum Excel General
    Replies: 2
    Last Post: 03-11-2013, 11:53 AM
  5. Replies: 5
    Last Post: 11-12-2012, 08:38 PM
  6. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  7. delete duplicate rows, delete empty rows
    By loade in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-03-2012, 05:42 AM

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