VBA sorting and boarders - adding to existing code

dmj120

Active Member
Joined
Jan 5, 2010
Messages
286
Office Version
  1. 365
  2. 2019
  3. 2010
I've been able to add more columns to the "search results" and a sorting option, the 'commented' line (the first with statement to sort C7 ascending) worked, but .SortFields.Add Key:=Range("E7"), Order:=xlDescending does not sort the dates correctly. Any ideas??

Also, I'm trying to figure out how to add boarders to the resultant list....but it ain't workin! Any suggestions??


VBA Code:
Sub Search_criteria_pricing()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long
  Dim cad1 As String, cad2 As String, cad3 As String
  '
  Set sh1 = Sheets("pricing")
  Set sh2 = Sheets("search")
  '
  a = sh1.Range("A1:Q" & sh1.Range("A:Q").Find("*", , xlValues, , 1, 2).Row).Value2
  sh2.Range("A8:H" & Rows.Count).ClearContents
  ReDim b(1 To UBound(a, 1), 1 To 16)
  
  If sh2.[B2] <> "" Then cad1 = "*" & LCase(sh2.[B2].Value) & "*"
  If sh2.[B3] <> "" Then cad2 = "*" & LCase(sh2.[B3].Value) & "*"
  If sh2.[B4] <> "" Then cad3 = "*" & LCase(sh2.[B4].Value) & "*"
  
  'j = sh2 columns, a = sh1 corresponding columns
  ' ///// need to add boarders to resultant list /////
  For i = 1 To UBound(a, 1)
    If LCase(a(i, 1)) Like cad1 And LCase(a(i, 2)) Like cad2 And LCase(a(i, 3)) Like cad3 Then
      j = j + 1
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
      b(j, 3) = a(i, 3)
      b(j, 4) = a(i, 9)
      b(j, 5) = a(i, 12)
      b(j, 6) = a(i, 14)
      b(j, 7) = a(i, 15)
      b(j, 8) = a(i, 16)
    End If
  Next i
  
  If j > 0 Then sh2.Range("A8").Resize(j, 16).Value = b
  
'moves to top of displayed list
  Range("A8").Select

'sort results by column C then E  --- **** dates (col E) not sorting correctly
    With ActiveSheet.Sort
        '.SortFields.Add Key:=Range("C7"), Order:=xlAscending
        .SortFields.Add Key:=Range("E7"), Order:=xlDescending
        .SetRange Range("A7:H5555")
        .Header = xlYes
        .Apply
    End With
  
  
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,212,933
Messages
6,110,759
Members
448,295
Latest member
Uzair Tahir Khan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top