473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Compare two Table(Any efficient way)

9
I wrote a code as follows to compare 2 tables on diff worksheet.
Each work sheet has about 6000 Rows.
The code works but appears to Work very slow.
Is there a more efficient way to do it.

Expand|Select|Wrap|Line Numbers
  1. Sub MergeTable()
  2. '
  3. ''
  4. Dim ROW1 As Long
  5. Dim ROW2 As Long
  6. Dim C1 As String
  7. Dim C2 As String
  8. Dim C3 As String
  9. Dim C4 As String
  10. ROW1 = 2
  11. Do While Sheets("TABLE2").Cells(ROW1, 1).Value <> ""
  12.   ROW2 = 2
  13.   Do While Sheets("TABLE1").Cells(ROW2, 1).Value <> ""
  14.   C1 = Mid(Sheets("TABLE2").Cells(ROW1, 1), 1, 6)
  15.   C2 = Mid(Sheets("TABLE1").Cells(ROW2, 1), 1, 6)
  16.  
  17.   If ((Sheets("TABLE2").Cells(ROW1, 2).Value = Sheets("TABLE1").Cells(ROW2, 2).Value) And (C1 = C2)) Then
  18.  
  19.    Sheets("TABLE2").Cells(ROW1, 3).Value = Sheets("TABLE1").Cells(ROW2, 1).Value
  20.  
  21.    End If
  22.  
  23.    ROW2 = ROW2 + 1
  24.   Loop
  25.   ROW1 = ROW1 + 1
  26. Loop
  27.  
  28. End Sub
  29.  
  30.  
  31.  
TABLE1

Expand|Select|Wrap|Line Numbers
  1. LABEL          ID1    TAG
  2. ABA001_X1      1
  3. ABA001_X2      1 
  4. ABA001_X3      2
  5. ABA001_X4      1
  6. ABA001_X5      1
  7. ABA001_X6      2
  8. ABA002_X1      3
  9. ABA002_X2      4
  10.  
TABLE2
Expand|Select|Wrap|Line Numbers
  1. LABEL          ID1 
  2. ABA001_1        1
  3. ABA001_2        2 
  4. ABA002_1        3
  5. ABA002_2        4
  6.  
EXPECTED RESULT IN
TABLE1
Expand|Select|Wrap|Line Numbers
  1. LABEL          ID1    TAG
  2. ABA001_X1      1       ABA001_1 
  3. ABA001_X2      1       ABA001_1 
  4. ABA001_X3      2       ABA001_2
  5. ABA001_X4      1       ABA001_1 
  6. ABA001_X5      1       ABA001_1 
  7. ABA001_X6      2       ABA001_2
  8. ABA002_X1      3       ABA002_1
  9. ABA002_X2      4       ABA002_1
  10.  
Nov 7 '06 #1
3 1459
Killer42
8,435 Expert 8TB
I wrote a code as follows to compare 2 tables on diff worksheet.
Each work sheet has about 6000 Rows.
The code works but appears to Work very slow.
Is there a more efficient way to do it.
...
You might be better off using one of Excel's built-in lookup functions to populate the Tag column. For example, I recreated your two tables from the data shown in your post, but in TABLE2 I reversed the order of the two columns. Then I used this formula in cell C2 of TABLE1, I placed this formula =VLOOKUP(B2,TABLE2!$A$2:$B$5,2,FALSE). I copied this cell and pasted it down the rest of the column, and this is the result...
Expand|Select|Wrap|Line Numbers
  1. LABEL        ID1    TAG
  2. ABA001_X1     1    ABA001_1
  3. ABA001_X2     1    ABA001_1
  4. ABA001_X3     2    ABA001_2
  5. ABA001_X4     1    ABA001_1
  6. ABA001_X5     1    ABA001_1
  7. ABA001_X6     2    ABA001_2
  8. ABA002_X1     3    ABA002_1
  9. ABA002_X2     4    ABA002_2
(edited slightly for formatting here)
Nov 9 '06 #2
DONE1
9
Thank you so much
Nov 15 '06 #3
Killer42
8,435 Expert 8TB
Thank you so much
No problem. Let us know whether it works.
Nov 15 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are...
5
by: aboycalled3 | last post by:
I'm interested in using the fascinating CSS available at http://www.moronicbajebus.com/playground/cssplay/reformat-table/ which allows one to present tabular data in a way that's more appealing...
1
by: Good Man | last post by:
Hi there I'm developing a large web application. Part of this web application will be storing numerical chart data in a MySQL table - these numbers will be already calculated, and are just being...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
12
by: Martin Heuckeroth | last post by:
Hi Any idea on how to get a row number from the original table? We do a query and get a result. The row number from the result is different from the rownumber of the table the result originated...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
2
by: ChasW | last post by:
I am trying to get a correct recordcount from a recordset that references a linked table as the source. Here is the code: Dim db1 As DAO.Database Dim rs1 As DAO. Recordset Set db1 =...
3
Ganon11
by: Ganon11 | last post by:
Hey everyone, For a class assignment, I am making a class scheduling CGI program/script. I have everything working so far except actually displaying the schedule as a grid/2D table. I am not...
2
by: AlfredoAMD | last post by:
So I've been working on this for over a week now and I'm still totally stumped. Basically I receive a sales report weekly which has several fields such as "Opportunity # (Unique)", "Planned Close...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.