Advertisement
Guest User

Compare Cells in Different Workbooks

a guest
May 21st, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Find_Tag()
  2.  
  3. ' This script compares a specified data sheet against the OURS data sheet, and finds discrepancies
  4. ' based on column headings.
  5. ' Project Creator: XXX
  6. ' Date Created: 5/9/2012
  7.  
  8. ' The user of this script must create 3 sheets to be used by the script, in the workbook where the variable
  9. ' worksheet resides. These sheets must then have their names stored in the variables
  10. ' s_WS_SheetName, s_WS_FileName, and s_WS_DataSheetName.
  11. ' The user of this script must also ensure that in the worksheet being used to compare
  12. ' against OURS.xlsx, it must contain headings with the same heading names. Heading locations
  13. ' do not matter. There must also be a column heading named PERMANENT_TAG_NUMBER in order
  14. ' to identify which rows in s_WS_SheetName goes to which row in OURS.
  15.  
  16. ' In order to use this script effectively, there must be two workbooks opened, one of which must be named ' OURS.xlsx and have a sheet named Sheet1 to be compared to.
  17. ' The next workbook can be whatever you want to compare against OURS workbook, Sheet1 worksheet.
  18. ' In the next workbook, there must be two sheets, one called Data_Sheet for acting as temporary storage
  19. ' and Final Sheet, where the discrepancies are output.
  20.  
  21. ' Modified 5/21/2012 by Tom Shallenberger
  22. ' Added s_WS_tag_col_num and char in order to make it easier to obtain
  23. ' col letter and number for the PERM_TAG_NUM col location.
  24.  
  25.  
  26.  
  27. Dim s_WS_Tag, s_ours_address As String
  28. Dim s_OURS_row, s_WS_row, s_OURS_col, s_WS_col As String
  29. Dim OURS_string, s_WS_string As String
  30.  
  31. Dim s_WS_Filename, s_WS_SheetName, s_WS_DataSheetName, s_WS_tag_col As String
  32. Dim i_WS_colCount, i_WS_rowCount As Integer
  33.  
  34. ' **************************************************
  35. s_WS_Filename = "Wendy.xls"                         ' Remember to fill these
  36. s_WS_SheetName = "Outside DC Scope"                 ' in with the right names!
  37. s_WS_DataSheetName = "Data_Sheet"                 ' This doesn't need to be changed unless needed
  38. s_WS_tag_col = "PERMANENT_TAG_NUMBER"             ' This is what identifies the rows
  39. ' **************************************************
  40.  
  41. Dim s_WS_tag_col_num, s_WS_tag_col_char As String
  42.  
  43. Dim c As Range
  44. Dim testchar As String
  45. Dim s_WS_row_one, OURS_row_one_s As String
  46. Dim OURS_row_a, v_WS_Array As Variant
  47. Dim concat_string As String
  48.  
  49. Dim Test_Compare As Integer
  50.  
  51. i_WS_colCount = 0
  52. i_WS_rowCount = 0
  53. i_WS_colCount = Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).UsedRange.Columns.Count     ' assigns the number of
  54. i_WS_rowCount = Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).UsedRange.Rows.Count        ' col and rows to integers.
  55.  
  56.  
  57.  
  58. For i = 2 To i_WS_colCount ' going from the 2d col over, searching the columns in the first
  59.                ' row and storing the corresponding col letters
  60.            '*****************************************************
  61.            
  62.             ' Assign the values, one at a time, in DC to be verified row 1 col i to wendy-row_s
  63.            s_WS_row_one = Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).Cells(1, i).Value
  64.             Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 1).Value = s_WS_row_one
  65.            
  66.             ' ****************************************************
  67.            
  68.             ' v_WS_Array takes the substrings from the above address that have been split
  69.            v_WS_Array = Split(Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).Cells(1, i).Address(), "$", -1)
  70.            
  71.             ' ****************************************************
  72.            
  73.             ' assign a cell in Data_Sheet the second string in the variant array,
  74.            ' which would be the col letter in the address
  75.            If s_WS_row_one = s_WS_tag_col Then
  76.                 s_WS_tag_col_char = v_WS_Array(1)
  77.                 s_WS_tag_col_num = Asc(s_WS_tag_col_char) - 64
  78.             End If
  79.             Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 2) = v_WS_Array(1)
  80.            
  81.             '******************************************************
  82.            
  83.             ' Using the following worksheet and range
  84.            With Workbooks("OURS.XLSX").Sheets("Sheet1").Range("A1:AH1")
  85.            
  86.             '******************************************************
  87.            
  88.             ' Find the range/cell that contains s_WS_row_one
  89.            Set c = .Find(s_WS_row_one)
  90.                 If c Is Nothing Then
  91.                     ' If it's nothing, put a dash in the data sheet
  92.                    Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 3).Value = "-"
  93.                 Else
  94.                     ' if it does exist, split the OURS address and assign it to a variant array
  95.                    OURS_row_one_a = Split(.Find(s_WS_row_one).Address(), "$", -1)
  96.                     ' assign the value in the data sheet the OURS col letter
  97.                    Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 3).Value = OURS_row_one_a(1)
  98.                 End If
  99.                
  100.             End With
  101.            
  102.             '********************************************************
  103.    Next
  104.    
  105.     For i = 2 To i_WS_rowCount ' Going from the top row down to the last row
  106.    
  107.         '********************************************************
  108.    
  109.         ' Select the range on the specified sheet
  110.        Application.Goto Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).Range(s_WS_tag_col_char & "1:" & s_WS_tag_col_char & CStr(i_WS_rowCount))
  111.        
  112.         '********************************************************
  113.        
  114.         ' put the tag number in col 1, variable row, in s_WS_Tag
  115.        s_WS_Tag = Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).Cells(i, CInt(s_WS_tag_col_num)).Value
  116.        
  117.         '********************************************************
  118.        'assign a cell in the data sheet the tag stored above
  119.        Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 5).Value = s_WS_Tag
  120.        
  121.         '********************************************************
  122.        ' With the following workbook/sheet/range
  123.        With Workbooks("OURS.XLSX").Sheets("Sheet1").Range("E1:E500")
  124.        
  125.        
  126.                 ' Find the range or cell that contains the tag
  127.               Set c = .Find(s_WS_Tag)
  128.         End With
  129.        
  130.         '********************************************************
  131.        
  132.         If c Is Nothing Then    ' If the tag doesnt exist
  133.            ' assign val to data sheet saying it doesnt exist
  134.            Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 4).Value = "N/A"
  135.         Else
  136.             ' Else, assign the value to the data sheet saying it has been found
  137.            Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 4).Value = "Located"
  138.             ' assign the address of the tags cell to s_ours_address
  139.            s_ours_address = c.Address()
  140.             ' use the OURS variant again to split the address and parse the row number
  141.            OURS_row_a = Split(s_ours_address, "$", 3)
  142.             ' parse the row number into OURS_row
  143.            s_OURS_row = OURS_row_a(2)
  144.             ' put the row number in the data sheet
  145.            Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 6).Value = s_OURS_row
  146.            
  147.         End If
  148.        
  149.         '********************************************************
  150.    Next
  151.    
  152.     For i = 2 To i_WS_rowCount ' going from the top row to the bottom row
  153.    
  154.         '********************************************************
  155.        
  156.         s_WS_row = i     ' Starting with the first row in the sheet being compared (wendy)
  157.        testchar = Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 6).Value
  158.        
  159.         '********************************************************
  160.        
  161.         If testchar <> "" Then
  162.        
  163.             s_OURS_row = Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(i, 6).Value      ' Taking the OURS row num from the data sheet
  164.            ' In Final_Sheet, first column, variable row, placing tag number from data sheet
  165.            Workbooks(s_WS_Filename).Sheets("Final_Sheet").Range("A" & i).Value = Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Range("E" & i)
  166.            
  167.             '********************************************************
  168.            
  169.             For j = 2 To i_WS_colCount ' Going from 2d column to last col
  170.                
  171.                 s_WS_col = Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(j, 2).Value ' the compared sheets col is stored in the first col in the data sheet
  172.                s_OURS_col = Workbooks(s_WS_Filename).Sheets(s_WS_DataSheetName).Cells(j, 3).Value  ' the OUR sheet col is stored in the second col in the data sheet
  173.                
  174.                 '********************************************************
  175.                
  176.                 If s_OURS_col <> "-" Then   ' If there isn't a dash marking where there isnt a col letter
  177.                    ' Store the values in the cell marked by the corresponding col and row letters and numbers
  178.                    
  179.                     s_WS_string = Workbooks(s_WS_Filename).Sheets(s_WS_SheetName).Range(s_WS_col & s_WS_row).Value
  180.                    
  181.                     OURS_string = Workbooks("OURS.xlsx").Sheets("Sheet1").Range(s_OURS_col & s_OURS_row).Value
  182.                    
  183.                     If s_WS_string = "" And OURS_string = Empty Then
  184.                         OURS_string = ""
  185.                     End If
  186.                    
  187.                
  188.                 End If
  189.                
  190.                 '********************************************************
  191.                
  192.                 Test_Compare = StrComp(s_WS_string, OURS_string) ' compare the strings
  193.                
  194.                 '********************************************************
  195.                
  196.                 If Test_Compare <> 0 Then   ' store the strings if they dont match. concat them and format to make em look pretty.
  197.                    concat_string = "[" & s_WS_string & "] [" & OURS_string & "]"
  198.                     Workbooks(s_WS_Filename).Sheets("Final_Sheet").Range(s_WS_col & s_WS_row).Value = concat_string ' output them to final_sheet in specified cells.
  199.                End If
  200.                
  201.                 '********************************************************
  202.            Next
  203.         End If
  204.     Next
  205.        
  206. Workbooks(s_WS_Filename).Sheets("Final_Sheet").Activate
  207.  
  208.        
  209.    
  210.    
  211.    
  212. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement