Comparing two queries with VBA macro

Hi,
I have a workbook woth 2 sheets. Every sheet has a BEx query (so 2 queries).
I need, for every column in the first query, to check if it is present in the second query, to generate a list with the NOT PRESENT values (or simply, marking the not present values).
I tried with Searching excel functions by column, but every query hs more that 30.000 rows, so it takes a long time.
I think I have to use an excel macro. I have been lookong for in the forum but my doubt is that I have to execute when the two queries has been refreshed.
How can I know when the two queries has been refreshed?
Is there any other way to do it?
Any idea of how to reference a query from the other?
I think the code shoud be in this way:
for every row in the first colum of 1st query
look in every row in the first column of 2nd query
mark in first query if not present.
If I put the macro in 1st query, how can I reference the 2nd one?

Hi Oscar, there are several ways to do this.
The easiest way would be:
1.  in the SAPBEXonRefresh subroutine (which you will find in the SAPBEX Module attached to your query-containing workbook), add code that looks something like this:
Set ws = resultArea.parent
if ws is nothing then exit sub
resultArea.name = ws.codename & "_results"
ws.Range("D2") = Date
if queryID = "SAPBEXq0002" then Call CompareQueries(queryID)
'make the query ID that of the second query
This will create named ranges in Excel so that you can easily locate the result tables later.  And, ensure that you know the date that each query was refreshed.  And, after the second query is refreshed, it will call your query comparison routine.
2.  In the comparison routine, you will need something like this:
set ws1 = Query1 'assuming that you have set the code name of the first query sheet as Query1
set ws2 = Query2 'assuming that you have set the code name of the second query sheet as Query2
'set ranges for the two result tables
set Range1 = ws1.codename & "_results"
set Range2 = ws2.codename & "_results"
'locate first and last Row and Column for result tables
firstRow1 = Range1.cells(1).row
firstCol1 = Range1.cells(1).column
numCells = Range1.cells.count
lastRow1 = Range1.cells(numCells).row
lastCol1 = Range1.cells(numCells).column
'do same for Range2
'locate the columns you want to compare
searchCol = 0
for j = firstCol1 to lastCol1
    if cells(firstRow1, j) like "some text here" then
        searchCol = j
        exit for
    end if
next j
if searchCol = 0 then
    msgbox "Did not find ""some text here"".", vbCritical
    exit sub
Else:
    set SearchRange = ws1.cells(1, searchCol).entireColumn
end if
for j = firstCol2 to lastCol2
    if cells(firstRow2, j) like "some text here" then
        searchCol = j
        exit for
    end if
next j
'do the search
for i = firstRow2 to lastRow2
    searchFor = cells(i, searchCol)
    matchRow = 0
    on error resume next
    matchRow = Application.worksheetfunction.match(searchFor, SearchRange, 0)
    if matchRow > 0 then
        cells(i, lastCol2+1) = "match found on row " & matchRow
    Else:
        cells(i, lastCol2+1) = "NOT PRESENT"
    End if
next i
- Pete

