This is my situation:
I have this Worksheet
and on a VBA userform this ListBox where a User defines Sort Criteria
In addition I have a report based on all worksheet data. The report in itself works fine. Now I want to give the user the option to select a sort criteria by selecting a “Sortkey1” from within the listbox.
Note: I made sure that Column Headers and Listbox values both are formatted as Text.
The code I’m using is as follows:
Private Sub ListBoxDonReportsSortField_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Integer, Sortkey1 As String
For i = 0 To ListBoxDonReportsSortField.ListCount - 1
If ListBoxDonReportsSortField.Selected(i) Then
Sortkey1 = Sortkey1 & ListBoxDonReportsSortField.List(i) & vbCrLf
End If
Next i
End Sub
Then, within my Report code: (Active sheet is my worksheet)
Dim rgFind As Range
With Range("A1:F1")
Set rgFind = .Find(What:=Sortkey1, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).row
With ActiveSheet.Sort
.SortFields.Add Key:=Range(Cells(1, rgFind.Column)), Order:=xlAscending
.SetRange Range("A1:F" & lr)
.Header = xlYes
.Apply
End With
Help please, this is what I get: Doesn’t work, getting Error 91, Object Variable or With Block not set
I’m using the same code within other applications and rgFind.column typically gives me the correct column. Messing with this now for 2 days, giving up and ask for your help. What am I missing here?
Thanks for your help, Uwe
I have this Worksheet
ID | Fname | Lname | City | ZIP | Last Date |
1 | Joe | Green | Denver | 12345 | 1/1/2021 |
2 | Mary | Gold | New York | 64578 | 2/1/2021 |
3 | Frank | Black | Chicago | 45678 | 12/1/2020 |
and on a VBA userform this ListBox where a User defines Sort Criteria
ListBox |
Lname |
City |
ZIP |
Date |
In addition I have a report based on all worksheet data. The report in itself works fine. Now I want to give the user the option to select a sort criteria by selecting a “Sortkey1” from within the listbox.
Note: I made sure that Column Headers and Listbox values both are formatted as Text.
The code I’m using is as follows:
Private Sub ListBoxDonReportsSortField_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Integer, Sortkey1 As String
For i = 0 To ListBoxDonReportsSortField.ListCount - 1
If ListBoxDonReportsSortField.Selected(i) Then
Sortkey1 = Sortkey1 & ListBoxDonReportsSortField.List(i) & vbCrLf
End If
Next i
End Sub
Then, within my Report code: (Active sheet is my worksheet)
Dim rgFind As Range
With Range("A1:F1")
Set rgFind = .Find(What:=Sortkey1, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).row
With ActiveSheet.Sort
.SortFields.Add Key:=Range(Cells(1, rgFind.Column)), Order:=xlAscending
.SetRange Range("A1:F" & lr)
.Header = xlYes
.Apply
End With
Help please, this is what I get: Doesn’t work, getting Error 91, Object Variable or With Block not set
I’m using the same code within other applications and rgFind.column typically gives me the correct column. Messing with this now for 2 days, giving up and ask for your help. What am I missing here?
Thanks for your help, Uwe