Look for keywords and copy to another sheet - excel

I currently have a working code that does exactly what I want it to do, loops through a particular part of an excel document looking for certain keywords, then pasting those keywords into a separate sheet in the excel spreadsheet. It is just very long and doesn't allow for any more than 10 repetitions. I was wondering if anyone had advice on making this code loop until the user selects vbNo when asked if they have any more keywords?
Option Compare Text
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+h
'
Application.ScreenUpdating = False
Dim lastLine As Long
Dim findWhat As String
Dim toCopy As Boolean
Dim cell As Range
Dim i As Long
Dim j As Long
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
s = 2
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1

What you want to do is make a loop that will continue until the user chooses to stop by hitting cancel in your input box. To start the loop, set the loop check variable, "continue" to be equal to the result of clicking 'yes' on the box that asks if the user wants to continue.
Then, after executing the code, ask the user if they wish to add another word. If not, the loop will end. If so, the loop will continue and do another word.
Here's a start....
Dim Continue As Long
Dim findWhat As String
Dim LastLine As Long
Dim toCopy As Boolean
Dim cell As Range
Dim i As Long
Dim j As Long
Dim newsheet As Worksheet
s = 2
Continue = vbYes 'initialize loop variable
Do While Continue = vbYes 'keep getting more use input until they state they do not want to continue
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Set newsheet = activeworkbook.sheets.add
newsheet.name = findWhat
Rows(i).Copy Destination:=newsheet.Rows(j)
j = j + 1
End If
Set newsheet = Nothing
toCopy = False
Next i
s = s + 1
'find out if user wishes to continue.
Continue = MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion)
Loop

Related

Unable to find circular reference in custom made formula for excel

