Delete row messing with formulas

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi, I have this piece of code which I'm using to delete products from a database, but when I run it I get a #REF error on other sheets which have a formula dependent on the items I have deleted. I have tried replacing "Delete" with "ClearContents" and then running my sort macro. This doesn't give me the problem with the formulas, but the sort macro gets stuck on the empty row and doesn't move past it. What would be the best way to solve this problem.

This is my delete macro.

VBA Code:
Private Sub DeleteRow()
    Dim iRw As Integer
    Dim wsWS As Worksheet
    Set wsWS = Worksheets("Order Sheet")
    iRw = cbxItem.ListIndex + 2 'listindex starts at 0, plus a header row, so +2
    wsWS.Cells(iRw, 1).EntireRow.Delete
End Sub

This is my sort macro.

VBA Code:
Sub Sort_Me() 
    With Worksheets("Order Sheet").Range("$A$2").CurrentRegion
        .Sort Key1:=.Columns(1), Header:=xlYes, Order1:=xlAscending   
    End With     
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I think that the problem will be in the formulas, not the code, however there is not enough information to say for certain.
 
Upvote 0
Upvote 0
Yep, that helps a great deal. Looking at your formulas, you will see that each one refers to a specific individual cell, once that cell is deleted it no longer exists, the formula can't find it so it shows the #REF! error that you see.

As far as I can see from your example, the formulas are a direct copy of columns A and J of 'Order Sheet', try this formula in A1, then drag Right and Down.

=INDEX(('Order Sheet'!$A:$A,'Order Sheet'!$J:$J),ROW(),,COLUMN())
 
Upvote 0
Yep, that helps a great deal. Looking at your formulas, you will see that each one refers to a specific individual cell, once that cell is deleted it no longer exists, the formula can't find it so it shows the #REF! error that you see.

As far as I can see from your example, the formulas are a direct copy of columns A and J of 'Order Sheet', try this formula in A1, then drag Right and Down.

=INDEX(('Order Sheet'!$A:$A,'Order Sheet'!$J:$J),ROW(),,COLUMN())
That formula isn't working, it's giving me a #REF! error.
 
Upvote 0
That formula isn't working, it's giving me a #REF! error.
That might be down to me trying to be a bit too clever and do it in a single formula :oops: this should work in column A

=INDEX('Order Sheet'!$A:$A,ROW())

Column B

=INDEX('Order Sheet'!$J:$J,ROW())

If that gives you an error too then it would suggest another problem that is not visible in the example.
 
Upvote 0
Thank you. It works perfectly. I knew it would be a fairly simple fix, but I'm only fairly new to all this.
 
Upvote 0
Jasonb75, how would you then wrap up an if function into an index function?

=IF('Order Sheet'!N2>=0,'Order Sheet'!A2,"")
 
Upvote 0
Never mind, I figured it out. Thanks for your help.
=IF(INDEX('Order Sheet'!$N:$N,ROW())>=0,INDEX('Order Sheet'!$A:$A,ROW()),"")
 
Upvote 0

Forum statistics

Threads
1,215,547
Messages
6,125,461
Members
449,228
Latest member
moaz_cma

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