I have created a possapp for my bar. Everything works fine but i have to recalculate the listbox when delete 1 or more items.
This is my code for the delete button
Private Sub CommandButton84_Click()
Dim ItemTarget&, s, i%
s = 0
ItemTarget = ListBox1.ListCount
If ItemTarget > 0 Then
Me.ListBox1.RemoveItem ItemTarget - 1
For i = 0 To Me.ListBox1.ListCount - 1
s = s + Val(Me.ListBox1.List(i, 1))
Next
Me.TextBox1 = s
Else
MsgBox "De lijst is reeds leeg", vbInformation, "Café De Zoete Inval"
End If
Me.TextBox4 = Me.ListBox1.ListCount
End Sub
Private Sub CommandButton100_Click()
Dim LItem As Long
Dim IRange As Integer
Dim sht As Worksheet
Dim LastRow As Long
Dim rows As Integer
rows = 0
Set sht = ActiveSheet
For LItem = 0 To ListBox1.ListCount - 1
ListBox1.ColumnCount = 2
With Worksheets("Sheet6")
.Cells(LItem + 7, 1) = ListBox1.List(LItem, 0)
.Cells(LItem + 7, 2) = ListBox1.List(LItem, 1)
.Cells(LItem + 8, 1).EntireRow.Insert
rows = rows + 1
End With
With Sheets("Histo")
LastRow = .Cells.Find("*", searchorder:=xlByRows,
searchdirection:=xlPrevious).Row
If Time < "07:00:00" Then
.Cells(LastRow + 1, 1) = Format(Date - 1, "dd-mm-yyyy")
Else
.Cells(LastRow + 1, 1) = Date
End If
.Cells(LastRow + 1, 2) = ListBox1.List(LItem, 0)
.Cells(LastRow + 1, 3) = ListBox1.List(LItem, 1)
End With
Next LItem
With ThisWorkbook.Sheets("Sheet6")
ListBox1.Clear
TextBox2.Value = ""
TextBox1.Value = 0
Range("Sheet6!B5").ClearContents
For i = 1 To rows
.Cells(7, 1).EntireRow.Delete
Next
End With
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click()
With ThisWorkbook.Sheets("Sheet1")
Me.ListBox1.AddItem .Range("B2").Value
Me.ListBox1.Column(1, ListBox1.ListCount - 1) = Format(Val(.Range("C2").Value),
"€#,##0.00")
Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C2").Value
Me.TextBox1.Value = Format(Me.TextBox1.Value, "#,##0.00")
End With
Me.TextBox4 = Me.ListBox1.ListCount
End Sub
Related
I'm new to VBA and what I have so far is mashup from various tutorials and websearches. But so far it works how I want. Now I'd like to add search option and after hours of websearch I cannot find right solution.
Basically I'm trying to create userform which shows data from sheet in listbox, when I double-click item on listbox it shows values of defined cell in textboxes and checkboxes. Got that. I'd like to add search option to list results in same cleared listbox without duplicates (if searched value appears multiple times in same row list it only once in listbox) and when I double-click on an item in listbox it will show details of that record in userform textboxes with checkbox values. All works fine with CheckBox1 changing colours aswell.
What I tried so far.
My listbox populates with all data and on double click shows everything properly.
Sub All_Data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("TABLE")
Dim last_Row As Long
last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
With Me.ListBox1
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = "0,70,60,60,0,0,0,0,0,0,120,0"
.List = Range(Cells(1, 1), Cells(last_Row, .ColumnCount)).Value
.RemoveItem 0
End With
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
Me.TextBox5.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
Me.TextBox6.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 5)
Me.TextBox7.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 6)
Me.ComboBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 7)
Me.Checkbox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 8)
Me.Checkbox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 9)
Me.CheckBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 10)
Me.CheckBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 11)
End Sub
Private Sub CheckBox1_Change()
If CheckBox1.Value Then
CheckBox1.ForeColor = &H8000&
Else
CheckBox1.ForeColor = &HC0&
End If
End Sub
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Checkbox values are changing properly from red to green and vice versa.
cbgreen cbred
Then I tried to add search option and it kind of works, but checkbox values being greyed out despite showing right true value (but still works with all data listed).
cbgrey
Private Sub searchButton_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("TABLE")
Dim i As Long
Dim x As Long
Dim p As Integer, k As Integer
Me.searchTextBox = LCase(Me.searchTextBox)
If Me.searchTextBox = "" Then
Call All_Data
Exit Sub
End If
With Me.ListBox1
.Clear
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = "0,70,60,60,0,,0,0,0,0,120,0"
For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 2))
p = Me.searchTextBox.TextLength
For k = 2 To .ColumnCount - 4
If LCase(Mid(sh.Cells(i, k), x, p)) = Me.searchTextBox And Me.searchTextBox <> "" Then
.AddItem
.List(.ListCount - 1, 0) = sh.Cells(i, 1).Value
.List(.ListCount - 1, 1) = sh.Cells(i, 2).Value
.List(.ListCount - 1, 2) = sh.Cells(i, 3).Value
.List(.ListCount - 1, 3) = sh.Cells(i, 4).Value
.List(.ListCount - 1, 4) = sh.Cells(i, 5).Value
.List(.ListCount - 1, 5) = sh.Cells(i, 6).Value
.List(.ListCount - 1, 6) = sh.Cells(i, 7).Value
.List(.ListCount - 1, 7) = sh.Cells(i, 8).Value
.List(.ListCount - 1, 8) = sh.Cells(i, 9).Value
.List(.ListCount - 1, 9) = sh.Cells(i, 10).Value
.List(.ListCount - 1, 10) = sh.Cells(i, 11).Value
.List(.ListCount - 1, 11) = sh.Cells(i, 12).Value
End If
Next k
Next x
Next i
RemoveDuplicates Listbox1
End With
End Sub
Sub RemoveDuplicates(aListBox As MSForms.ListBox)
Dim i As Long, j As Long
With aListBox
For i = .ListCount - 1 To 1 Step -1
For j = 0 To i - 1
If (.List(i, 0) = .List(j, 0)) Or (.List(i, 0) = vbNullString) Then
.RemoveItem i
Exit For
End If
Next j
Next i
End With
End Sub
Is there another way to search, list results in listbox without duplicates and on double-click to populate values to textboxes and ComboBox1 not greyed out (changing colour)?
Thank you for any suggestions and help.
Avoid the duplicates by exiting the column loop at the first match.
Option Explicit
Const COLWIDTHS = "0,70,60,60,0,,0,0,0,0,120,0"
Private Sub searchButton_Click()
Dim sh As Worksheet, data
Dim s As String, lastrow As Long
Dim i As Long, j As Long, k As Long, n As Long
s = Trim(LCase(Me.searchTextBox)) ' search term
If Len(s) = 0 Then
Call All_Data
Exit Sub
End If
With Me.ListBox1
.Clear
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = COLWIDTHS
End With
Set sh = ThisWorkbook.Sheets("TABLE")
With sh
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
' use array to speed up search
data = .Range("A1:L" & lastrow).Value2 ' 12 columns
End With
'search
n = 0 ' list index
s = "*" & s & "*" ' like
For i = 2 To lastrow
For j = 2 To 8 ' search col B to H
If LCase(data(i, j)) Like s Then
With Me.ListBox1
.AddItem
For k = 1 To 12 ' Col A to L
.List(n, k - 1) = data(i, k)
Next
n = n + 1
End With
Exit For
End If
Next
Next
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim n As Long, i As Long, c As Control
With Me.ListBox1
i = .ListIndex
If i < 0 Then Exit Sub
For n = 1 To 7 ' A to G
Set c = Me.Controls("TextBox" & n)
c.Value = .List(i, n - 1)
Next
Me.ComboBox1.Value = .List(i, 7) ' col H
For n = 9 To 12 ' col I - L
Set c = Me.Controls("CheckBox" & n - 8)
If .List(i, n - 1) = 1 Then
c.Value = True
c.ForeColor = &H8000&
Else
c.Value = False
c.ForeColor = &HC0&
End If
Next
End With
End Sub
Sub All_Data()
Dim sh As Worksheet, last_Row As Long
Set sh = ThisWorkbook.Sheets("TABLE")
With sh
last_Row = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With Me.ListBox1
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = COLWIDTHS
.List = sh.Range(sh.Cells(2, 1), sh.Cells(last_Row, .ColumnCount)).Value
End With
End Sub
' repeat for checkboxes 2,3 4
Private Sub CheckBox1_Change()
If CheckBox1.Value Then
CheckBox1.ForeColor = &H8000&
Else
CheckBox1.ForeColor = &HC0&
End If
End Sub
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Private Sub UserForm_Activate()
All_Data
End Sub
Sub GetMaxl()
Dim nMonths As Integer, nRegions As Integer
Dim iRow As Integer, iCol As Integer
Dim regionMax As Double, monthMax As Double
Dim maxScore As Integer
With wsSales.Range("A3")
nMonths = Range(.Offset(0, 1), .Offset(0, 1).End(xlToRight)) _
.Columns.Count
nRegions = Range(.Offset(1, 0), .Offset(1, 0).End(xlDown)) _
.Rows.Count
.Offset(0, nMonths + 1) = "MaxScore"
.Offset(nRegions + 1, 0) = "MaxScore"
For iRow = 1 To nRegions
regionMax = 0
For iCol = 1 To nMonths
regionMax = WorksheetFunction.Max(Range(.Offset(iRow, iCol), .End(xlToRight)))
Debug.Print , regionMax
Next iCol
.Offset(iRow, nMonths + 1) = regionMax
Next iRow
For iCol = 1 To nMonths
monthMax = 0
For iRow = 1 To nRegions
monthMax = WorksheetFunction.Max(Range(.Offset(iRow, iCol), .End(xlDown)))
Next iRow
.Offset(nRegions + 1, iCol) = monthMax
Next iCol
End With
End Sub
I am trying to get the max score against columns and rows in a loop but I am not getting correct results.
It can be done easier and faster
Option Explicit
Sub GetMaxl()
Dim Rng As Range, rng_total_right As Range, rng_total_down As Range
Set Rng = wsSales.Range("A3").CurrentRegion
'get the ranges for max column and row at right and down
Set rng_total_right = Intersect(Rng.Offset(, 1), wsSales.Columns(Rng.Column + Rng.Columns.Count))
Set rng_total_down = Intersect(Rng.Offset(1, 0), wsSales.Rows(Rng.Row + Rng.Rows.Count))
With rng_total_right ' max column
.NumberFormat = "0"
.FormulaR1C1 = "=MAX(RC[-" & Rng.Columns.Count - 1 & "]:RC[-1])" ' insert formulas and calculate them
.Value = .Value 'replace formulas by values
.Cells(1) = "MaxScore" 'make header
End With
With rng_total_down 'max row
.NumberFormat = "0"
.FormulaR1C1 = "=MAX(R[-" & Rng.Rows.Count - 1 & "]C:R[-1]C)"
.Value = .Value
.Cells(1) = "MaxScore"
End With
End Sub
I'm trying to run a compare though multiple sheets.
I get
Runtime error 9 subscript out of range
Sub Comp_TEST()
Dim ar As Variant
Dim var()
Dim i As Long
Dim n As Long
Dim Last_Row As Long
Dim WS As Worksheet
For Each WS In ActiveWorkbook.Worksheets
If WS.Name <> "GALVANISED" And WS.Name <> "ALUMINUM" And WS.Name <> "LOTUS" And WS.Name <> "TEMPLATE" And WS.Name <> "SCHEDULE CALCULATIONS" And WS.Name <> "TRUSS" And WS.Name <> "DASHBOARD CALCULATIONS" And WS.Name <> "GALVANISING CALCULATIONS" Then
WS.Range("D3:D1000").Copy
WS.Range("O3").PasteSpecial xlPasteValues
WS.Range("K3:K1000").Copy
WS.Range("N3").PasteSpecial xlPasteValues
Application.CutCopyMode = False
ar = WS.Range("N3").CurrentRegion
ReDim var(1 To UBound(ar, 1), 1 To 1)
With CreateObject("scripting.dictionary")
.comparemode = 1
For i = 1 To UBound(ar, 1)
.Item(ar(i, 2)) = Empty
Next
For i = 1 To UBound(ar, 1)
If Not .exists(ar(i, 1)) Then
n = n + 1
var(n, 1) = ar(i, 1) 'error happens here
End If
Next
End With
WS.[P3].Resize(n).Value = var
Erase var
ReDim var(1 To UBound(ar, 1), 1 To 1)
Last_Row = WS.Range("D2").End(xlDown).Offset(1).Row
WS.Range("P3:P1000").Copy
WS.Range("D" & Last_Row).PasteSpecial xlPasteValues
WS.Range("N3:P1000").ClearContents
End If
Next WS
End Sub
The following works but then I need to make a Sub for at the moment 26 sheets which could be more later. I don't want to make another Sub each time that happens.
Or I may also need to delete a sheet then I would have delete that Sub.
Sub Comp_ALL_VANS()
Dim ar As Variant
Dim var()
Dim i As Long
Dim n As Long
Dim Last_Row As Long
Worksheets("ALL VANS").Range("D3:D1000").Copy
Worksheets("ALL VANS").Range("O3").PasteSpecial xlPasteValues
Worksheets("ALL VANS").Range("K3:K1000").Copy
Worksheets("ALL VANS").Range("N3").PasteSpecial xlPasteValues
Application.CutCopyMode = False
ar = Worksheets("ALL VANS").Range("N3").CurrentRegion
ReDim var(1 To UBound(ar, 1), 1 To 1)
With CreateObject("scripting.dictionary")
.comparemode = 1
For i = 1 To UBound(ar, 1)
.Item(ar(i, 2)) = Empty
Next
For i = 1 To UBound(ar, 1)
If Not .exists(ar(i, 1)) Then
n = n + 1
var(n, 1) = ar(i, 1)
End If
Next
End With
Worksheets("ALL VANS").[P3].Resize(n).Value = var
Last_Row = Worksheets("ALL VANS").Range("D2").End(xlDown).Offset(1).Row
Worksheets("ALL VANS").Range("P3:P1000").Copy
Worksheets("ALL VANS").Range("D" & Last_Row).PasteSpecial xlPasteValues
Worksheets("ALL VANS").Range("N3:P1000").ClearContents
End Sub
Option Explicit
Sub Comp_TEST()
Dim ws As Worksheet, n As Long
Dim arSkip
arSkip = Array("GALVANISED", "ALUMINUM", "LOTUS", "TEMPLATE", "SCHEDULE CALCULATIONS", _
"TRUSS", "DASHBOARD CALCULATIONS", "GALVANISING CALCULATIONS")
For Each ws In ActiveWorkbook.Worksheets
If IsError(Application.Match(ws.Name, arSkip, 0)) Then
Call process(ws)
n = n + 1
Else
Debug.Print "Skipped " & ws.Name
End If
Next
MsgBox n & " sheets processed", vbInformation
End Sub
Sub process(ws As Worksheet)
Dim dict As Object, k As String, arK, arD, arNew
Dim n As Long, i As Long, LastRowD As Long, LastRowK as Long
Set dict = CreateObject("scripting.dictionary")
dict.comparemode = 1
With ws
LastRowK = .Cells(.Rows.Count, "K").End(xlUp).Row
If LastRowK < 4 Then LastRowK = 4 ' ensure 2 cells for array
arK = .Range("K3:K" & LastRowK)
LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row
If LastRowD <= 3 Then
arD = .Range("D3:D4") ' ensure 2 cells for array
If LastRowD < 2 Then LastRowD = 2
Else
arD = .Range("D3:D" & LastRowD)
End If
End With
' array for new
ReDim arNew(1 To UBound(arK), 1 To 1)
' fill dictionary from col D
For i = 1 To UBound(arD)
k = arD(i, 1)
If dict.exists(k) Then
MsgBox "Duplicate key '" & k & "' at D" & i + 2, vbCritical, "Error " & ws.Name
Exit Sub
ElseIf Len(k) > 0 Then
dict.Add k, i
End If
Next
' compare col K with col D
n = 0
For i = 1 To UBound(arK)
k = arK(i, 1)
If Not dict.exists(k) Then
n = n + 1
arNew(n, 1) = k
End If
Next
' result
If n > 0 Then
ws.Range("D" & LastRowD + 1).Resize(n) = arNew
End If
End Sub
I want to copy all rows and some columns from sheet (one) to sheet (two). My code only copies the first 64 records. I have tried it different ways wirh vba. I have two command buttons each try it a different way. Both only give me the first 64 rows.
Private Sub CommandButton1_Click()
Dim lastrow As Long, erow As Long
Dim I As Integer
I = 2
Worksheets("two").Activate
Range("A1:F1").Font.Bold = True
Range("A1:F1").Font.Name = "Tahoma"
Range("A1:F1").Font.Size = 14
ActiveSheet.Range("A1").Value = "Last"
ActiveSheet.Range("B1").Value = "First"
Columns("A:f").HorizontalAlignment = xlCenter
ActiveSheet.Range("C1").Value = "Wife"
ActiveSheet.Range("D1").Value = "Phone"
ActiveSheet.Range("E1").Value = "B Day"
Worksheets("Two").Range("A1:F1").Font.Bold = True
Worksheets("Two").Range("A1:F1").Font.Name = "Tahoma"
Worksheets("Two").Range("A1:F1").Font.Size = 14
Worksheets("Two").Range("A1").HorizontalAlignment = xlCenter
Worksheets("One").Activate
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To lastrow
Worksheets("One").Cells(I, 1).Copy
erow = Worksheets("Two").Cells(Rows.Count + 1).End(xlUp).Row
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 1)
Worksheets("One").Cells(I, 2).Copy
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 2)
Worksheets("One").Cells(I, 3).Copy
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 3)
Worksheets("One").Cells(I, 4).Copy
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 4)
Worksheets("One").Cells(I, 10).Copy
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 5)
Worksheets("One").Cells(I, 18).Copy
Worksheets("one").Paste Destination:=Worksheets("Two").Cells(erow + 1, 6)
Next I
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Worksheets("two").Activate
Worksheets("Two").Range("A2").Activate
End Sub
Private Sub CommandButton2_Click()
Dim a As String
Dim b As String
a = Worksheets("one").Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To a
If Worksheets("One").Cells(I, 20).Value = 1 Then
Worksheets("One").Rows(I).Copy
Worksheets("One").Activate
b = Worksheets("Two").Cells(Rows, Count, 1).End(xlUp).Row
Worksheets("Two").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("One").Activate
End If
Next I
Application.CutCopyMode = False
ThisWorkbook.Worksheets("one").Cells(1, 1).Select
Worksheets("two").Activate
End Sub
Begin by replacing:
erow = Worksheets("Two").Cells(Rows.Count + 1).End(xlUp).Row
with:
erow = Worksheets("Two").Cells(Rows.Count ,1).End(xlUp).Row+1
There my be other errors
You can do this without Copy/Paste and avoiding Activate and Select
Private Sub CommandButton1_Click()
Dim lastrow As Long
Dim erow As Long
Dim I As Long
I = 2
With Worksheets("Two")
.Range("A1").Value = "Last"
.Range("B1").Value = "First"
.Columns("A:f").HorizontalAlignment = xlCenter
.Range("C1").Value = "Wife"
.Range("D1").Value = "Phone"
.Range("E1").Value = "B Day"
.Range("A1:F1").Font.Bold = True
.Range("A1:F1").Font.Name = "Tahoma"
.Range("A1:F1").Font.Size = 14
.Range("A1").HorizontalAlignment = xlCenter
End With
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To lastrow
erow = Worksheets("Two").Cells(Rows.Count).End(xlUp).Row + 1
Worksheets("Two").Cells(erow, 1) = Worksheets("One").Cells(I, 1)
Worksheets("Two").Cells(erow, 2) = Worksheets("One").Cells(I, 2)
Worksheets("Two").Cells(erow, 3) = Worksheets("One").Cells(I, 3)
Worksheets("Two").Cells(erow, 4) = Worksheets("One").Cells(I, 4)
Worksheets("Two").Cells(erow, 5) = Worksheets("One").Cells(I, 10)
Worksheets("Two").Cells(erow, 6) = Worksheets("One").Cells(I, 18)
Next I
Sheet2.Columns().AutoFit
End Sub
Private Sub CommandButton2_Click()
Dim a As Long
Dim b As Long
a = Worksheets("One").Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To a
If Worksheets("One").Cells(I, 20).Value = 1 Then
b = Worksheets("Two").Cells(Rows, Count, 1).End(xlUp).Row + 1
Worksheets("Two").Rows(b) = Worksheets("One").Rows(I)
End If
Next I
End Sub
I am working on macro that highlights duplicate Reference designators under immediate top level of multilevel Bill of material.
My code is as below:
'To identify duplicates RDs
Sheet1.Columns(14).Copy (Sheet4.Cells(1, 1))
Sheet4.Select
Sheet4.Rows("1:1").Select
Selection.Copy
Selection.Insert shift:=xlDown
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10000").AutoFilter Field:=1, Criteria1:="="
Cells.Select
Selection.Delete shift:=xlUp
Sheet4.Columns(1).Interior.ColorIndex = xlNone
Dim FromLine As Integer
Dim ToLine As Integer
Dim Count As Integer
Dim Leng As Integer
Dim RefTemp, RefTemp1, RefTemp2 As String
Dim Cha As String
Dim ReferenceNo As String
Dim PartNo As String
Dim Description As String
Dim Flag As Boolean
FromLine = 1
Cha = " "
While Cells(FromLine, 1) <> ""
Flag = True
ReferenceNo = LTrim(Cells(FromLine, 1))
RefTemp = RTrim(ReferenceNo)
Leng = Len(RefTemp)
Cells(FromLine, 1) = RefTemp
Count = 1
While Count <= Leng And Flag
RefTemp1 = Left(ReferenceNo, 1)
If RefTemp1 <> " " And RefTemp1 <> "," Then
ReferenceNo = Right(ReferenceNo, Leng - Count)
Else
Cells(FromLine, 1) = Left(RefTemp, Count - 1)
Flag = False
RefTemp2 = Right(ReferenceNo, Leng - Count)
FromLine = FromLine + 1
Rows(FromLine).Select
Selection.Insert shift:=xlDown
Cells(FromLine, 1) = RefTemp2
FromLine = FromLine - 1
End If
Count = Count + 1
Wend
FromLine = FromLine + 1
Wend
Dim cel1 As Variant
Dim myrng1 As Range
Dim clr1 As Long
Set myrng1 = Sheet4.Range("A1:A" & Sheet4.Range("A65536").End(xlUp).Row)
myrng1.Interior.ColorIndex = xlNone
j = 1
For Each cel1 In myrng1
If Application.WorksheetFunction.CountIf(myrng1, cel1) > 1 Then
If WorksheetFunction.CountIf(Sheet4.Range("A1:A" & cel1.Row), cel1) = 1 Then
Sheet4.Cells(j, 2).Value = cel1
j = j + 1
Else
cel1.Interior.ColorIndex = myrng1.Cells(WorksheetFunction.Match(cel1.Value, myrng1, False), 1).Interior.ColorIndex
End If
End If
Next
Dim lastrow4 As Long
lastrow4 = Sheet4.Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow4
For j = 1 To lastrow
k1 = InStr(Sheet1.Cells(j, 14).Value, Sheet4.Cells(i, 2).Value)
len1 = Len(Sheet4.Cells(i, 2).Value)
If k1 > 0 Then
Sheet1.Cells(j, 14).Interior.ColorIndex = 28
Sheet1.Cells(j, 14).Characters(k1, len1).Font.ColorIndex = 3
End If
Next j
Next i
Sheet4.Rows("1:" & Rows.Count).Delete shift:=xlUp
Sheet1.Select
Problems:
Requirement is to highlight duplicate 'Ref Des' under immediate top level.
For e.g. in above screenshot 'P2'& 'P3' is immediate childs of 'M1' (P2 &P3 are level 2 and M1 is level 1).
So, in column N, letter J is highlighting. It is correct.
But P4 is child of M2. It must not highlight.
Please help.
I have got solution for above problem as below:
'To identify duplicates RDs
Sheet1.Columns(14).Copy (Sheet4.Cells(1, 1))
Sheet4.Select
Sheet4.Rows("1:1").Select
Selection.Copy
Selection.Insert shift:=xlDown
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10000").AutoFilter Field:=1, Criteria1:="="
Cells.Select
Selection.Delete shift:=xlUp
Sheet4.Columns(1).Interior.ColorIndex = xlNone
Dim FromLine As Integer
Dim ToLine As Integer
Dim Count As Integer
Dim Leng As Integer
Dim RefTemp, RefTemp1, RefTemp2 As String
Dim Cha As String
Dim ReferenceNo As String
Dim PartNo As String
Dim Description As String
Dim Flag As Boolean
FromLine = 1
Cha = " "
While Cells(FromLine, 1) <> ""
Flag = True
ReferenceNo = LTrim(Cells(FromLine, 1))
RefTemp = RTrim(ReferenceNo)
Leng = Len(RefTemp)
Cells(FromLine, 1) = RefTemp
Count = 1
While Count <= Leng And Flag
RefTemp1 = Left(ReferenceNo, 1)
If RefTemp1 <> " " And RefTemp1 <> "," Then
ReferenceNo = Right(ReferenceNo, Leng - Count)
Else
Cells(FromLine, 1) = Left(RefTemp, Count - 1)
Flag = False
RefTemp2 = Right(ReferenceNo, Leng - Count)
'PartNo = Cells(FromLine, 2)
'Description = Cells(FromLine, 3)
FromLine = FromLine + 1
Rows(FromLine).Select
Selection.Insert shift:=xlDown
Cells(FromLine, 1) = RefTemp2
'Cells(FromLine, 2) = PartNo
'Cells(FromLine, 3) = Description
FromLine = FromLine - 1
End If
Count = Count + 1
Wend
FromLine = FromLine + 1
Wend
Dim cel1 As Variant
Dim myrng1 As Range
Dim clr1 As Long
Set myrng1 = Sheet4.Range("A1:A" & Sheet4.Range("A65536").End(xlUp).Row)
myrng1.Interior.ColorIndex = xlNone
j = 1
For Each cel1 In myrng1
If Application.WorksheetFunction.CountIf(myrng1, cel1) > 1 Then
If WorksheetFunction.CountIf(Sheet4.Range("A1:A" & cel1.Row), cel1) = 1 Then
'cel1.Interior.ColorIndex = 7
'cel1.Font.ColorIndex = 1
Sheet4.Cells(j, 2).Value = cel1
j = j + 1
Else
cel1.Interior.ColorIndex = myrng1.Cells(WorksheetFunction.Match(cel1.Value, myrng1, False), 1).Interior.ColorIndex
End If
End If
Next
Dim lastrow4 As Long
lastrow4 = Sheet4.Range("B" & Rows.Count).End(xlUp).Row
Dim myarr() As String
For i = 1 To lastrow4
For j = 1 To lastrow
myarr() = Split(Sheet1.Cells(j, 14).Value, ",")
k1 = 0
For y = LBound(myarr) To UBound(myarr)
If myarr(y) = Sheet4.Cells(i, 2).Value Then
k1 = 1
End If
Next y
'L1 = InStr(Sheet1.Cells(j, 14).Value, Sheet4.Cells(i, 2).Value)
len1 = Len(Sheet4.Cells(i, 2).Value)
If Not IsEmpty(Sheet4.Cells(i, 2)) Then
If k1 > 0 Then
Start = 1
Do
L1 = InStr(Start, Sheet1.Cells(j, 14).Value, Sheet4.Cells(i, 2).Value)
If L1 > 0 Then
Start = L1 + 1
Sheet1.Cells(j, 14).Interior.ColorIndex = 28
Sheet1.Cells(j, 14).Characters(L1, len1).Font.ColorIndex = 3
End If
Loop While L1 > 0
End If
End If
Next j
Next i
Sheet4.Rows("1:" & Rows.Count).Delete shift:=xlUp
Sheet1.Select