Ok, so I'm completely unable to understand why there is a circular refence, or why the function is being called again in the following code:
Option Explicit
Enum rc
rc1 = 2
rl1 = 2
rcwork = 10 ' Has to be an even number, it's the column index where the 'Work Hours' column is
End Enum
Public Function WorkHours(currentRow As Long) As Variant
Dim i As Long, j As Long, lastCol As Long, lastColTrue As Long ' ,lastRow As Long
Dim workTime As Variant
Dim conditionMissingExit As Boolean, conditionMissingEntry As Boolean, conditionMissingEntryExit As Boolean
Application.Volatile
'Init values
conditionMissingExit = False
conditionMissingEntry = False
i = currentRow
lastCol = CurrentMonth.Cells(i, rc1).End(xlToRight).Column
lastColTrue = CurrentMonth.Cells(i, rcwork).End(xlToLeft).Column
If lastCol = rcwork And lastColTrue >= rc1 Then
lastCol = lastColTrue
ElseIf lastCol = rcwork And lastColTrue < rl1 And CurrentMonth.Cells(i, rc1 + 1) = "" Then
lastCol = rc1 + 1
ElseIf lastCol = rcwork And CurrentMonth.Cells(i, rcwork - 2) <> "" Then
lastCol = rcwork - 1
End If
If lastColTrue = lastCol Then
lastCol = lastColTrue
Else
If lastColTrue = 1 And CurrentMonth.Cells(i, rc1).Value = "" Then
workTime = ""
GoTo SafeExit
End If
For j = lastCol To lastColTrue
If Not IsNumeric(Cells(i, j).Value) Then
workTime = "Invalid Entry at cell (" & i & "," & ColIndexToLetter(j) & ")"
GoTo SafeExit
End If
If CurrentMonth.Cells(i, j).Value = "" And (j Mod 2 = 0) Then
conditionMissingEntry = True
If conditionMissingEntry Then
If CurrentMonth.Cells(i, j + 1).Value = "" And Not ((j + 1) Mod 2 = 0) Then
workTime = "Missing entry and exit"
GoTo SafeExit
Else
workTime = "Missing entry"
GoTo SafeExit
End If
End If
ElseIf CurrentMonth.Cells(i, j).Value = "" And Not (j Mod 2 = 0) Then
conditionMissingExit = True
If conditionMissingExit Then
If CurrentMonth.Cells(i, j + 1).Value = "" And ((j + 1) Mod 2 = 0) Then
workTime = "Missing entry and exit"
GoTo SafeExit
Else
workTime = "Missing exit"
GoTo SafeExit
End If
End If
End If
Next j
End If
For j = rc1 To lastCol Step 2
conditionMissingExit = (Not CurrentMonth.Cells(i, j).Value = "" And CurrentMonth.Cells(i, j + 1).Value = "")
conditionMissingEntry = (CurrentMonth.Cells(i, j).Value = "" And Not CurrentMonth.Cells(i, j + 1).Value = "")
conditionMissingEntryExit = (CurrentMonth.Cells(i, j).Value = "" And CurrentMonth.Cells(i, j + 1).Value = "")
If Not IsNumeric(Cells(i, j).Value) Then
workTime = "Invalid Entry at cell (" & i & "," & ColIndexToLetter(j) & ")"
Exit For
ElseIf Not IsNumeric(Cells(i, j + 1).Value) Then
workTime = "Invalid Entry at cell (" & i & "," & ColIndexToLetter(j + 1) & ")"
Exit For
End If
If Not conditionMissingExit And Not conditionMissingEntry And Not conditionMissingEntryExit Then
workTime = workTime + DateDiff("n", CurrentMonth.Cells(i, j).Value, CurrentMonth.Cells(i, j + 1).Value) / 60
ElseIf conditionMissingEntry Then
workTime = "Missing entry"
Exit For
ElseIf conditionMissingExit Then
If CurrentMonth.Cells(i, j + 2).Value = "" And j + 2 <= lastCol Then
workTime = "Missing entry and exit"
Exit For
Else
workTime = "Missing exit"
Exit For
End If
ElseIf conditionMissingEntryExit Then
workTime = "Missing entry and exit"
Exit For
End If
Next j
SafeExit:
WorkHours = workTime
End Function
Public Function ColIndexToLetter(ColIndex As Long) As String
'This function returns the a String corresponding to the letter associated to the column of index ColIndex
Dim CellAddress As String
CellAddress = Cells(1, ColIndex).Address
ColIndexToLetter = Split(CellAddress, "$")(1)
End Function
The formula on cell J2 is:
=WorkHours(2)
which calls the public function WorkHours
I do not understand what is going on because the macro-formula works , not perfectly, because the function keeps calling itself for some reason when I fill the other cells, it will give the circular error only when I fill the H column cell without filling also the I column cell (See image below)
Anyway the main problem here is that the function keeps being called for the same row several times, and I do not know why.
UPDATE:
Thank you Tim Williams and Toddleson for taking your time to look at my problem.
Tim, following your advice I modified the WorkHours function like this:
Public Function WorkHours(currentRow As Long) As Variant
Dim i As Long, j As Long, lastCol As Long, lastColTrue As Long ' ,lastRow As Long
Dim workTime As Variant
Dim conditionMissingExit As Boolean, conditionMissingEntry As Boolean, conditionMissingEntryExit As Boolean
Application.Volatile
Debug.Print Application.Caller.Address
End Function
The formula is present only on cell J2 and it's being called like this:
=WorkHours(2)
So that no reference at all to any cell is inputed into the formula.
After that as you can see from the code of the function, the is no cell reading whatsoever.
Now the output in the immediate window is:
$J$2
Meaning the function is being called only once...
I need to analyse in my previous code what is reading/accessing the calling cell...
Anyway, thank you very much Tim Williams, I didn't know that reading the calling cell would cause this behaviour as it's the first time I use application.volatile
You need to make sure your function never tries to read the value of the cell it's in. If you need to know which cell that is, you can use either Application.Caller or Application.ThisCell
Here's an example to illustrate:
Function Tester()
Dim c As Range, this As Range, rw As Range
Set this = Application.ThisCell 'the cell with the formula (in this case in Col H)
'Get a range on the same row as the formula, first 20 cells (so that includes the cell with the formula)
'Note you don't really need the `currentRow` parameter since you can use `ThisCell.Row`
Set rw = this.Parent.Cells(this.Row, "A").Resize(1, 20)
'This loop will cause a circular reference error as it tries to read from `this`
' which triggers the UDF again, etc etc
For Each c In rw.Cells
Debug.Print c.Value
Next c
'this loop avoids the error by not reading from `this`
For Each c In rw.Cells
If c.Column <> this.Column Then 'skip the cell with the formula
Debug.Print c.Value
End If
Next c
End Function

Else without If error after a bunch of ElseIf statements

