I need to compare 2 excel sheets (Sheet1 (old report) & Sheet2 (new report)) for differences. If there are any additions or removals in Sheet2 compared to Sheet1 I need to print that.
I found this script to find the differences but this is not including the removals in the sheet. Can you help fixing this? Below is sample example on my expectation.
Sheet1:
S.No Name Class
abc1 1st
abc2 1st
abc3 1st
Sheet2:
S.No Name Class
abc1 1st
abc2 2nd
abc4 1st
.
Comparison should tell all these:
"Row(3,3)" is changed from "1st" to "2nd"
New row inserted in "sheet2" "Row4"
"Sheet1" "Row4" is deleted in "Sheet2"
Script currently I have:
Sub Compare2Shts()
For Each cell In Worksheets("CompareSheet#1").UsedRange
If cell.Value <> Worksheets("CompareSheet#2").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
For Each cell In Worksheets("CompareSheet#2").UsedRange
If cell.Value <> Worksheets("CompareSheet#1").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
End Sub
Sub CompareAnother2Shts()
For Each cell In Worksheets("CompareSheet#1").Range("A1:J50000")
If cell.Value <> Worksheets("CompareSheet#2").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
For Each cell In Worksheets("CompareSheet#2").Range("A1:J50000")
If cell.Value <> Worksheets("CompareSheet#1").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
End Sub
Sub FindDupes() 'assuming both sheets are in same book and book is open
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim cell1 As Range
Dim cell2 As Range
Dim str As String
str = InputBox("Type name of first sheet")
Set sht1 = Worksheets(str)
str = InputBox("Type name of second sheet")
Set sht2 = Worksheets(str)
sht1.Range("A65536").End(xlDown).Activate
Selection.End(xlUp).Activate
LastRowSht1 = ActiveCell.Row
sht2.Activate
sht2.Range("A65536").End(xlDown).Activate
Selection.End(xlUp).Activate
LastRowSht2 = ActiveCell.Row
sht1.Activate
For rowSht1 = 1 To LastRowSht1
If sht1.Cells(rowSht1, 1) = "" Then Exit Sub
For rowSht2 = 1 To LastRowSht2
If sht1.Cells(rowSht1, 1).Value = sht2.Cells(rowSht2, 1).Value Then
sht1.Cells(rowSht1, 1).Interior.ColorIndex = 3
sht2.Cells(rowSht2, 1).Interior.ColorIndex = 3
End If
Next
Next
sht1.Cells(1, 1).Select
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub checkrev()
With Sheets("Sheet1")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
With Sheets("Sheet2")
Sh2LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh2Range = .Range("A1:A" & Sh2LastRow)
End With
'compare sheet 1 with sheet 2
For Each Sh1cell In Sh1Range
Set c = Sh2Range.Find( _
what:=Sh1cell, LookIn:=xlValues)
If c Is Nothing Then
Sh1cell.Interior.ColorIndex = 3
Sh1cell.Offset(0, 1).Interior.ColorIndex = 3
Else
If Sh1cell.Offset(0, 1) <> c.Offset(0, 1) Then
Sh1cell.Interior.ColorIndex = 6
Sh1cell.Offset(0, 1).Interior.ColorIndex = 6
End If
End If
Next Sh1cell
'compare sheet 2 with sheet 1
For Each Sh2cell In Sh2Range
Set c = Sh1Range.Find( _
what:=Sh2cell, LookIn:=xlValues)
If c Is Nothing Then
Sh2cell.Interior.ColorIndex = 3
Sh2cell.Offset(0, 1).Interior.ColorIndex = 3
Else
If Sh2cell.Offset(0, 1) <> c.Offset(0, 1) Then
Sh2cell.Interior.ColorIndex = 6
Sh2cell.Offset(0, 1).Interior.ColorIndex = 6
End If
End If
Next Sh2cell
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub TestCompareWorksheets()
' compare two different worksheets in the active workbook
CompareWorksheets Worksheets("Sheet1"), Worksheets("Sheet2")
' compare two different worksheets in two different workbooks
' CompareWorksheets ActiveWorkbook.Worksheets("Sheet1"), _
Workbooks("WorkBookName.xls").Worksheets("Sheet2")
End Sub
Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With ws1.UsedRange
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With ws2.UsedRange
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = "Comparing cells " & Format(c / maxC, "0 %") & "..."
For r = 1 To maxR
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = ws1.Cells(r, c).FormulaLocal
cf2 = ws2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = "'" & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Compare " & ws1.Name & " with " & ws2.Name
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub Match()
r1 = Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row
r2 = Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set r3 = Worksheets("sheet1")
Worksheets("sheet2").Range("B2").Select
For a = 2 To r2
For i = 2 To r1
If Cells(a, "A") = r3.Cells(i, "A") Then
temp = r3.Cells(i, "B")
te = te & "," & temp
Else
End If
Next i
Cells(a, "B") = te
te = ""
Next a
End Sub
Sub Match2()
Dim myCon As String
Dim myCell As Range
Dim cell As Range
For Each cell In Sheet2.Range("A2:A10")
myCon = ""
For Each myCell In Sheet1.Range("A1:A15")
If cell = myCell Then
If myCon = "" Then
myCon = myCell.Offset(0, 1)
Else
myCon = myCon & ", " & myCell.Offset(0, 1)
End If
End If
Next myCell
cell.Offset(0, 1) = myCon
Next cell
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub Duplicates()
ScreenUpdating = False
'get first empty row of sheet1
'find matching rows in sheet 2
With Sheets("Masterfile")
RowCount = 1
Do While .Range("A" & RowCount) <> ""
ID = Trim(.Range("A" & RowCount))
'compare - look for ID in Sheet 2
With Sheets("List")
Set c = .Columns("A").Find(what:=ID, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
End With
If c Is Nothing Then
.Range("B" & RowCount) = "No"
Else
.Range("B" & RowCount) = "Yes"
End If
RowCount = RowCount + 1
Loop
End With
ScreenUpdating = True
End Sub
The code you have looks overly complex.
For a non-vba solution, see below.
Sheet 1 formula:
=IF(ISERROR(VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)),"Removed",IF(VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)=B2,"Same","Changed to: " &VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)))
Sheet 2 formula:
=IF(ISERROR(VLOOKUP(A2,Sheet1!$A$2:$B$4,2,0)),"Added",IF(VLOOKUP(A2,Sheet1!$A$2:$B$4,2,0)=B2,"Same","Changed"))
I realize I may haved simplified things a bit, but you can adjust wording and whatever is needed. You can also apply conditional formatting as needed.
Related
I have a sheet with details regarding orders. In column G a specific value indicates what container (shipping container) the order is packed in.screenshot
I would like all duplicate container no. to be highlighted with different colors and their row with them.
Meaning: that when I have "container no. X" the entire row connected to X is one color and rows connected to "container no. Y" is another color and so on.
I would also like an automatic update of colors when something changes or when I hit "update values" in the data bar
Blank cells in column G should not to be colored.
Is this possible and if so, can someone help me out. I am very much a beginner with VBA.
Sub ColorCompanyDuplicates()
'Updateby Extendoffice
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xChar As String
Dim xCellPre As Range
Dim xCIndex As Long
Dim xCol As Collection
Dim I As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
xCIndex = 2
Set xCol = New Collection
For Each xCell In xRg
On Error Resume Next
If xCell.Value <> "" Then
xCol.Add xCell, xCell.Text
If Err.Number = 457 Then
xCIndex = xCIndex + 1
Set xCellPre = xCol(xCell.Text)
If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
ElseIf Err.Number = 9 Then
MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
Exit Sub
End If
On Error GoTo 0
End If
Next
End Sub
This code does number 1 and 3.
Also, it only uses bright colors.
Sub ColorCompanyDuplicates()
Dim row_start As Long, last_row As Long, color_index As Long
Dim R As Long, last_col As Long, col As Long
Dim used_range As Range, paint_row As Boolean
'CONFIG -------------------------
row_start = 5 'first row of the data set
paint_row = True 'set to false if you want to paint only the column
'--------------------------------
color_index = 33
Set used_range = ActiveSheet.UsedRange
last_col = _
used_range.Columns.Count + used_range.Column - 1
last_row = _
Cells(Rows.Count, 7).End(xlUp).Row
'clean existing rows in container names
For R = row_start To last_row
If Range("g" & R) <> "" Then
Range("g" & R).Value = Split(Range("g" & R).Value, " ")(0)
End If
Next R
'paint duplicates
For R = row_start To last_row
'if the next container name is the same and is not null then paint
If Cells(R, 7) = Cells(R + 1, 7) And Cells(R, 7) <> "" Then
If paint_row Then
For col = used_range.Column To last_col
Cells(R, col).Interior.ColorIndex = color_index
Next col
Else
For col = used_range.Column To last_col
Cells(R, col).Interior.ColorIndex = 0
Next col
Cells(R, 7).Interior.ColorIndex = color_index
End If
'FOR THE LAST ONE in the group
'if previews container name is the same and is not null then paint
ElseIf Cells(R, 7) = Cells(R - 1, 7) And Cells(R, 7) <> "" Then
If paint_row Then
For col = used_range.Column To last_col
Cells(R, col).Interior.ColorIndex = color_index
Next col
Else
For col = used_range.Column To last_col
Cells(R, col).Interior.ColorIndex = 0
Next col
Cells(R, 7).Interior.ColorIndex = color_index
End If
'and change color for the next group
color_index = color_index + 1
'avoid dark colors
If color_index = 46 Then
color_index = 33
End If
End If
Next R
'add row numbers to containers name
For R = row_start To last_row
If Range("g" & R) <> "" Then
Cells(R, 7) = Cells(R, 7) & " ROW:" & R
End If
Next R
End Sub
I would suggest for number 2 just create a refresh button or a command shortcut.
I have the following input:
and would like the following output:
The intended operation is to search column A for duplicate values (column is already sorted). Each duplicate value in A should be merged into 1 cell. Also, merge the same rows in B (take the top value if different, but safe to assume they are the same). Do not touch C.
I'm doing this manually now and it is a huge pain. I am new to VBA but it seems like that would be simple way to speed this up. Any tips?
Sub MergeCells()
'set your data rows here
Dim Rows As Integer: Rows = 20
Dim First As Integer: First = 1
Dim Last As Integer: Last = 0
Dim Rng As Range
Application.DisplayAlerts = False
With ActiveSheet
For i = 1 To Rows + 1
If .Range("A" & i).Value <> .Range("A" & First).Value Then
If i - 1 > First Then
Last = i - 1
Set Rng = .Range("A" & First, "A" & Last)
Rng.MergeCells = True
Set Rng = .Range("B" & First, "B" & Last)
Rng.MergeCells = True
End If
First = i
Last = 0
End If
Next i
End With
Application.DisplayAlerts = True
End Sub
I've done this a few times...
Public Sub MergeDuplicates()
'disable alerts to avoid clicking OK every time it merges
Application.DisplayAlerts = False
'define the range
Dim r As Range
Set r = Sheets("Sheet1").Range("A1:B4")
'need a row counter
Dim i As Long
i = 1
'variables to store the value in A in a row and its upstairs neighbor
Dim this_A As String
Dim last_A As String
'step through the rows of the range
For Each rw In r.Rows
If i > 1 Then 'only compare if this is not the first row - nothing to look backwards at!
'get the values of A for this row and the one before
this_A = rw.Cells(1, 1).Value
last_A = rw.Cells(1, 1).Offset(-1, 0).Value
'compare this A to the one above; if they are the same, merge the cells in both columns
If this_A = last_A Then
'merge the cells in column A
Sheets("Sheet1").Range(r.Cells(i - 1, 1), r.Cells(i, 1)).Merge
'merge the cells in column B
Sheets("Sheet1").Range(r.Cells(i - 1, 2), r.Cells(i, 2)).Merge
End If
End If
i = i + 1 'increment the counter
Next rw
'enable alerts
Application.DisplayAlerts = True
End Sub
Try this, easily adaptible as the range can be modified without changing anything else.
Sub MergeRng
Dim Rng As Range, xCell As Range, WorkRng As Range
Dim xRows As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set WorkRng = Activeworkbook.ActiveSheet.Range("A1:B4")
xRows = WorkRng.Rows.Count
For Each Rng In WorkRng.Columns
For i = 1 To xRows - 1
For j = i + 1 To xRows
If Rng.Cells(i, 1).Value <> Rng.Cells(j, 1).Value Then
Exit For
End If
Next
With WorkRng.Parent.Range(Rng.Cells(i, 1), Rng.Cells(j - 1, 1))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
i = j - 1
Next
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Source:
https://www.extendoffice.com/documents/excel/1138-excel-merge-same-value.html
You have indicated that column A was sorted; it seems to me that both column A and column B should be sorted with column A as the primary key and column B as the secondary key.
Option Explicit
Sub wqwerq()
Dim i As Long, d As Long
Application.DisplayAlerts = False
With Worksheets("sheet3")
With .Cells(1, "A").CurrentRegion
.Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
Key2:=.Columns(2), Order2:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlNo
For i = .Rows.Count To 1 Step -1
If Not .Cells(i, "B").MergeCells Then
d = Application.CountIfs(.Columns(1), .Cells(i, "A"), .Columns(2), .Cells(i, "B"))
If CBool(d - 1) Then
With .Cells(i, "B")
.Resize(d, 1).Offset(1 - d, 0).Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End If
End If
If i = Application.Match(.Cells(i, "A"), .Columns(1), 0) Then
d = Application.CountIfs(.Columns(1), .Cells(i, "A"))
If CBool(d - 1) Then
With .Cells(i, "A")
.Resize(d, 1).Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End If
End If
Next i
End With
End With
Application.DisplayAlerts = True
End Sub
I want to highlight cells when there are 3 or more with the same value, I have the bellow code but it isn't working right because it loop twice into each range. Can someone tell me what is wrong?
Sub HighlightCells()
Dim k As Integer, myCounter As Integer, firstRow As Integer
Dim myClientExport As Worksheet, myTemplate As Variant
Dim vRange As Range, myRange As Range
Dim myAddr As String
Set myClientExport = Excel.ActiveSheet
Set vRange = myClientExport.UsedRange.SpecialCells(xlCellTypeVisible)
firstRow = vRange.Areas(2).Rows(1).Row
myAddr = Cells(firstRow, 4).Value
myCounter = 0
For Each myRange In vRange.Rows
k = myRange.Row
If k > firstRow Then
If myAddr = Cells(k, 4).Value Then
myCounter = myCounter + 1
Else
myAddr = Cells(k, 4).Value
myCounter = 0
End If
Select Case myCounter
Case 3
For i = 0 To 2
OId = Cells(k - i, 1).Value
Next i
Cells(k, 4).Interior.ColorIndex = 27
Cells(k - 1, 4).Interior.ColorIndex = 27
Cells(k - 2, 4).Interior.ColorIndex = 27
Case Is > 3
Cells(k, 4).Interior.ColorIndex = 27
End Select
End If
Next myRange
End Sub
My data unfiltered look like:
My data filtered look like
I want to highlight all Address with 3 or more Active order status.
I can't get your code to work. Right off I get "Application-defined or object-defined" run-time error on firstRow = vRange.Areas(2).Rows(1).Row.
So what I did is a CountIf formula in column E:
=COUNTIF(D$2:D$7,D2) then set Conditional Formatting. If the range is dynamic, use VBA to determine limits and propagate the formula and Conditional Formatting. Something like:
Dim rCount As Integer
rCount = Range("D1", Range("D2").End(xlDown)).Rows.Count
Range("D2:D" & rCount).Select
Selection.Cells.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$E2=3"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("E2:E" & rCount).Select
Selection.Formula = "=CountIf($D$2:$D$" & rCount & ", $D2)"
You may try something like this to see if that works for you.
The code will place a CountIFs formula in column F and delete it in the end.
You may tweak the code as per your requirement.
Sub HighlightFilteredCells()
Dim sws As Worksheet
Dim lr As Long
Dim cell As Range
Set sws = Sheets("Sheet1")
If sws.FilterMode Then sws.ShowAllData
lr = Cells(Rows.Count, 1).End(xlUp).Row
sws.Range("F2:F" & lr).Formula = "=COUNTIFS($D$2:$D$" & lr & ",D2,$E$2:$E$" & lr & ",""Active"")"
sws.Columns(5).Interior.ColorIndex = xlNone
With sws.Rows(1)
.AutoFilter field:=5, Criteria1:="Active"
If sws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
For Each cell In sws.Range("F2:F" & lr).SpecialCells(xlCellTypeVisible)
If cell.Value >= 3 Then cell.Offset(0, -1).Interior.Color = vbYellow
Next cell
End If
End With
sws.Columns(6).Clear
End Sub
I have been trying to only display a combo box list that is not empty but it's not working. Why? Though I add this line "If Not IsEmpty(ActiveCell.Value) Then"
Private Sub SearchButton_Click()
If SearchTeamComboBox.ListIndex < 0 And SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select Team and the Process/project you want to search ."
SearchTeamComboBox.SetFocus
ElseIf SearchTeamComboBox.ListIndex < 0 Then
MsgBox "Please select Team."
SearchTeamComboBox.SetFocus
ElseIf SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select the Process/project you want to search ."
SearchSelectPPComboBox.SetFocus
Else
Dim WHAT_TO_FIND As String
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range
WHAT_TO_FIND = SearchSelectPPComboBox.Value
Set ws = Sheets(SearchTeamComboBox.Value)
Set FoundCell = ws.Range("F8:F" & ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row).Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
'If Not IsEmpty(ActiveCell.Value) Then
If Not FoundCell Is Nothing Then
MsgBox (WHAT_TO_FIND & " is found ")
Me.ExistingProcessProjectNameTextbox = FoundCell.Offset(0, 0).Value
Me.ExistingTeamComboBox = SearchTeamComboBox.Value
Me.ExistingchecklistComboBox.Value = FoundCell.Offset(0, 1).Value
Me.ExistingORRComboBox.Value = FoundCell.Offset(0, 2).Value
Me.ExistingdateTextBox.Value = FoundCell.Offset(0, 3).Value
End If
End If
End If
End Sub
Next codes! ................................................................................................................................................................................................................................................................................................................................
Private Sub SearchTeamComboBox_Change()
Application.EnableEvents = False
SearchSelectPPComboBox.Clear
Application.EnableEvents = True
Dim PP As Object
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
' check that a team has been selected
If SearchTeamComboBox.ListIndex <> -1 Then
strSelected = SearchTeamComboBox.Value
If strSelected = "ACLT" Then
LastRow = Worksheets("ACLT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("ACLT").Range("E8:E" & LastRow)
ElseIf strSelected = "AIFCIF" Then
LastRow = Worksheets("AIFCIF").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("AIFCIF").Range("E8:E" & LastRow)
ElseIf strSelected = "FDM" Then
LastRow = Worksheets("FDM").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("FDM").Range("E8:E" & LastRow)
ElseIf strSelected = "Imaging" Then
LastRow = Worksheets("Imaging").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("Imaging").Range("E8:E" & LastRow)
ElseIf strSelected = "MRT" Then
LastRow = Worksheets("MRT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("MRT").Range("E8:E" & LastRow)
ElseIf strSelected = "PAT" Then
LastRow = Worksheets("PAT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("PAT").Range("E8:E" & LastRow)
ElseIf strSelected = "SSU" Then
LastRow = Worksheets("SSU").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("SSU").Range("E8:E" & LastRow)
ElseIf strSelected = "VEL" Then
LastRow = Worksheets("VEL").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("VEL").Range("E8:E" & LastRow)
End If
For Each PP In rngList
If Len(PP.Value) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1)
Next PP
End If
End If
End Sub
I added conditions for each combo box to add items only if not empty, in the Else branch
Private Sub SearchButton_Click()
Dim cmbTeam As ComboBox, cmbPP As ComboBox, lastRow As Long
Dim searchedText As String, ws As Worksheet, found As Range
Set cmbTeam = SearchTeamComboBox
Set cmbPP = SearchSelectPPComboBox
If cmbTeam.ListIndex < 0 And cmbPP.ListIndex < 0 Then
MsgBox "Please select Team and the Process/project to search."
cmbTeam.SetFocus
ElseIf cmbTeam.ListIndex < 0 Then
MsgBox "Please select Team."
cmbTeam.SetFocus
ElseIf cmbPP.ListIndex < 0 Then
MsgBox "Please select the Process/project to search."
cmbPP.SetFocus
Else
searchedText = cmbPP.Value
Set ws = Worksheets(cmbTeam.Value)
lastRow = ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row
Set found = ws.Range("F8:F" & lastRow).Find(what:=searchedText, lookat:=xlWhole)
If Not found Is Nothing Then
With found
MsgBox (searchedText & " is found ")
If Len(.Offset(0, 0).Value) > 0 Then Me.ExistingProcessProjectNameTextbox = .Offset(0, 0).Value
Me.ExistingTeamComboBox = cmbTeam.Value
If Len(.Offset(0, 1).Value) > 0 Then Me.ExistingchecklistComboBox.Value = .Offset(0, 1).Value
If Len(.Offset(0, 2).Value) > 0 Then Me.ExistingORRComboBox.Value = .Offset(0, 2).Value
If Len(.Offset(0, 3).Value) > 0 Then Me.ExistingdateTextBox.Value = .Offset(0, 3).Value
End If
End If
End If
End Sub
I also removed one extra "End If", but I didn't test it (don't have your form)
Edit:
You can fix the For Each part by replacing this:
For Each PP In rngList
If Len(PP.Value) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1)
Next PP
with this
For Each PP In rngList
If Len(PP.Offset(, 1)) > 0 Then SearchSelectPPComboBox.AddItem PP.Offset(, 1).Value2
Next
or this
For Each PP In rngList
If Len(PP.Offset(, 1).Value2) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1).Value2
End If
Next
It's also safe to change Dim PP As Object to Dim PP As Range
I've been trying to use the following code to compare two Excel sheets however I can't get it to function quite as I need. I need to compare row by row and report specific differences even when rows of data on each sheet are not necessarily in the same order. The code reports all differences based on how the data physically exists in the tables. So it is showing differences but if the data was put into the same order in each table they wouldn't actually be differences. Due to the nature of the data I can't sort first. Hope this makes sense. Could someone please suggest what changes are required to get what I need?
Sub Compare()
' compare two different worksheets in the active workbook
CompareWorksheets Worksheets("Sheet1"), Worksheets("Sheet2")
' compare two different worksheets in two different workbooks
'CompareWorksheets ActiveWorkbook.Worksheets("Sheet1"), _
' Workbooks("impchk1.xls").Worksheets("Sheet2")
End Sub
Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With ws1.UsedRange
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With ws2.UsedRange
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = "Comparing cells " & Format(c / maxC, "0 %") & "..."
For r = 1 To maxR
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = ws1.Cells(r, c).FormulaLocal
cf2 = ws2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = "'" & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Compare " & ws1.Name & " with " & ws2.Name
End Sub
In the worksheet that will be updated, convert each row to a string and save it to a dictionary. Then, in the worksheet you're updating from, loop through all the rows, get their string rep and see if it exists in the dictionary. If it doesn't, then add them.
Here's an example code that gets a string from a rows values
Sub getRowAsString()
Dim cell As Object
Dim sheet As Worksheet
Dim str As String
Dim arr() As Variant
Dim arr2() As Variant
Dim printCol As Integer
Set sheet = ActiveSheet
printCol = sheet.UsedRange.Columns.Count + 1
For Each cell In sheet.UsedRange.Rows
arr = cell.Value2
ReDim arr2(LBound(arr, 2) To UBound(arr, 2))
For i = LBound(arr, 2) To UBound(arr, 2)
arr2(i) = arr(1, i)
Next i
str = Join(arr2, ", ")
ActiveSheet.Cells(cell.Row, printCol).value = str
Next cell
End Sub
Here's an example of using a dictionary:
Sub createDictionary()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Key = "hello"
value = "world"
dict.Add Key, value
MsgBox "key exists: " & dict.exists(Key) & vbNewLine & "value: " & dict(Key)
End Sub
If the string representation of a row is too large, you can save a hashed value of it into the dictionary to make it more manageable. Here is a post that gives VBA code for hashing a string