Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 465
Default Match and delete rows on two different sheets from reference numbers



HI All

I have a little problem I need to solve with Excel.

In sheet1 in column A I have rows of reference numbers.

In sheet 2 in columns A I have equally rows of reference numbers , some
of which match those reference numbers in sheet 1 , and some don't.

I need help with a macro to delete the rows in Sheet 1 which don't have
corresponding reference numbers in sheet 2.

So for example - BEFORE

Sheet 1

A

1
2
3
4
5
6
7
8
9
10

Sheet 2

A

3
5
7

AFTER

Sheet 1

A

3
5
7

Only rows with references 3 5 & 7 remain in sheet 1 , as these were the
only ones which match the reference numbers in sheet 2. The other rows
were deleted from sheet 1.

Grateful for any help.



Best Wishes

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3,872
Default Match and delete rows on two different sheets from reference numbers

Hi Colin,

Am Tue, 6 Aug 2013 16:57:47 +0100 schrieb Colin Hayes:

Only rows with references 3 5 & 7 remain in sheet 1 , as these were the
only ones which match the reference numbers in sheet 2. The other rows
were deleted from sheet 1.


try:
Sub DeleteRows()
Dim LRow As Long
Dim LRow2 As Long
Dim i As Long

LRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
With Sheets("Sheet1")
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = LRow To 1 Step -1
If WorksheetFunction.CountIf(Sheets("Sheet2") _
.Range("A1:A" & LRow2), .Cells(i, 1)) = 0 Then
.Rows(i).Delete
End If
Next
End With
End Sub


Regards
Claus B.
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 465
Default Match and delete rows on two different sheets from reference numbers

In article , Claus Busch
writes
Sub DeleteRows()
Dim LRow As Long
Dim LRow2 As Long
Dim i As Long

LRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row With
Sheets("Sheet1")
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = LRow To 1 Step -1
If WorksheetFunction.CountIf(Sheets("Sheet2") _
.Range("A1:A" & LRow2), .Cells(i, 1)) = 0 Then
.Rows(i).Delete
End If
Next
End With
End Sub


Hi Claus

OK this works perfectly - thank you.

BTW I see that altering 0 to 1 in the line

If WorksheetFunction.CountIf(Sheets("Sheet2") _
Range("A1:A" & LRow2), .Cells(i, 1)) = 0 Then.Rows(i).Delete

gives the reverse function.

Would it be possible to build in a popup message box so the user can
enter 0 or 1 to get the action required?



Best Wishes

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3,872
Default Match and delete rows on two different sheets from reference numbers

Hi Colin,

Am Wed, 7 Aug 2013 00:41:39 +0100 schrieb Colin Hayes:


BTW I see that altering 0 to 1 in the line


if you are deleting rows from top the rows below shift up. And the
counter is not working properly.

Would it be possible to build in a popup message box so the user can
enter 0 or 1 to get the action required?


Is that what you want?

Sub DeleteRows()
Dim LRow As Long
Dim LRow2 As Long
Dim i As Long
Dim ans As Integer

ans = MsgBox("Do you really want to delete the rows", _
vbOKCancel + vbInformation, "Safety check")

If ans = vbOK Then
LRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
With Sheets("Sheet1")
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = LRow To 1 Step -1
If WorksheetFunction.CountIf(Sheets("Sheet2") _
.Range("A1:A" & LRow2), .Cells(i, 1)) = 0 Then
.Rows(i).Delete
End If
Next
End With
End If
End Sub


Regards
Claus B.
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 465
Default Match and delete rows on two different sheets from reference numbers

Is that what you want?

Sub DeleteRows()
Dim LRow As Long
Dim LRow2 As Long
Dim i As Long
Dim ans As Integer

ans = MsgBox("Do you really want to delete the rows", _
vbOKCancel + vbInformation, "Safety check")

If ans = vbOK Then
LRow2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
With Sheets("Sheet1")
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = LRow To 1 Step -1
If WorksheetFunction.CountIf(Sheets("Sheet2") _
.Range("A1:A" & LRow2), .Cells(i, 1)) = 0 Then
.Rows(i).Delete
End If
Next
End With
End If
End Sub


Regards
Claus B.



Hi Claus

Yes , that's fine. Thanks very much for your help,



Best Wishes

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete rows where colums match Del_F Excel Programming 3 August 16th 07 10:04 AM
Delete rows that do not match criteria rlee1999 Excel Programming 1 November 3rd 06 03:16 PM
VB Delete rows that do not match criteria rlee1999 Excel Programming 2 November 1st 06 11:19 PM
find closest match to a reference number in a row of numbers Nick Krill Excel Discussion (Misc queries) 4 December 21st 05 11:59 AM
how do I delete all rows that match a condition? djhs63 Excel Worksheet Functions 5 March 16th 05 03:55 PM


All times are GMT +1. The time now is 01:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"