compare two cells in different sheets

srr797

New Member
Joined
Nov 6, 2010
Messages
39
Hi,

I am hoping someone can help me with what seems to be a pretty basic code. I have two worksheets in the same workbook : sheet1 and sheet2. I need code that will compare cell B1 in each sheet and delete the sheet with the smaller value. Also, if the sheets have the same number in cell B1, then delete sheet2.

Thanks in advance!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try:
Code:
Sub test()
Application.DisplayAlerts = False
Select Case Sheets("Sheet1").Range("B1").Value
Case Is < Sheets("Sheet2").Range("B1").Value
Sheets("Sheet1").Delete
Case Is > Sheets("Sheet2").Range("B1").Value
Sheets("Sheet2").Delete
Case Is = Sheets("Sheet2").Range("B1").Value
Sheets("Sheet2").Delete
End Select
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thanks for your help. The code works for what I had asked. However, I have now run into a problem; that it will show an error if either sheet1 or sheet2 do not exist. Is there a way to prompt this code only to run when the two sheets exist?

Thanks again!
 
Upvote 0
You could just add an 'on error' part to the code.
Code:
Sub test()

    On Error GoTo ErrTrap

    Application.DisplayAlerts = False

    Select Case Sheets("Sheet1").Range("B1").Value
        Case Is < Sheets("Sheet2").Range("B1").Value
            Sheets("Sheet1").Delete
        Case Is > Sheets("Sheet2").Range("B1").Value
            Sheets("Sheet2").Delete
        Case Is = Sheets("Sheet2").Range("B1").Value
            Sheets("Sheet2").Delete
    End Select

    Application.DisplayAlerts = True

    Exit Sub

ErrTrap:
    ' You could put some code here which will only
    ' run if one of the sheets are not there.
End Sub

Hope that helps.
 
Upvote 0
Code:
Sub test()
On Error GoTo Control
Application.DisplayAlerts = False
Select Case Sheets("Sheet1").Range("B1").Value
Case Is < Sheets("Sheet2").Range("B1").Value
Sheets("Sheet1").Delete
Case Is > Sheets("Sheet2").Range("B1").Value
Sheets("Sheet2").Delete
Case Is = Sheets("Sheet2").Range("B1").Value
Sheets("Sheet2").Delete
End Select
Application.DisplayAlerts = True
Control:
MsgBox ("One sheet or value doesn't exist!")
End Sub
 
Upvote 0
For what it's worth, here's another way to approach the issues. One thing to note is that you shouldn't have to test for 3 different conditions.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Del_Sheet()<br>    <SPAN style="color:#00007F">Dim</SPAN> ws1 <SPAN style="color:#00007F">As</SPAN> Worksheet, ws2 <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> ws1 = Sheets("Sheet1"): <SPAN style="color:#00007F">Set</SPAN> ws2 = Sheets("Sheet2")<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> ws1 <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Or</SPAN> ws2 <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "At least one sheet doesn't exist"<br>    <SPAN style="color:#00007F">Else</SPAN><br>        Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> ws1.Range("B1").Value < ws2.Range("B1").Value <SPAN style="color:#00007F">Then</SPAN><br>            ws1.Delete<br>        <SPAN style="color:#00007F">Else</SPAN><br>            ws2.Delete<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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