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

ItemReferenceList Without Sub-Items

The document is a VBA macro that generates an 'Item Reference List' from a worksheet named 'TEL'. It classifies items based on predefined subsection headers, extracts relevant data, and formats it into a new worksheet. The macro also includes error handling for existing sheets and adjusts column widths for better readability.

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)
8 views3 pages

ItemReferenceList Without Sub-Items

The document is a VBA macro that generates an 'Item Reference List' from a worksheet named 'TEL'. It classifies items based on predefined subsection headers, extracts relevant data, and formats it into a new worksheet. The macro also includes error handling for existing sheets and adjusts column widths for better readability.

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 GenerateItemReferenceList()

Dim wsTEL As Worksheet, wsNew As Worksheet


Dim lastRow As Long, i As Long, outputRow As Long, serialNo As Long
Dim code As String, desc As String, currentSubsection As String
Dim subsectionData As Object, subsectionHeaders As Variant
Dim regex As Object

' Define subsection headers


subsectionHeaders = Array("STRUCTURED CABLING NETWORK", "CCTV SYSTEM", "PUBLIC
ADDRESS SYSTEM", _
"ADDRESSABLE FIRE ALARM SYSTEM", "CATV SYSTEM",
"ACCESS CONTROL SYSTEM", "BIRD GUARD SYSTEM")

' Set TEL worksheet


Set wsTEL = ThisWorkbook.Sheets("TEL")

' Define new worksheet name


Dim newSheetName As String
newSheetName = "Item Reference List"

' Check if the worksheet exists, delete if found


On Error Resume Next
Application.DisplayAlerts = False
Sheets(newSheetName).Delete
Application.DisplayAlerts = True
On Error GoTo 0

' Create new worksheet for item reference list


Set wsNew = ThisWorkbook.Sheets.Add
wsNew.Name = newSheetName

' Get last used row in TEL worksheet (Column A)


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

' Initialize dictionary for subsections


Set subsectionData = CreateObject("Scripting.Dictionary")
For Each Header In subsectionHeaders
subsectionData.Add Header, CreateObject("Scripting.Dictionary")
Next Header

' Initialize regular expression for decimal number detection


Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^\d+\.\d+$" ' Matches decimal numbers only (e.g., 1.51, 2.30)
regex.Global = False

' Loop through TEL worksheet and classify items


For i = 2 To lastRow
code = Trim(wsTEL.Cells(i, 1).Value) ' Read code from Column A
desc = Trim(wsTEL.Cells(i, 2).Value) ' Read description from Column B

' Check if Column B contains a main section indicator (all caps)


If code = "" And desc = UCase(desc) Then
' If Column A is empty and Column B is all caps, it's a subsection
If subsectionData.exists(desc) Then
currentSubsection = desc
End If
ElseIf regex.Test(code) Then
' Only process items that have a valid decimal code assigned
If currentSubsection <> "" Then
' Store in respective subsection
If Not subsectionData(currentSubsection).exists(desc) Then
subsectionData(currentSubsection).Add desc, code
Else
' Append additional codes for the same description
subsectionData(currentSubsection)(desc) =
subsectionData(currentSubsection)(desc) & ", " & code
End If
End If
End If
Next i

' Format the title row


wsNew.Range("A1:C1").Merge
wsNew.Cells(1, 1).Value = "DEPOT"
wsNew.Cells(1, 1).Font.Bold = True
wsNew.Cells(1, 1).Font.Size = 14
wsNew.Cells(1, 1).HorizontalAlignment = xlCenter
outputRow = 2

' Write column headers


wsNew.Cells(outputRow, 1).Value = "S. No."
wsNew.Cells(outputRow, 2).Value = "Item Description"
wsNew.Cells(outputRow, 3).Value = "Item No."
wsNew.Range(wsNew.Cells(outputRow, 1), wsNew.Cells(outputRow, 3)).Font.Bold =
True
wsNew.Range(wsNew.Cells(outputRow, 1), wsNew.Cells(outputRow,
3)).Borders.LineStyle = xlContinuous
outputRow = outputRow + 1

' Write sorted data to the new worksheet


For Each Header In subsectionHeaders
If subsectionData(Header).Count > 0 Then
wsNew.Cells(outputRow, 2).Value = Header ' Move subsection headers to
column B
wsNew.Cells(outputRow, 2).Font.Bold = True
outputRow = outputRow + 1

serialNo = 1
Dim key As Variant
For Each key In subsectionData(Header).keys
wsNew.Cells(outputRow, 1).Value = serialNo
wsNew.Cells(outputRow, 2).Value = key
wsNew.Cells(outputRow, 3).Value = subsectionData(Header)(key)

' Apply wrap text for readability


wsNew.Cells(outputRow, 2).WrapText = True
wsNew.Cells(outputRow, 3).WrapText = True

outputRow = outputRow + 1
serialNo = serialNo + 1

' Insert empty row after each data row


outputRow = outputRow + 1
Next key

' Insert empty row after each subsection


outputRow = outputRow + 1
End If
Next Header

' Adjust column widths


wsNew.Cells.Columns("A").ColumnWidth = 6 ' S. No.
wsNew.Cells.Columns("B").ColumnWidth = 50 ' Item Description
wsNew.Cells.Columns("C").ColumnWidth = 25 ' Item No.
wsNew.Cells.WrapText = True

MsgBox "Item Reference List has been generated successfully!"

End Sub

You might also like