79734380

Date: 2025-08-13 14:11:12
Score: 1.5
Natty:
Report link
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    ' Column A is 1, B is 2, C is 3, etc.
    Const PROJECT_NUMBER_COLUMN As Long = 1
    
    ' Verify the change was made in the correct column
    If Not Target.Column = PROJECT_NUMBER_COLUMN Then
        Exit Sub
    End If
    
    ' Get the project number that was entered
    Dim projectNumber As String
    projectNumber = Target.Value2
    
    ' Loop through all subfolders, looking for the project number
    Dim fso As New Scripting.FileSystemObject
    Dim projectFolder As Folder, projectFolderNumber As String
    For Each projectFolder In fso.GetFolder("your\root\file\path").SubFolders
        
        projectFolderNumber = Split(projectFolder.Name, " ")(0)
        
        ' Do we have a match?
        If projectFolderNumber = projectNumber Then
            Dim projectName As String
            projectName = Replace(projectFolder.Name, projectFolderNumber, "")
            
            ' Turn off events before we write to the screen
            ' Otherwise, this change will trigger Workbook_SheetChange to fire again
            Application.EnableEvents = False
            ThisWorkbook.Sheets("Sheet1").Cells(Target.Row, Target.Column + 1).Value = projectName
            Application.EnableEvents = True
            
            Exit For
        End If
        
    Next projectFolder
    
End Sub

A few notes on the code above:

Reasons:
  • RegEx Blacklisted phrase (2.5): Do we have a
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: TehDrunkSailor