+ Reply to Thread
Results 1 to 3 of 3

walk me through this code for comparing values in a column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    walk me through this code for comparing values in a column

    Hello. Could someone walk me through with a LOT of of detail what this code is doing and what each piece of the code means? Thanks!!

    Sub comparevalues()
    Dim sws, dws As Worksheet
    Dim rng, rng1, cell As Range
    Dim lr As Long
    Dim n As Integer
    Set sws = ThisWorkbook.Sheets("Sheet1")
    Set dws = ThisWorkbook.Sheets("Sheet2")
    sws.Activate
    lr = sws.Cells(Rows.Count, 2).End(xlUp).Row
    Set rng = sws.Range("B2:B" & lr)
    Set rng1 = sws.Range("P2:P" & lr)
    Application.ScreenUpdating = False
    sws.Range("B1:C1").Copy dws.Range("A1")
    sws.Range("P1:Q1").Copy dws.Range("C1")
    For Each cell In rng
        On Error Resume Next
        n = WorksheetFunction.Match(cell, rng1, 0)
        If Err = 0 Then
            If cell.Offset(0, 1) <> Cells(n + 1, "Q") Then
                Cells(n + 1, "R") = "Wrong Amount"
                cell.Resize(1, 2).Copy dws.Range("A" & Rows.Count).End(3)(2)
                Cells(n + 1, "P").Resize(1, 2).Copy dws.Range("C" & Rows.Count).End(3)(2)
            End If
        End If
        Err = 0
    Next cell
    Columns("R:S").AutoFit
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: walk me through this code for comparing values in a column

    I think this will help you if you are not familiar with walking through code.
    http://www.cpearson.com/excel/DebuggingVBA.aspx
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: walk me through this code for comparing values in a column

    I think some of this code is from Excel 2003, and I am a newb when it comes to VBA but I have commented about every line of code.

    Copy and paste this into the VBA editor to have it display the comments in a different color

    Sub comparevalues()
    
    '=============================================
    'Declare Variables
    '=============================================
    Dim sws As Worksheet
    Dim dws As Worksheet
    Dim rng As Range
    Dim rng1 As Range
    Dim cell As Range
    Dim lr As Long
    Dim n As Integer
    
    '=============================================
    'Define Variables
    '=============================================
    Set sws = ThisWorkbook.Sheets("Sheet1") 'This sets the variable sws to "Sheet1" in this workbook
    Set dws = ThisWorkbook.Sheets("Sheet2") 'This sets the variable sws to "Sheet2" in this workbook
    sws.Activate 'this activates sws (or Sheet1)
    lr = sws.Cells(Rows.Count, 2).End(xlUp).Row 'This declares lr as the last used cell in column B by starting from the bottom and going up to the first cell used
    Set rng = sws.Range("B2:B" & lr) 'this defines rng as B2:B(Last Row number used)
    Set rng1 = sws.Range("P2:P" & lr) 'this defines rng1 as P2:P(last rell used just like above)
    
    '==========================================================================================================
    ' Example to clarify in comments directly below
    '==========================================================================================================
    'So the easy way to explain the above is lr probably stands for LASTROW and it finds the last row used
    '    by going to the very bottom of column B then going up to find the first non empty cell
    '        For simplicity lets assume lr=650
    '    So once we declared lr, then you then define rng as B2:B650 and rng1 as P2:P650
    'All in that is all used to allow for a dynamic range that will adjust as you get more rows used
    '==========================================================================================================
    
    Application.ScreenUpdating = False ' 'turns Off Screen updating for SPEED mostly
    
    sws.Range("B1:C1").Copy dws.Range("A1") 'This copies Sheet1 cells B1:C1 to Sheet2 cells A1:B1
    sws.Range("P1:Q1").Copy dws.Range("C1") 'This copies Sheet1 cells P1:Q1 to Sheet2 cells C1:D1
    
    '=============================================
    'Begin loop
    '=============================================
    For Each cell In rng 'This says loop through EACH cell in the defined range "rng"
        On Error Resume Next 'If there is an error, simply go to the next cell in the range "rng"
        
        n = WorksheetFunction.Match(cell, rng1, 0) 'This declares variable "n" (which was NOT defined above!) as the matching row # in rng1
        If Err = 0 Then 'If no error then proceed with the following
            If cell.Offset(0, 1) <> Cells(n + 1, "Q") Then 'This verifies that the cell to the right of the active cell is NOT equivalent to the column Q and row below "n" defined above
            'If above statement is true, then do the following
                Cells(n + 1, "R") = "Wrong Amount" 'In column R and row under the "n" type "Wrong Amount"
                cell.Resize(1, 2).Copy dws.Range("A" & Rows.Count).End(3)(2) 'this selects active cell and one row down and 2 columns to the right _
                                                                            'Then copies it and pastes it to Sheet2 A and bottom of the data
                Cells(n + 1, "P").Resize(1, 2).Copy dws.Range("C" & Rows.Count).End(3)(2)
            End If 'Ends the if statment
        End If
        Err = 0
    Next cell 'This goes to the next cell in the range "rng
    
    '=============================================
    'End Macro procedures
    '=============================================
    Columns("R:S").AutoFit 'This auto adjusts the width of the columns
    Application.ScreenUpdating = True 'Turns Screen Updating back on
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Walk through code that copies data
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2014, 05:55 PM
  2. Replies: 19
    Last Post: 03-15-2014, 04:50 PM
  3. Replies: 3
    Last Post: 10-15-2012, 10:42 AM
  4. Grouping rows based on column values and comparing row values
    By bernborough in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2010, 10:18 AM
  5. [SOLVED] Comparing column values
    By Mark Christensen in forum Excel General
    Replies: 2
    Last Post: 01-18-2006, 02:50 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1