The following VBA code below searches all the sheet VBA in my workbook 'Service…
- User
The following VBA code below searches all the sheet VBA in my workbook 'Service Providers' for file paths that start "G:\Shared drives". The code is effective and accurate.
Can you please write me a similar code that searches the workbook Cells and not the VBA for file paths outside the workbook.
Sub SearchVBALinkedDocs() 'SEARCHES VBA OF ALL SHEETS FOR LINK PATH TO G Dim wb As Workbook Dim linkedDocsWs As Worksheet Dim lastRow As Long Dim ws As Worksheet Dim moduleCode As String Dim addressPath As String Dim lineIndex As Long Dim startPos As Long Dim endPos As Long Set wb = ThisWorkbook Set linkedDocsWs = wb.Sheets("Test Sheet") For Each ws In wb.Sheets On Error Resume Next lineIndex = 1 ' Check if the sheet has a code module If ws.Type = xlWorksheet Then ' Get the module code of the sheet moduleCode = wb.VBProject.VBComponents(ws.CodeName).codeModule.Lines(1, wb.VBProject.VBComponents(ws.CodeName).codeModule.CountOfLines) startPos = InStr(1, moduleCode, "G:\Shared drives") Do Until startPos = 0 endPos = InStr(startPos, moduleCode, """") If endPos > startPos Then addressPath = Mid(moduleCode, startPos, endPos - startPos) lastRow = linkedDocsWs.Cells(linkedDocsWs.Rows.Count, "D").End(xlUp).Row + 1 linkedDocsWs.Cells(lastRow, "D").Value = addressPath linkedDocsWs.Cells(lastRow, "E").Value = "'" & ws.Name End If startPos = InStr(startPos + 1, moduleCode, "G:\Shared drives") Loop End If On Error GoTo 0 Next ws Call Module32.SearchMODLinkedDocs End Sub
- Assistant
Sub SearchCellValueLinkedDocs() 'SEARCHES CELLS IN ALL WORKSHEETS FOR LINK PATH TO G Dim wb As Workbook Dim linkedDocsWs As Worksheet Dim lastRow As Long Dim ws As Worksheet Dim cell As Range Dim addressPath As String Set wb = ThisWorkbook Set linkedDocsWs = wb.Sheets("Test Sheet") For Each ws In wb.Sheets For Each cell In ws.UsedRange If InStr(1, cell.Value, "G:\Shared drives") > 0 Then lastRow = linkedDocsWs.Cells(linkedDocsWs.Rows.Count, "D").End(xlUp).Row + 1 linkedDocsWs.Cells(lastRow, "D").Value = cell.Value linkedDocsWs.Cells(lastRow, "E").Value = "'" & ws.Name End If Next cell Next ws Call Module32.SearchMODLinkedDocs End Sub
- User
Can you please omit Sheets("Test Sheet") from the search
- Assistant
Certainly! Here's the modified code without the reference to "Test Sheet":
Sub SearchCellValueLinkedDocs() 'SEARCHES CELLS IN ALL WORKSHEETS FOR LINK PATH TO G Dim wb As Workbook Dim linkedDocsWs As Worksheet Dim lastRow As Long Dim ws As Worksheet Dim cell As Range Dim addressPath As String Set wb = ThisWorkbook ' Create a new worksheet to store the results Set linkedDocsWs = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)) linkedDocsWs.Name = "Linked Documents" lastRow = 1 ' Start at the first row in the new worksheet For Each ws In wb.Sheets For Each cell In ws.UsedRange If InStr(1, cell.Value, "G:\Shared drives") > 0 Then linkedDocsWs.Cells(lastRow, "D").Value = cell.Value linkedDocsWs.Cells(lastRow, "E").Value = "'" & ws.Name lastRow = lastRow + 1 ' Move to the next row in the new worksheet End If Next cell Next ws Call Module32.SearchMODLinkedDocs End Sub
- User
What I meant is that while the code is searching, it should not search sheet 'Test Sheet'
- Assistant
Apologies for the misunderstanding. Here's the modified code that excludes the "Test Sheet" from the search:
Sub SearchCellValueLinkedDocs() 'SEARCHES CELLS IN ALL WORKSHEETS FOR LINK PATH TO G Dim wb As Workbook Dim linkedDocsWs As Worksheet Dim lastRow As Long Dim ws As Worksheet Dim cell As Range Dim addressPath As String Set wb = ThisWorkbook ' Create a new worksheet to store the results Set linkedDocsWs = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)) linkedDocsWs.Name = "Linked Documents" lastRow = 1 ' Start at the first row in the new worksheet For Each ws In wb.Sheets ' Exclude the "Test Sheet" from the search If ws.Name <> "Test Sheet" Then For Each cell In ws.UsedRange If InStr(1, cell.Value, "G:\Shared drives") > 0 Then linkedDocsWs.Cells(lastRow, "D").Value = cell.Value linkedDocsWs.Cells(lastRow, "E").Value = "'" & ws.Name lastRow = lastRow + 1 ' Move to the next row in the new worksheet End If Next cell End If Next ws Call Module32.SearchMODLinkedDocs End Sub
- User
I am getting a Type Mismatch when I run this code: Sub SearchCellValueLinkedDocs() 'SEARCHES CELLS IN ALL WORKSHEETS FOR LINK PATH TO G Dim wb As Workbook Dim linkedDocsWs As Worksheet Dim lastRow As Long Dim ws As Worksheet Dim cell As Range Dim addressPath As String Set wb = ThisWorkbook Set linkedDocsWs = wb.Sheets("Test Sheet") For Each ws In wb.Sheets If ws.Name <> "Test Sheet" Then For Each cell In ws.UsedRange If InStr(1, cell.Value, "G:\Shared drives") > 0 Then lastRow = linkedDocsWs.Cells(linkedDocsWs.Rows.Count, "D").End(xlUp).Row + 1 linkedDocsWs.Cells(lastRow, "D").Value = cell.Value linkedDocsWs.Cells(lastRow, "E").Value = "'" & ws.Name lastRow = lastRow + 1 ' Move to the next row in the new worksheet End If Next cell End If Next ws 'Call Module32.SearchMODLinkedDocs End Sub
- Assistant
The "Type Mismatch" error typically occurs when there is an inconsistency between the data types being used. In your case, the error is indicating that there is an issue with the
lastRowvariable.To resolve this error, we can update the code by changing the line
lastRow = linkedDocsWs.Cells(linkedDocsWs.Rows.Count, "D").End(xlUp).Row + 1tolastRow = linkedDocsWs.Cells(linkedDocsWs.Rows.Count, "D").End(xlUp).Row. This will get the last row that contains data in column "D" of the "Test Sheet".Here's the updated code:
vba