Whe I run this code, I do not get any errors but it just runs continously like its in a…
- User
Whe I run this code, I do not get any errors but it just runs continously like its in a loop. Can you help me fix it. Sub FindReferencingSheets() Dim linkedDocsSheet As Worksheet Dim fileRow As Long Dim fileName As String Dim referencingSheets As String Dim sheet As Worksheet Dim vbComp As Object Dim codeLines As Long Dim lineNum As Long Dim foundMatch As Boolean
'Set the "Linked Docs" sheet Set linkedDocsSheet = ThisWorkbook.Worksheets("Linked Docs") 'Loop through each file name in column A For fileRow = 2 To linkedDocsSheet.Cells(Rows.Count, "A").End(xlUp).Row 'Get the file name fileName = linkedDocsSheet.Cells(fileRow, "A").Value 'Clear previous referencing sheets linkedDocsSheet.Cells(fileRow, "C").Resize(1, 100).ClearContents 'Loop through each sheet in the workbook For Each sheet In ThisWorkbook.Sheets 'Check if the sheet has a VBA component If sheet.Type = xlWorksheet And sheet.CodeName <> "Linked_Docs" Then 'Loop through each VBA component in the sheet For Each vbComp In sheet.Parent.VBProject.VBComponents 'Check if VBA component is a module If vbComp.Type = 1 Then codeLines = vbComp.codeModule.CountOfLines 'Skip empty modules or modules with no code lines If codeLines > 0 Then Set codeModule = vbComp.codeModule lineNum = 1 foundMatch = False 'Search for the file name in the module’s code Do While lineNum <= codeLines And Not foundMatch lineNum = codeModule.Find(fileName, lineNum, 1, -1, -1, False, False, False) 'If the file name is found in the code If lineNum > 0 Then 'Add the sheet name to the referencing sheets referencingSheets = referencingSheets & sheet.Name & "," foundMatch = True End If 'Increment the line number lineNum = lineNum + 1 Loop End If End If Next vbComp End If Next sheet 'Remove trailing comma and add referencing sheets to column C onwards If Len(referencingSheets) > 0 Then referencingSheets = Left(referencingSheets, Len(referencingSheets) - 1) linkedDocsSheet.Cells(fileRow, "C").Value = referencingSheets End If 'Reset referencing sheets variable for the next file referencingSheets = "" Next fileRowEnd Sub
- Assistant
- User
I am getting an error 'User-defined type not defined' on 'CodeModule As CodeModule'
- Assistant
I apologize for that oversight. The
CodeModuleobject requires a reference to the Microsoft Visual Basic for Applications Extensibility library.To add the reference:
- Open the Visual Basic Editor by pressing Alt + F11.
- Go to Tools > References in the menu bar.
- In the References dialog box, scroll down and find "Microsoft Visual Basic for Applications Extensibility."
- Check the box next to it to select the reference.
- Click OK to apply the changes and close the dialog box.
After adding the reference, the error should be resolved, and you should be able to compile and run the code without any issues.
- User
the code does not have any errors but it has been running for the last five minutes
- Assistant
If the code has been running for an extended period of time without producing any output or completing, it is possible that the code is encountering slow performance due to a large number of worksheets or VBA components in your workbook. Additionally, searching through the code of each VBA component can take some time, especially if there are many code lines to search through.
Here are a few suggestions to optimize the code and potentially improve the performance:
-
Reduce the number of sheets to search through: If you have sheets in your workbook that do not contain any VBA code or are not relevant to the search, consider excluding them from the loop. You can add a condition to skip sheets based on their names or other criteria.
-
- User
errors encountered
- Assistant
- User
Can you write me a VBA code that will list all linked file names in my workbook and the nmae of the sheet or module in which they are found. I would like the list in my sheet 'Linked Docs' with the file names in column L and the location in column M
- Assistant
- User
The VBA works but it gives me more information that I actually need. The information I want in column L are files that in G: drive as shown in the example below G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Auxillary.xlsm
- Assistant