Comparing two ranges of numbers
Solved/Closed
ChrisCoR
-
Apr 28, 2011 at 01:49 PM
RWomanizer Posts 365 Registration date Monday February 7, 2011 Status Contributor Last seen September 30, 2013 - May 2, 2011 at 12:38 AM
RWomanizer Posts 365 Registration date Monday February 7, 2011 Status Contributor Last seen September 30, 2013 - May 2, 2011 at 12:38 AM
Related:
- Comparing two ranges of numbers
- Excel compare two sheets - Guide
- Comparing two excel files - Programming Forum
- Range of numbers ✓ - Excel Forum
- Comparing two columns using partial match logic ✓ - Excel Forum
- Conditional Formatting: 3 range of numbers ✓ - Excel Forum
5 responses
RWomanizer
Posts
365
Registration date
Monday February 7, 2011
Status
Contributor
Last seen
September 30, 2013
120
Apr 29, 2011 at 12:33 AM
Apr 29, 2011 at 12:33 AM
Hi ChrisCoR,
you can use VLOOKUP for the required result instead of macro.
anyway the new code after correction is as:
hope it will helps you.
you can use VLOOKUP for the required result instead of macro.
anyway the new code after correction is as:
hope it will helps you.
Sub CompareExpendables1() 'This macro checks all of the cells in one column on sheet 1(expendables) 'against one column on sheet 2(on hand inventory) and creates a new sheet 'showing all of the items that appear on both Dim sh1 As Worksheet Dim sh2 As Worksheet Dim sh1row As Integer Dim sh2row As Integer Dim sh1col As Integer Dim sh2col As Integer Dim rng1ct As Range Dim rng2ct As Range Dim row1 As Range Dim row2 As Range Set sh1 = ActiveWorkbook.Sheets(1) Set sh2 = ActiveWorkbook.Sheets(2) sh1row = sh1.Range("a" & Rows.Count).End(xlUp).Row sh1col = sh1.Range("a" & Columns.Count).End(xlToLeft).Column Set rng1ct = sh1.Range("a2").Resize(sh1row, sh1col) sh2row = sh2.Range("a" & Rows.Count).End(xlUp).Row sh2col = sh2.Range("a" & Columns.Count).End(xlToLeft).Column Set rng2ct = sh2.Range("b2").Resize(sh2row, sh2col) 'Set row1 = sh1.Cells(2, 1) 'Set row2 = sh2.Cells(2, 2) Worksheets.Add After:=Worksheets(Worksheets.Count) For Each row1 In rng1ct For Each row2 In rng2ct If row2 = row1 Then row2.EntireRow.Copy Destination:=Worksheets(3).Range("a" & Rows.Count).End(xlUp).Offset(1, 0) End If Next row2 Next row1 End Sub
RWomanizer
Posts
365
Registration date
Monday February 7, 2011
Status
Contributor
Last seen
September 30, 2013
120
Apr 29, 2011 at 07:23 AM
Apr 29, 2011 at 07:23 AM
You are most welcomed
I looked over the VLOOKUP option, and I needed this to be usable with just a couple clicks(my coworkers on this project aren't as comfortable with computers as I am). I added a little bit of code to assist with formatting of the new spreadsheet, but its working perfectly! :)
Sub CompareExpendables() 'This macro checks all of the cells in one column on sheet 1(expendables) 'against one column on sheet 2(on hand inventory) and creates a new sheet 'showing all of the items that appear on both Dim sh1 As Worksheet Dim sh2 As Worksheet Dim sh1row As Integer Dim sh2row As Integer Dim sh1col As Integer Dim sh2col As Integer Dim rng1ct As Range Dim rng2ct As Range Dim row1 As Range Dim row2 As Range Set sh1 = ActiveWorkbook.Sheets(1) Set sh2 = ActiveWorkbook.Sheets(2) sh1row = sh1.Range("a" & Rows.Count).End(xlUp).Row sh1col = sh1.Range("a" & Columns.Count).End(xlToLeft).Column Set rng1ct = sh1.Range("a2").Resize(sh1row, sh1col) sh2row = sh2.Range("b" & Rows.Count).End(xlUp).Row sh2col = sh2.Range("b" & Columns.Count).End(xlToLeft).Column Set rng2ct = sh2.Range("b2").Resize(sh2row, sh2col) Worksheets.Add After:=Worksheets(Worksheets.Count) sh2.Range("a1:l1").Copy Destination:=Worksheets(3).Range("a1:l1") Worksheets(3).Columns("A:A").ColumnWidth = 7 Worksheets(3).Columns("B:B").ColumnWidth = 20 Worksheets(3).Columns("C:C").ColumnWidth = 64 Worksheets(3).Columns("L:L").ColumnWidth = 12.2 For Each row1 In rng1ct For Each row2 In rng2ct If row1 = row2 Then row2.EntireRow.Copy Destination:=Worksheets(3).Range("a" & Rows.Count).End(xlUp).Offset(1, 0) End If Next row2 Next row1 End Sub
Didn't find the answer you are looking for?
Ask a question
RWomanizer
Posts
365
Registration date
Monday February 7, 2011
Status
Contributor
Last seen
September 30, 2013
120
May 2, 2011 at 12:38 AM
May 2, 2011 at 12:38 AM
You can use this code after for loop.
instead of these multiple lines
Worksheets(3).Columns("A:A").ColumnWidth = 7
Worksheets(3).Columns("B:B").ColumnWidth = 20
Worksheets(3).Columns("C:C").ColumnWidth = 64
Worksheets(3).Columns("L:L").ColumnWidth = 12.2
Worksheets(3).Range("A1:L1").Select Selection.EntireColumn.AutoFit
instead of these multiple lines
Worksheets(3).Columns("A:A").ColumnWidth = 7
Worksheets(3).Columns("B:B").ColumnWidth = 20
Worksheets(3).Columns("C:C").ColumnWidth = 64
Worksheets(3).Columns("L:L").ColumnWidth = 12.2