Similar Messages

  • Can anybody tell how to compare two documents with two pointers controlled with the same mouse

    can anybody tell how to compare two documents with two pointers controlled with the same mouse ??

    I saw what I need but in a game to find the differences between two photos (two screens, two pointers controlled by one mouse), and I need a program to make the same thing   (compare a chosen files)

  • Joining two queries with Visual Composer

    Hallo,
    I was working with visual composer to join two queries with the distinct operator. But on my table view, when i linked it with the distinct operator, i get only the key element customer (customer, customer_ext_key, customer_key). These are the queries that i want join:
    Query 1:
    Customer
    Net Sales
    Query 2:
    Customer
    Billed Quantity
    What i want to see is the following result:
    Customer
    Net Sales
    Billed Quantity
    Can anybody help me with this problem?
    Greeting,
    Murat.

    Hallo,
    I was working with visual composer to join two queries with the distinct operator. But on my table view, when i linked it with the distinct operator, i get only the key element customer (customer, customer_ext_key, customer_key). These are the queries that i want join:
    Query 1:
    Customer
    Net Sales
    Query 2:
    Customer
    Billed Quantity
    What i want to see is the following result:
    Customer
    Net Sales
    Billed Quantity
    Can anybody help me with this problem?
    Greeting,
    Murat.

  • Compare two dateTime with different timezone

    HI, All,
    I found a strange thing when compare two dateTime in BPEL;
    In my BPEL process, client passed a date time to the process , and the process compare the date time with current data time.
    1.client pass cutoffDate to process
    2. in a switch activity, I compare cutoffDate with current date. code:
    bpws:getVariableData('cutoffDate')<=xpath20:add-dayTimeDuration-to-dateTime()
    but seems this compare ignored the timezone information.
    For example:
    cutoffDate=2010-03-05T06:17:38.838+00:00
    currentDate=2010-03-05T14:10:38.838+08:00 this time =2010-03-05T06:10:38.838+00:00
    but cutoffDate<currentDate == true... seems it ignored the timezone info..
    This is a bug or I used a wrong compare function?
    Thanks.
    Edited by: Colin Song on Mar 5, 2010 3:28 PM

    Hi Colin,
    Please go through below link, there is topic about calculating difference between dates. Hope you find solution.
    http://blogs.oracle.com/reynolds/2007/07/19/
    Please let me know, if still not successful.
    Thx,

  • Compare two queries of two different system.

    Hello all,
           I need to certify the some queries, the process we follow is as below.
    1. The query will be created in Production system, we will transport back to Development server.
    2. Compare the query (Characterstics, Key Figures and Definition), In other words I need to compare and validate each and every aspect of Query with the Development and Production.
    3. I need to change the query if I find anything difference in Prod and Dev system.
    4. Transport to the Production.
    Here my problem is I'm comparing manually by opening RRMX in each of the system and each variable, its time consuming as it is a manual process there is a chance of missing some important. So I would like to know is there any way to compare the queries Prod and Dev server in easier way than manually.
    I hope requirement is clear to you or else just let me know your concerns and thoughts on this.
    Thanks in advance,
    Umashankar

    Umashankar,
    I dont think there is an easy way to do this as you are asking for - however you can do a basic level comparison as to number of query ellements , variables that are there etc - this can be done using the table rszcompdir which has all the query elements of the query but then a direct one to onw comparison may not be possible since the query elements are identified by GUIDs which usually will be different across systems.

  • Compare two dates with NULL in one

    I am trying to do the following with a simple SQL. Compare two dates and get the latest one, but the greatest() function always returns NULL if NULL is present. So how can I do it ?
    Date1 Date2 Desired Result
    Null 01-Dec-09 01-Dec-09
    01-May-09 01-Mar-09 01-May-09
    01-May-09 NULL 01-May-09
    01-May-09 01-Nov-09 01-Nov-09
    NULL NULL NULL
    Any suggestion ? Thanks

    Hi,
    Try this,
    create table test1
    fdate date,
    tdate date
    insert into test1 values (null,'25-jan-2010');
    select greatest(nvl(fdate,tdate),nvl(tdate,fdate)) greatest from test1;
    Thanks&Regards,
    Jai

  • Comparing Two tables with 300k records and update one table

    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.

    Hi,
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results  will be the contents of the changed table(s) when everything is finished.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002
    ef2019c7-080c-4475-9cf4-2cf1b1057a41 wrote:
    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.
    Why are you trying to use PL/SQL tables?  If tabl_1 and tabl_2 are regular database tables, it will be much simpler and faster just to use them.
    Depending on your requirements, you can do an UPDATE or MERGE, either in SQL or in PL/SQL.

  • Return two queries with cfreturn

    Hello All,
    I know I've seen it done somewhere, but I can't seem to find
    the example. I need to return two queries like:
    <cfreturn getCurrentProjects and
    getProjectRequestByStatus/>
    Something like that... not getting the right syntax or
    something. Any clues?
    Thanks!

    You can't!!!! You can only return one value with
    <cfreturn...>
    But of course there is nothing preventing that one value to
    be a
    structure or array contain two query record sets.
    I.E.
    <cfset var myReturn = strunctNew()>
    <cfset myReturn.getCurrentProjects =
    getCurrentProjects>
    <!--- getCurrentProjects is scoped as a local variable
    with VAR,
    correct? --->
    <cfset myReturn.getProjectRequestByStatus =
    getProjectRequestByStatus>
    <!--- getProjectRequest is scoped as a local variable with
    Var, correct?--->
    <cfreturn myReturn>

  • Comparing two numbers with different formats

    Dear Java developers
    I am trying to compare two numbers but they have different formats. long x = 8981261369, Object obj = "8,981,261,369".
    I am trying to see if this two values are equal which is the case in the example above except one has a thousand separator but not sure how to do that.
    How can I lose the thousand separator and just compare them as two long values.
    Your input will be much appreciated
    Thanks

    iu433 wrote:
    I need a generic way of doing this. Because sometimes the Object does not have a thousand separator. Can i use some kind of NumberFormat to do this.You'll need to know what formats the string (not the number--as already mentioned, numbers don't have formats) can have, and then try them one by one.
    Of course, there can be some ambiguity. Does "123,456" represent one hundred twenty three thousand four hundred fifty six, or is that comma a decimal separator, and it represents one hundred twenty three and four hundred fifty six thousandths?
    The point is, you have to have a clear definition of what formats are allowed and how to determine which one it is. There's no magic way for Java to do that for you.

  • Comparing two queries yields one result too many

    I have a problem that I'm pretty sure is resident in the
    structure of a loop, but I'm not quite sure how to fix it.
    All of this is being done within a cfc. The cfc calls the
    first method for Query1, then calls the second method for Query2.
    Query1 has 173 records, Query2 has 117 records. Technically the
    difference should be 56 records.
    However, the result of myquery (below) is giving me 57
    records. And every one of them is a real record. Code as follows:
    <CFSET myquery = QueryNew("var1, var2, var3, var4,
    var5")>
    <CFLOOP INDEX="i" FROM="1" TO="#Query1.recordcount#">
    <CFQUERY NAME="checkJob" DBTYPE="query">
    SELECT var1
    FROM Query2
    WHERE var1 = <cfqueryparam cfsqltype="cf_sql_varchar"
    value="#Query1.var1
    #">
    </CFQUERY>
    <CFIF checkJob.recordcount lte 0>
    <cfset newRow = QueryAddRow(myQuery, 1)>
    <cfset temp = QuerySetCell(myQuery, "var1",
    Query1.var1)>
    <cfset temp = QuerySetCell(myQuery, "var2", Query1.var2
    )>
    <cfset temp = QuerySetCell(myQuery, "var3",
    Query1.var3)>
    <cfset temp = QuerySetCell(myQuery, "var4", Query1.var4
    )>
    <cfset temp = QuerySetCell(myQuery, "var5",
    Query1.var5)>
    </CFIF>
    </CFLOOP>
    <CFRETURN myQuery>
    So if all is done correctly, I should be getting the results
    from Query1 that are NOT in Query2.
    It's *almost* right.
    Since var1 exists in all of the records returned by myquery,
    one presumes that there's an extra record being returned that *IS*
    in Query2.
    I'm not sure why, though.
    The resultant screen needs to print out all of the variables
    from query1 that do not exist in query2. Hence, myquery.
    Anyone have a better recommendation on how to fix this
    problem? I feel like a goober for even asking it, but it's been
    annoying me all afternoon.
    Rizados

    Simon, I would appreciate not being called lazy and yet
    overly complicated in the same sentence. Ignorant, perhaps, overly
    complicated, very likely, but lazy isn't the word I'd use.
    When it comes to MSSQL, I was actually unaware I could use a
    query of queries approximation without writing the whole thing out.
    Thank you so much for correcting my ignorance. My apologies if this
    isn't the type of database I've been using for the last few years,
    and if my familiarity with the database is not as great as yours.
    I have been trying to componentize this application where
    applicable, and the queries I speak of were already componentized
    prior to me coming into this. Both the primary and secondary
    queries are being used by multiple applications on the same server,
    and multiple times in the same page with different parameters (the
    same page is being used several times). This third query is
    obviously the difference between the two. If the second query has
    to change at any time, then changing it only once within a
    component makes more sense than having to find it multiple times
    within the code to change it again, since the business rules on
    said query have had to change a few times before. Better to have it
    in one place than to change it in multiple places and risk having
    one be changed while the others are not.
    Lazy has nothing to do with it so much as accuracy. Multiple
    instances of the same code means having to find it multiple times
    when the code needs to change, and risking that you may miss one.
    If they allowed me to use a view to do it as recommended by
    cf_dev2, I would do so, and it would make my life a lot less
    complicated. However, that option is not available to me, therefore
    I have to go the "more complicated" route.

  • Compare two months with time-dependent master data

    Hi gurus,
    I have the requirement to make a query that compares values from two months but the key figures are restricted by a time-dependent atributte, I created a structure restricted with an Exit-variable that fills the month in course and the month before, the issue is that query designer only accepts individual variables so only one month displays data.
    Is there a way to use several key-dates so the time-dependent attributes can be used for especific months?
    Regards,
    Gerardo.

    Thanks Raj,
    If there´s no other option I will include attributes in cube. Check if points are correctly assigned
    Regards,
    Gerardo

  • Compare two invoices with same distribution line count

    I am trying to pull data out of Oracle Payables - invoices for which the invoice amount ,the vendor and distribution line count is same.
    I could achieve pulling invoices with same Vendor having same amount.But finding hard to compare the counts.
    Can anyone share ideas on how to achieve this ... Tried self join but did not work.
    The query which I used is as follows :
    select invoice_num,invoice_id,invoice_amount,vendor_id,
    (select vendor_name from apps.po_vendors where vendor_id=aia.vendor_id) vendor_name,
    (select count(*) from apps.ap_invoice_distributions_all aid where aid.invoice_id=aia.invoice_id) line_count
    from apps.ap_invoices_all aia
    where invoice_amount in (select aiab.invoice_amount
    from apps.ap_invoices_all aiab
    where aiab.creation_date >='01-AUG-2012'
    and vendor_id=aia.vendor_id
    group by aiab.invoice_amount
    Having (count(aiab.invoice_amount) >1))
    and aia.creation_date >='01-AUG-2012'
    Thanks in Advance.

    I did try your query with sample records and counts are also correct plz chk the following, for me counts are correct as per your logic -
    select aia.invoice_num,aia.invoice_id,aia.invoice_amount,aia.vendor_id,
    (select vendor_name from
    (select 'XX' vendor_name, 'A' vendor_id from dual union all
    select 'XY' vendor_name, 'B' vendor_id from dual union all
    select 'XZ' vendor_name, 'C' vendor_id from dual union all
    select 'XA' vendor_name, 'D' vendor_id from dual ) po
    where vendor_id=aia.vendor_id) vendor_name,
    (select count(*) from
    (select 1 invoice_id from dual union all
    select 1 invoice_id from dual union all
    select 1 invoice_id from dual union all
    select 2 invoice_id from dual union all
    select 3 invoice_id from dual ) aid
    where aid.invoice_id=aia.invoice_id) line_count
    from
    select 10 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 11 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 12 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 13 invoice_num, 2 invoice_id,100 invoice_amount, 'B' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 14 invoice_num, 2 invoice_id,100 invoice_amount, 'B' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 15 invoice_num, 3 invoice_id,100 invoice_amount, 'C' vendor_id ,'01-SEP-2012' creation_date from dual union all
    select 16 invoice_num, 4 invoice_id,100 invoice_amount, 'D' vendor_id,'01-OCT-2012' creation_date from dual) aia
    where aia.invoice_amount in (select aiab.invoice_amount
    from
    select 10 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 11 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 12 invoice_num, 1 invoice_id,100 invoice_amount, 'A' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 13 invoice_num, 1 invoice_id,100 invoice_amount, 'B' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 14 invoice_num, 1 invoice_id,100 invoice_amount, 'B' vendor_id ,'01-AUG-2012' creation_date from dual union all
    select 15 invoice_num, 1 invoice_id,100 invoice_amount, 'C' vendor_id ,'01-SEP-2012' creation_date from dual union all
    select 16 invoice_num, 1 invoice_id,100 invoice_amount, 'D' vendor_id,'01-OCT-2012' creation_date from dual) aiab
    where aiab.creation_date >='01-AUG-2012'
    and aiab.vendor_id=aia.vendor_id
    group by aiab.invoice_amount
    Having (count(aiab.invoice_amount) >1))
    and aia.creation_date >='01-AUG-2012'
    o/p
    INVOICE_NUM,INVOICE_ID,INVOICE_AMOUNT,VENDOR_ID,VENDOR_NAME,LINE_COUNT
    10,1,100,A,XX,3
    11,1,100,A,XX,3
    12,1,100,A,XX,3
    13,2,100,B,XY,1
    14,2,100,B,XY,1
    -------

  • Comparing two variables with if statements

    I'm working with 2 global variables and I need to compare them on 6 different button function clicks for a lot of different scenarios.
    Here is an example of what I have for 1 button:
    my Btn.onPress = function() {
    _global.firstVar = 19;
    _global.firstYear =2009;
    _root.checkAll();
    My function is starting to look like this;
    if ((_global.firstVar == 19) && (_global.firstYear == 2009)){     (first...is this the correct way to write this statement with the && to compare?)
      gotoAndStop(8);
      trace("itworks")
      loadMovie("mymovie.swf", 1);
    else if..........
    only problem i see is that there are many different scenarios and this if statement could get very long.
    Is there a way to combine it somehow so there wouldn't be that many else if's?

    That is most likely the way to write it, but I have the feeling that you are making it much more complex than it needs to be. What are you actually trying to do?
    How are firstVar and firstYear being set? Can they be any number at all or just some specific ones?
    Using _global variables isn't really a best practice and should only be done the most unusual of circumstances. There are some tricky things about the _global object. Generally they are just a shortcut for getting around scope problems, but if you have those problems it is better to work them out and understand scope that to just put everything in _global. In AS3, the _global object has gone away—unless you make your own....
    Also depending upon what you are doing with the loading of the swf, you probably should look into using the MovieClipLoader instead of a loadMovie. With loadMovie there is no easy way to know when the clip has finished loading, unless you write your own preloader code. With the MovieClipLoader class there is a nice simple event called onInit. You can also use the onLoadProgress event to know how much of your external asset has loaded.
    Also using gotoAndStop(8) or some other number is likely to become confusing later down the line. You might want to use labels and names. If you have a lot of different scenarios then maybe give them names. That way if the timeline changes or you come back in 3 months it will generally be easier to track down:
    gotoAndStop('midYearReviewScenario")
    rather than
    gotoAndStop(14);
    Just a suggestion from years' of experiences.
    And finally I'm with Ned. Generally for buttons onRelease is a better event to do navigation and other results things that happen upon a click. onPress is really for things where you need to press and hold the button.

  • Comparing two images with Imaging Lingo

    Hi everybody!
    Here's the deal: I have two grayscale images and I want to
    find out where they match the closest. I want to compute the
    difference between the two and find the spot with the least
    difference.
    So I copy one into the other with the #ink: 38 ("subtract")
    and wherever is the darkest spot the images are closest alike.
    Right? Wrong!
    When Director subtracts one color from the other and the
    result is below 0, Director adds another 256 so the result is
    always between 0 and 255. When I use "subtract pin" the result is
    almost completely black, so this doesn't help as well. I thought
    about simulation high dynamic range images with some stupid hacks
    but to no avail.
    Where am I thinking wrong? Can I compute the difference with
    Imaging Lingo? Any help is greatly appreciated.

    Here's an idea: use reverse ink to copy one image onto the
    other. The areas which are most nearly white in the resulting image
    will be the closest match. You can adapt the code found at
    nonlinear.openspark.com
    to isolate the pixels that are nearly white.

  • How to compare two strings with each other?

    Hello,
    I have modeled a formular which has a radio group and a button. Furthermore, I have one result state corresponding to each option in the radio group.
    In order to determine which result state actually is to be reached I want to attach a condition to each flow to the result states that tests which option has been chosen.
    Which operator can I use for writing these conditions? I have tried the "like" operator as well as the "==" operator but neither seems to work.
    I have written something like:
    =LIKE(@decision, 'option a')
    as well as:
    =(@decision=='option a')
    What am I doing wrong here?
    Thank you very much
    Alexander

    Hi Alexander,
    Could this be an Upper/Lower case issue.
    I tried the following expression
    =(@TXT1=="opt a")
    and it worked.
    If you want your expression to ignore case you should use this expression:
    =(UPPER(@TXT1)==UPPER("opt a"))
    In case you're not sure what is the exact string in the field (@decision) you can always use the PROMPT action to display the field's value.
    Regards,
    Udi

Maybe you are looking for

  • "The disk you have inserted is not readable by the computer"

    I get this error on startup of my new mac mini. I believe the error is that I was time machine backing-up to a drive that no longer exists. It still lists in time machine from system preferences, but I cannot remove it from the listing of backup driv

  • Problems with Skin display and publish options on FrameMaker 12

    Hi, Customising the FrameMaker output for web help is proving problematic. 1. Here's the first issue - The Online FrameMaker help says the following: "You can also import WebHelp or compressed WebHelp skins defined in .skn or .zkn files, respectively

  • Cursor within the cursor showing error

    Hi, I am using a cursor within the cursor to create a procedure.I am getting the below error while compiling this 15/7 PL/SQL: SQL Statement ignored 16/14 PL/SQL: ORA-00942: table or view does not exist 79/10 PL/SQL: Statement ignored 82/31 PLS-00364

  • HT5188 I've created an itunes account without credit card, but I can't buy any free apps. Could anyone tell me why.

    Recently I've created a new itunes account without credit,but unfortunately fail to buy any free apps. When I press the "download" button, a pop-up appear and ask me to complete the billing info. The point is that many of my friends can buy free apps

  • Confirmed Quantity not displayed in SO

    Hi All, Recently we went live on ECC 6.0 and now we are using APO for availability. We have an issue: A sales order contains 5 line items. The first line item is confirmed for Feb mid. the other items are not confirmed while creating the SO. User lat