In my worksheet 'Work Permit' I have a drop down List in H3 of company names. When I…
- User
In my worksheet 'Work Permit' I have a drop down List in H3 of company names. When I select a 'Company Name', formulas in column K reveal 'Personnel Names' associated to the Company I selected in the drop down list. The data is sourced from sheet 'Permit DBS' using formulas where Column A3:A501 contains Personnel Names Column J3:J501 contains Company Names
In the same sheet'Work Permit' I want to be able to do the following using VBA:
Manually enter Part of a name in cell H10 and in H12 going downwards and not further than H22, get a list of all possible close matches from sheet 'Permit DBS' column A3:A501 with the Company Name from sheet 'Permit DBS' column J3:J501 for each Personel Name listed in I12 going downwards
- Assistant
- User
error Variable not defined on 'cell' in line 'For Each cell In wsPermitDBS.Range("A3:A501")'
- Assistant
Apologies for the error. Please modify and use the updated code below:
Sub GetMatchingPersonnelNames() Dim wsPermitDBS As Worksheet Dim wsWorkPermit As Worksheet Dim companyDropdown As Range Dim searchInput As Range Dim searchResults As Range Dim cell As Range Dim i As Long ' Set references to the necessary worksheets Set wsPermitDBS = ThisWorkbook.Sheets("Permit DBS") Set wsWorkPermit = ThisWorkbook.Sheets("Work Permit") ' Set the range for the company dropdown list Set companyDropdown = wsWorkPermit.Range("H3") ' Set the range for the search input cell Set searchInput = wsWorkPermit.Range("H10") ' Clear the previous search results wsWorkPermit.Range("H12:H22").ClearContents ' Check if a company is selected in the dropdown list If companyDropdown.Value <> "" Then ' Find the matching company names in column J of Permit DBS Set searchResults = wsPermitDBS.Range("J3:J501").Find(companyDropdown.Value, LookIn:=xlValues, lookat:=xlWhole) ' If a match is found, loop through the personnel names in column A and populate the search results If Not searchResults Is Nothing Then i = 0 For Each cell In wsPermitDBS.Range("A3:A501") ' Check if the personnel name contains the search input value If InStr(1, cell.Value, searchInput.Value, vbTextCompare) > 0 Then ' Add the matching personnel name and corresponding company name to the search results wsWorkPermit.Range("H12").Offset(i, 0).Value = cell.Value wsWorkPermit.Range("I12").Offset(i, 0).Value = searchResults.Value i = i + 1 ' Exit the loop if the maximum number of search results has been reached If i > 10 Then Exit For End If Next cell End If End If End Sub - User
The code does not have any errors, but it is not finding any names
- Assistant
- User
Code is not getting any data
- Assistant
- User
It is not getting any data. I tried it also with an exact string text match and still it could not find it
- Assistant
- User
still not finding any data
- Assistant