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:
PROJECT_NUMBER_COLUMN
is a value that you'll need to change based on what column you're entering the numbers into. I'm assuming that there's only one column in your sheet that you're entering these numbers into.Scripting
library is used rather than Late Binding.