If is last row with data (values) in cells then...

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Code:
Hi everyone i have a simple macro that removes some data in a row then copies what is below and moves it all up (so there is not an empty row). Its pretty basic in the sence it just highlights the row that needs to be deleted and deletes the cells in it and then with Cntr and arrow buttons does the rest. ( see my macro below) The problem is if it is the last row it errors out because what is below is 1000's of lines of empy data. Would anyone be able to help me to modify this with an If Else statement that identifies if this is the last row of data?

Here is what im using now:

Code:
Sub Delete_Row_20()
Application.ScreenUpdating = False
    Range("B20").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    Range("B21").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Range("B20").Select
    ActiveSheet.Paste
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    Range("C2").Select
Application.ScreenUpdating = False
End Sub

I have a button like this in 30 rows (1 for each row) I just need to delete its particular row and move any data below (if there is any) up. I cannot use a simple delete row macro, cause it cause REF#! errors in other sheets.

Thanks everyone in advance. :)

sd
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You could have one macro for all thirty buttons.

This identifies the row number of the button that was clicked and deletes that button along with the row that it is on.

Code:
Sub Macro()
Dim bRow As Long
    bRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
    ActiveSheet.Shapes(Application.Caller).Delete
    Rows(bRow).Delete
End Sub

This will only work with a button from the Form Controls, this will not work with a Command button from the ActiveX Controls.

Please backup your data before trying.
 
Upvote 0
You could have one macro for all thirty buttons.

This identifies the row number of the button that was clicked and deletes that button along with the row that it is on.

Code:
Sub Macro()
Dim bRow As Long
    bRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
    ActiveSheet.Shapes(Application.Caller).Delete
    Rows(bRow).Delete
End Sub

This will only work with a button from the Form Controls, this will not work with a Command button from the ActiveX Controls.

Please backup your data before trying.


Comfy, Thanks for your reply. I really like that idea. Is there a way to NOT delte the row? It seems when i delete a row on this sheet it kills vlookups on other sheets.

Also Id like to keep the button as well for future use?

Thanks again.

sd
 
Upvote 0
You could just hide the row?

Code:
Sub Macro()
Dim bRow As Long
    bRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
If Rows(bRow).EntireRow.Hidden = False Then Rows(bRow).EntireRow.Hidden = True
End Sub

else I'll add your else if
 
Upvote 0
You could just hide the row?

Code:
Sub Macro()
Dim bRow As Long
    bRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
If Rows(bRow).EntireRow.Hidden = False Then Rows(bRow).EntireRow.Hidden = True
End Sub

else I'll add your else if


I like the way your think but, I cant hide it cause of the vlookups on other sheets that reference those rows. The data would have to be replaced.

Helps if I tell what the rows are. :) The rows are names of employees. Often times they are not current, so a name will need to be deleted (then the below names( if any) will need to be moved up, so the vlookups on the other sheets can reference those names.

Thanks sd
 
Upvote 0
Deleting Rows of unused data should not have an adverse effect on vlookups in other sheets as when rows are deleted Excel automatically changes the vlookup Range to the new, in this case, shorter range.
 
Upvote 0
Deleting Rows of unused data should not have an adverse effect on vlookups in other sheets as when rows are deleted Excel automatically changes the vlookup Range to the new, in this case, shorter range.


When i delete a row on this sheet, the vlookup referencing that row on the other sheets get a #REF! error. Can you think of something I am doing wrong?
 
Upvote 0
Could you paste some examples of you vlookups so that I can see how you have your sheets setup.
 
Upvote 0
A possible way around the #ref error you're getting on the other sheets could be to name the range you're looking up...<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>


For instance I'd consider:<o:p></o:p>
  • Naming range A1 to A10 (or whatever your cells are that contain data you want removing), for example as 'colleaguenames'<o:p></o:p>
  • Refer your vlookups to the named range
    for example: vlookup(A1,Colleaguenames,1,false)<o:p></o:p>
  • When you delete a row the named range will update automatically and shrink, so to re-size the range, I’d find the last row of data and then reset the named range every time the deletion prompt was done
    For example: FinalRow = cells(rows.count,1).end(xlup).row
    range(“A1:A”& FinalRow).name = “colleaguenames” <o:p></o:p>
Might be others ways to do it, but it’ll mean you don’t get #ref errors and you’ll have dynamic ranges<o:p></o:p>
Does that make sense?<o:p></o:p>
 
Upvote 0
Could you paste some examples of you vlookups so that I can see how you have your sheets setup.


Im embarrassed !! Im usint "=" formulas

Code:
=MyStoreInfo!$B$10&" "&MyStoreInfo!$C$10

Is there a way to still use some thing like so, when i delete a row?

thanks for hanging in there. :)

sd
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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