Solved

excel macro error

Posted on 2011-02-15
3
349 Views
Last Modified: 2012-05-11
hi all

i have an example work book attached, there is a macro on both sheet 1 an 2
the macro in sheet 1 will compare column A of sheet 1 and 2 and return what is unique to sheet1 in sheet3
and vice versa for sheet2

the macro (i did not write) works fine for sheet 1, but fails on sheet2 with type mismatch - and i think this is because sheet 2 has 1 less row

can anyone tell me how i could get around this?
----sheet1 macro---------
Sub CopyUniques_old()
   Dim lngLastRow As Long
   Dim varData
   With Sheets("Sheet1")
      lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
      varData = .Evaluate("INDEX(IF(ISNA(MATCH(A2:A" & lngLastRow & ",Sheet2!A:A,0)),A2:A" & lngLastRow & ",""Matched""),0,0)")
      varData = Filter(Application.Transpose(varData), "Matched", False)
      varData = Application.Transpose(varData)
   End With
   With Sheets("Sheet3")
      .Range("A:A").ClearContents
      .Range("A1").Value = "Stock Codes not on new Forecast "
      .Range("A2").Resize(UBound(varData, 1), 1).Value2 = varData
   End With


End Sub


-----sheet 2 macro----------

Sub CopyUniques_new()
   Dim lngLastRow As Long
   Dim varData
   With Sheets("Sheet2")
      lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
      varData = .Evaluate("INDEX(IF(ISNA(MATCH(A2:A" & lngLastRow & ",Sheet1!A:A,0)),A2:A" & lngLastRow & ",""Matched""),0,0)")
      varData = Filter(Application.Transpose(varData), "Matched", False)
      varData = Application.Transpose(varData)
   End With
   With Sheets("Sheet4")
      .Range("A:A").ClearContents
      .Range("A1").Value = "Stock Codes not on new Forecast "
      .Range("A2").Resize(UBound(varData, 1), 1).Value2 = varData
   End With


End Sub

Open in new window

differences-test.xlsx
0
Comment
Question by:malraff
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34895853
It's because all of the data matches. Try this version:
Sub CopyUniques_new()
   Dim lngLastRow As Long
   Dim varData
   With Sheets("Sheet2")
      lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
      varData = .Evaluate("INDEX(IF(ISNA(MATCH(A2:A" & lngLastRow & ",Sheet1!A:A,0)),A2:A" & lngLastRow & ",""Matched""),0,0)")
      varData = Filter(Application.Transpose(varData), "Matched", False)
      If UBound(varData) < LBound(varData) Then
        MsgBox "No unmatched data"
        Exit Sub
    End If
      varData = Application.Transpose(varData)
   End With
   With Sheets("Sheet4")
      .Range("A:A").ClearContents
      .Range("A1").Value = "Stock Codes not on new Forecast "
      .Range("A2").Resize(UBound(varData, 1), 1).Value2 = varData
   End With


End Sub

Open in new window

0
 

Author Comment

by:malraff
ID: 34895890
great stuff rorya - i think i owe u a pint or 2 ;)
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34895902
Stick a couple of cans of Guinness in the post. ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now