0% found this document useful (0 votes)
20 views3 pages

Filtered BOQ

The provided VBA macro duplicates the active worksheet and removes rows associated with unchecked checkboxes. It prompts the user for a prefix and suffix to format item codes, then renumbers the remaining items sequentially. Finally, it restores application settings and notifies the user of the successful operation.

Uploaded by

adilrchanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Filtered BOQ

The provided VBA macro duplicates the active worksheet and removes rows associated with unchecked checkboxes. It prompts the user for a prefix and suffix to format item codes, then renumbers the remaining items sequentially. Finally, it restores application settings and notifies the user of the successful operation.

Uploaded by

adilrchanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub FilteredBOQ()

Dim ws As Worksheet
Dim newWs As Worksheet
Dim cb As Object
Dim i As Long
Dim rowToDelete As Long
Dim deleteRows As Range
Dim seqNum As Integer
Dim subSeqNum As Integer
Dim prefix As String
Dim suffix As String
Dim currentMainCode As String
Dim subChar As String
Dim cell As Range
Dim lastRow As Long
Dim startRow As Long
Dim foundItem As Boolean

' Disable screen updating and calculations for speed


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

' Set the active worksheet


Set ws = ActiveSheet

' Get user-defined prefix and suffix for code formatting


prefix = InputBox("Enter the prefix for the code format (e.g., '02-'):", "Code
Format Selection", "02-")
If prefix = "" Then Exit Sub

suffix = InputBox("Enter the suffix for the code format (e.g., 'T'):", "Code
Format Selection", "T")
If suffix = "" Then Exit Sub

' Duplicate the active worksheet


Application.DisplayAlerts = False
ws.Copy After:=ws
Application.DisplayAlerts = True
Set newWs = ActiveSheet

' Convert entire worksheet to values to prevent formula errors


newWs.Cells.Copy
newWs.Cells.PasteSpecial xlPasteValues
Application.CutCopyMode = False ' Clear clipboard

' Find the last row


lastRow = newWs.Cells(newWs.Rows.Count, 1).End(xlUp).Row

' Search for "Item" in column A


foundItem = False
For Each cell In newWs.Range("A1:A" & lastRow)
If InStr(1, cell.Value, "Item", vbTextCompare) > 0 Then
foundItem = True
startRow = cell.Row + 2 ' Skip one row after "Item"
Exit For
End If
Next cell
' If "Item" is not found, exit
If Not foundItem Then
MsgBox "'Item' not found in column A!", vbExclamation, "Error"
Exit Sub
End If

' Initialize numbering


seqNum = 1
subSeqNum = 0

' Loop through checkboxes in reverse order to prevent skipping


For i = newWs.CheckBoxes.Count To 1 Step -1
Set cb = newWs.CheckBoxes(i)

' If checkbox is unchecked, mark row for deletion


If cb.Value = xlOff Then
rowToDelete = cb.TopLeftCell.Row

' Add row to deletion range


If deleteRows Is Nothing Then
Set deleteRows = newWs.Rows(rowToDelete)
Else
Set deleteRows = Union(deleteRows, newWs.Rows(rowToDelete))
End If

' Add the next row as well


If deleteRows Is Nothing Then
Set deleteRows = newWs.Rows(rowToDelete + 1)
Else
Set deleteRows = Union(deleteRows, newWs.Rows(rowToDelete + 1))
End If

' Add the third row as well


If deleteRows Is Nothing Then
Set deleteRows = newWs.Rows(rowToDelete + 2)
Else
Set deleteRows = Union(deleteRows, newWs.Rows(rowToDelete + 2))
End If

' Add the fourth row as well


If deleteRows Is Nothing Then
Set deleteRows = newWs.Rows(rowToDelete + 3)
Else
Set deleteRows = Union(deleteRows, newWs.Rows(rowToDelete + 3))
End If

' Delete the checkbox


cb.Delete
End If
Next i

' Delete all marked rows at once


If Not deleteRows Is Nothing Then deleteRows.Delete Shift:=xlUp

' Renumber codes sequentially in column A starting from startRow


For Each cell In newWs.Range("A" & startRow & ":A" & newWs.Cells(Rows.Count,
1).End(xlUp).Row)
If Trim(cell.Value) <> "" Then
' Check if it's a main section or a subsection
If IsNumeric(Left(cell.Value, 1)) Then
' Main section
currentMainCode = prefix & seqNum & suffix
seqNum = seqNum + 1
subSeqNum = 0 ' Reset subsection count
Else
' Subsection
subSeqNum = subSeqNum + 1
subChar = "(" + Chr(96 + subSeqNum) + ")" ' Convert 1 ? a, 2 ? b,
etc.
currentMainCode = prefix & (seqNum - 1) & suffix & subChar
End If

' Assign new code


cell.Value = currentMainCode
End If
Next cell

' Restore settings


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

' Notify user


MsgBox "Worksheet duplicated successfully with unchecked rows removed.",
vbInformation, "Success"
End Sub

You might also like