Solved

excel macro error

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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;…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

743 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

13 Experts available now in Live!

Get 1:1 Help Now