I'm getting an Else without If error after all my ElseIf statements. I'm trying to end my initial If AColValue = "LV Fuses" with an Else statement but it's giving me this error at the Else.
Do While j <> 1
If AColValue = "LV Fuses" Then 'Fuse info starts with a row labelled "LV Fuses" in Col A
j = 1
Exit Do
ElseIf AColValue = "HV/MV with Trip-Unit" Then '
j = 1
Exit Do
ElseIf AColValue = "HV/MV without Trip-Unit" Then '
j = 1
Exit Do
ElseIf AColValue = "Relays" Then '
j = 1
Exit Do
ElseIf AColValue = "MCP" Then '
j = 1
Exit Do
ElseIf AColValue = "MOL" Then '
j = 1
Exit Do
ElseIf AColValue = "HV Fuses" Then '
j = 1
Exit Do
ElseIf AColValue = "Switches" Then '
j = 1
Exit Do
ElseIf AColValue = "" Then '
NumOfBkrs = NumOfBkrs + 1
RowNumberPlus1 = RowNumber + 1
AColValue = Range("A" & RowNumberPlus1)
If AColValue = "" Then
RowNumberPlus1 = RowNumberPlus1 + 1
AColValue = Range("A" & RowNumberPlus1)
If AColValue = "" Then
j = 1
End If
End If
End If
Else
RowNumber = RowNumber + 1
End If
AColValue = Range("A" & RowNumber)
Loop
Your second to last End If closes the main If.
Then, the following Else has no If.

Adding drop-down multiple selection list in a column for all rows in Excel

I have a worksheet where I must allow the selection of more than one value in a cell. I'd like a drop-down list with checkboxes.
This is the VBA that makes the drop-down list.
How do I add it to each cell in the column "Roadmap"? How do I retrieve the selection values?
Private Sub Worksheet_Change(ByVal Target As Range)
'
' Calculate Percentages for Each Quarter
'
Dim this_sheet, select_period
this_sheet = ActiveSheet.Name
If Not Intersect(Target, Range("J2")) Is Nothing Then
ActiveSheet.Cells(2, 12).Value = 0
select_period = ActiveSheet.Cells(2, 10).Value
'MsgBox ("Creeps! " & this_sheet & " " & select_period)
ActiveSheet.Cells(2, 11).Value = " " & select_period & ": "
ActiveSheet.Cells(2, 11).HorizontalAlignment = xlCenter
Select Case select_period
Case "Overall"
ActiveSheet.Cells(2, 12).Formula = "=COUNTIFS($I10:$I3000,""Done"",$L10:$L3000,""2019"")/COUNTIF($L10:$L3000,""2019"")"
Case "2019"
ActiveSheet.Cells(2, 12).Formula = "=COUNTIF($L10:$L3000,""2019"")"
ActiveSheet.Cells(2, 13).Formula = "=COUNTIFS($I10:$I3000,""Done"",$L10:$L3000,""2019"")/COUNTIF($L10:$L3000,""2019"")"
Case "2020"
ActiveSheet.Cells(2, 12).Formula = "=COUNTIF($L10:$L3000,""2020 - Q1"")"
ActiveSheet.Cells(2, 13).Formula = "=COUNTIFS($I10:$I3000,""Done"",$L10:$L3000,""2020 - Q1"")/COUNTIF($L10:$L3000,""2020 - Q1"")"
Case "2020 - Q1"
ActiveSheet.Cells(2, 12).Formula = "=COUNTIF($L10:$L3000,""2020 - Q1"")"
ActiveSheet.Cells(2, 13).Formula = "=COUNTIFS($I10:$I3000,""Done"",$L10:$L3000,""2020 - Q1"")/COUNTIF($L10:$L3000,""2020 - Q1"")"
End Select
End If
'
' Create Multiple Selection Listbox
'
Dim LBColors As MSForms.ListBox
Dim LBobj As OLEObject
Dim i As Long
Set LBobj = Me.OLEObjects("LB_Colors")
Set LBColors = LBobj.Object
If Not Intersect(Target, Range("H2")) Is Nothing Then
Set fillRng = Target
With LBobj
.Left = fillRng.Left
.Top = fillRng.Top
.Width = fillRng.Width
.Visible = True
End With
Else
LBobj.Visible = False
If Not fillRng Is Nothing Then
fillRng.ClearContents
With LBColors
If .ListCount <> 0 Then
For i = 0 To .ListCount - 1
If fillRng.Value = "" Then
If .Selected(i) Then fillRng.Value = .List(i)
Else
If .Selected(i) Then fillRng.Value = _
fillRng.Value & "," & .List(i)
End If
Next
End If
'For i = 0 To .ListCount - 1
' .Selected(i) = False
'Next
End With
Set fillRng = Nothing
End If
End If
End Sub
Excel File here

How to recalculate prices in ListBox1 after deleting

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

How to highlight duplicate reference designaters in Multilevel Bill of material

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

Resources