Results 1 to 5 of 5

Thread: How to find the difference of DATA

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    How to find the difference of DATA

    Hello,

    I have 3 Excel sheet on which i have 3 Columns each,

    1.UserName 2. Name 3. Contact#

    I paste some data on Sheet1 in morning and some data on Sheet 2 in evening, How can i got the difference of data on Sheet 3.

    Like on sheet 1, I paste only username A, B, C, D, E in morning and A, B, C, D, E, F, G, H, I on Sheet 2 in evening.

    How can i get F, G, H, I on sheet 3.

    Please help me.

    FAROOQ

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to find the difference of DATA

    Here is a basic example. you will have to modify this to suit your code.

    to understand this, type a-e in Col A in Sheet1 starting from A2 and a-h in Col A in Sheet2 starting from A2. Run this code and it will populate the difference in Sheet3

    Code:
    Option Explicit
    
    Sub Diff()
    
    Dim Rng1 As Range, Rng2 As Range
    Dim cl1 As Range, cl2 As Range
    Dim Flg As Boolean
    Dim i As Integer
    
    Flg = False
    'Change this number to the row
    'number from where you want the
    'data to be stored in sheet3
    'for Example A2
    i = 2
    
    'Your range in Sheet1 in Column A in Sheet1
    Set Rng1 = Sheets("Sheet1").Range("A2:A6")
    'Your range in Sheet1 in Column A in Sheet2
    Set Rng2 = Sheets("Sheet2").Range("A2:A9")
    
    For Each cl2 In Rng2
        For Each cl1 In Rng1
            If cl1.Value = cl2.Value Then
                Flg = False
                Exit For
            Else
                Flg = True
            End If
        Next
            
        'if no match found store value
        If Flg = True Then
            Sheets("Sheet3").Range("A" & i).Value = cl2.Value
            i = i + 1
            Flg = False
        End If
    Next
    
    End Sub
    If you have any queries, please feel free to ask.

    Hope this puts you in the right direction....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to find the difference of DATA

    This code works much faster and works with any number of records in Sheet1 and Sheet2.
    Code:
    Option Explicit
    
    Sub GetNewRecords()
       Dim RangeToCopy As Range
       Dim UserNameRange As Range
       Dim aCell As Range
       
       Set aCell = Sheet2.Range("A2")
       Set UserNameRange = Sheet2.Range(aCell, aCell.End(xlDown))
       Set RangeToCopy = Sheet2.Range("A1:C1")
       For Each aCell In UserNameRange
          If Sheet1.Columns(1).Find(What:=aCell, LookAt:=xlWhole) Is Nothing Then
             Set RangeToCopy = Union(RangeToCopy, Sheet2.Range(aCell, aCell.Offset(0, 2)))
          End If
       Next
       Sheet3.Cells.ClearContents
       RangeToCopy.Copy Sheet3.Range("A1")
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: How to find the difference of DATA

    How to use this code in EXCEL ??? I m ery new to Excel

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to find the difference of DATA

    In Excel VBA Editor, insert a Module then copy the code above to paste into the new Module.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width