The code below works well, but when it sorts the column, it also sorts row 1 and row 2.…
- User
The code below works well, but when it sorts the column, it also sorts row 1 and row 2. I only want the sort from row 3 to row 503. Sub SearchAndLinkFiles() Dim folderPath As String Dim fileName As String Dim ws As Worksheet Dim keyword As String Dim row As Long Dim col As Long ' Set the folder path where the files are located folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Total Water Compliance" ' Set the worksheet where the keywords are located Set ws = ThisWorkbook.Sheets("Files") ws.Range("A3:A503").ClearContents ws.Range("C3:C503").ClearContents ws.Range("E3:E503").ClearContents ws.Range("G3:G503").ClearContents ws.Range("I3:I503").ClearContents ws.Range("K3:K503").ClearContents ws.Range("M3:M503").ClearContents ws.Range("O3:O503").ClearContents ws.Range("Q3:Q503").ClearContents ws.Range("S3:S503").ClearContents ' Loop through each keyword in row 2 For col = 1 To ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column keyword = ws.Cells(2, col).Value ' Start from row 3 to avoid overwriting the keyword row row = 3 ' Loop through each file in the folder fileName = Dir(folderPath & ".") Do While fileName <> "" ' Check if the file name contains the keyword If InStr(1, fileName, keyword, vbTextCompare) > 0 Then ' Create a hyperlink to the file ws.Hyperlinks.Add Anchor:=ws.Cells(row, col), _ Address:=folderPath & fileName, _ TextToDisplay:=fileName row = row + 1 End If fileName = Dir Loop
' Sort links based on the last values (without file extension) in the columns Dim rng As Range Set rng = ws.Range(ws.Cells(3, col), ws.Cells(row - 1, col)) rng.Value = Evaluate("IFERROR(LEFT(" & rng.Address & ", FIND(""."", " & rng.Address & ") - 1), " & rng.Address & ")")
ws.Columns(col).Sort key1:=ws.Range(ws.Cells(3, col), ws.Cells(row - 1, col)), _ order1:=xlAscending, Header:=xlNo rng.Value = ws.Range(ws.Cells(3, col), ws.Cells(row - 1, col)).Value Next colEnd Sub
- Assistant
To sort only rows 3 to 503, you can modify the code as follows:
Sub SearchAndLinkFiles() Dim folderPath As String Dim fileName As String Dim ws As Worksheet Dim keyword As String Dim row As Long Dim col As Long ' Set the folder path where the files are located folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Total Water Compliance" ' Set the worksheet where the keywords are located Set ws = ThisWorkbook.Sheets("Files") ws.Range("A3:A503").ClearContents ws.Range("C3:C503").ClearContents ws.Range("E3:E503").ClearContents ws.Range("G3:G503").ClearContents ws.Range("I3:I503").ClearContents ws.Range("K3:K503").ClearContents ws.Range("M3:M503").ClearContents ws.Range("O3:O503").ClearContents ws.Range("Q3:Q503").ClearContents ws.Range("S3:S503").ClearContents ' Loop through each keyword in row 2 For col = 1 To ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column keyword = ws.Cells(2, col).Value ' Start from row 3 to avoid overwriting the keyword row row = 3 ' Loop through each file in the folder fileName = Dir(folderPath & ".") Do While fileName <> "" ' Check if the file name contains the keyword If InStr(1, fileName, keyword, vbTextCompare) > 0 Then ' Create a hyperlink to the file ws.Hyperlinks.Add Anchor:=ws.Cells(row, col), _ Address:=folderPath & fileName, _ TextToDisplay:=fileName row = row + 1 End If fileName = Dir Loop
- User
When I include the amendment into the code, I am getting an error and it is still trying to include row 1 and row 2 into the sort; ' Sort links based on the last values (without file extension) in the columns Dim rng As Range Set rng = ws.Range(ws.Cells(3, col), ws.Cells(row - 1, col)) rng.Value = Evaluate("IFERROR(LEFT(" & rng.Address & ", FIND(""."", " & rng.Address & ") - 1), " & rng.Address & ")")
ws.Columns(col).Sort key1:=ws.Range("A3", ws.Cells(row - 1, col)), _ order1:=xlAscending, Header:=xlNo
rng.Value = ws.Range(ws.Cells(3, col), ws.Cells(row - 1, col)).Value
- Assistant
Apologies for the confusion. I made an error in my previous response. You need to modify the sort range to exclude rows 1 and 2. Here is the corrected code:
' Sort links based on the last values (without file extension) in the columns Dim rng As Range Set rng = ws.Range(ws.Cells(3, col), ws.Cells(row, col)) rng.Value = Evaluate("IFERROR(LEFT(" & rng.Address & ", FIND(""."", " & rng.Address & ") - 1), " & rng.Address & ")") ws.Columns(col).Sort key1:=rng, _ order1:=xlAscending, Header:=xlNo rng.Value = rng.ValueIn this updated code, the range for sorting is set to
ws.Range(ws.Cells(3, col), ws.Cells(row, col)), which includes rows 3 to the last row containing data in the column. Additionally, the range objectrngis used in theSortmethod instead ofws.Range("A3", ws.Cells(row - 1, col)). The value assignmentrng.Value = rng.Valueis kept to remove the formulas and keep only the sorted values in the column. - User
It is still including row 1 and row 2 in the sort
- Assistant
Apologies for the confusion. Let's modify the code to exclude rows 1 and 2 from the sort range. Here's the corrected code:
' Sort links based on the last values (without file extension) in the columns Dim rng As Range Set rng = ws.Range(ws.Cells(3, col), ws.Cells(row, col)) rng.Value = Evaluate("IFERROR(LEFT(" & rng.Address & ", FIND(""."", " & rng.Address & ") - 1), " & rng.Address & ")") Dim sortRange As Range Set sortRange = ws.Range(ws.Cells(3, col), ws.Cells(row, col)) With ws.Sort .SortFields.Clear .SortFields.Add Key:=sortRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange sortRange .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With rng.Value = rng.ValueIn this updated code, we use a separate range object called
sortRangethat includes only rows 3 to the last row containing data in the column. We then use theSortmethod with thesortRangeto perform the sorting, excluding rows 1 and 2. Finally, we assign the values back to the original rangerngto remove formulas and keep only the sorted values.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.