Solved

excel macro error

Posted on 2011-02-15
3
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

695 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