Solved

excel macro error

Posted on 2011-02-15
3
358 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

735 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