Code line to delete two table rows below last filled row

djbroek

New Member
Joined
Mar 2, 2013
Messages
21
This macro I have doesn't work because it will deletes two rows directly below the row I'm in that have data in them.
This causes problems because those rows below me may already have data in them.

I need to change the .EntireRow.Delete line below to instead delete the next two blank rows that are beneath the last filled row in the worksheet. I'm just getting started in using VBA and have tried multiple different codes but cannot figure it out. Please help!


Code:
[SIZE=3][COLOR=#000000][FONT=Calibri]Private Sub Worksheet_Change(ByVal Target As Range)<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]If Target.Count > 1 Then Exit Sub<o:p></o:p>[/FONT][/COLOR][/SIZE]
<o:p>[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]</o:p>
[SIZE=3][COLOR=#000000][FONT=Calibri]Dim RowNum As Long<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    RowNum = Target.Row<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    <o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    If WorksheetFunction.CountA(Range("D" & RowNum & ":S" & RowNum)) > 1 Then<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    Range("D" & RowNum & ":S" & RowNum).ClearContents<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    Range("D" & RowNum).Select<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    MsgBox "Please only make one entry per row"<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]    Range("D" & RowNum + 1, Range("D" & RowNum + 2)).EntireRow.Delete<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]End If<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]End If<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]End Sub<o:p></o:p>[/FONT][/COLOR][/SIZE]
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
check if the selected lines are the lines you want to delete

Code:
Sub djbroek()
lastrow = Range("A" & Rows.Count).End(xlUp).Row 'lastrow with data
Rows(lastrow + 1 & ":" & lastrow + 2).Select
End Sub
 
Upvote 0
I need to change the .EntireRow.Delete line below to instead delete the next two blank rows that are beneath the last filled row in the worksheet.
This doesn't make much sense. Any rows below the last 'filled' row would already be empty, so what is the point of deleting them?
 
Upvote 0
i thought the same but if you use clearcontents anywhere cell formats are still active maybe he wants to delete those??
 
Upvote 0
True, but the same considerations might also apply to rows below those two as well.
 
Upvote 0
I replaced the

Code:
Range("D" & RowNum + 1, Range("D" & RowNum + 2)).EntireRow.Delete

with

Code:
lastrow = Range("A" & Rows.Count).End(xlUp).Row 'lastrow with data
Rows(lastrow + 1 & ":" & lastrow + 2).Select

and either it prevented another row from being added after the last filled row or it did delete one row below the last filled row. But for some reason it did not delete the second row.

The reason I need two rows taken out after that last filled row after the MsgBox "Please only make one entry per row" event is activated is because I have a double-click macro which adds another row to the table after the last filled row each time a user fills a row. When the MsgBox "Please only make one entry per row" event is activated, it means that the user had tried to double-click another entry on the same row. So then, the row gets cleared and then the user still has to double-click on the row again to enter the desired selection (which then causes another row to be added after the last filled row).

I hope the above paragraph makes some sense. I just wanted to have the same number of blank rows below the last filled row to maintain a desired cosmetic look to the worksheet.
 
Last edited:
Upvote 0
I have several colored rows after the last filled rows which are there to keep from having the glaring white blank rows below the filled rows. I have sensitive eyes and need to keep the white areas out of the computer screen.
 
Upvote 0
I have several colored rows after the last filled rows which are there to keep from having the glaring white blank rows below the filled rows. I have sensitive eyes and need to keep the white areas out of the computer screen.
In that case, might I suggest, you're going about the the entirely wrong way. Whilst shading the worksheet might help your eyes, it also affects affects the printout - as well as requiring extra work to maintain. A better approach would be to use the Windows Control Panel to change the screen brightness and/or the Window colour. These changes will affect all programs - and even the viewing colours of many internet sites.
 
Upvote 0
Sorry about the post on another forum. That won't ever happen again.
I still wish to have an answer to this code problem. The information from this worksheet is linked to another worksheet where data will be printed.
I'm not sure why it would cause problems the users in my group all use the same type of computers and network.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top