Dynamic search code that ignores non-numerical data.

freddie mitchell

New Member
Joined
Apr 14, 2011
Messages
43
Hello everyone,

A question on a code I'm using to auto sort a table by date, included below. Is it possible to add an IF clause to ignore / skip over non-numerical values from the code? For example, if someone were to change a pre-existing date in the worksheet to "September" the code would not try to auto sort it!


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    On Error Resume Next
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Range("A4").Sort Key1:=Range("A5"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End If

End Sub


Thank you for the help, you're all super :)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Whilst it's possible to check if text has been entered & stop the code, then next time somebody changes a value in col A the data will be sorted, including the text.
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0
Fluff, on a related issue that you might be able to help with.

The previously mentioned auto-sort search code is causing issues for another bit of code, linked below. Could you tell me why this would be?

The issue is that the Insert_Row function is not working. So when the okbutton_click fires it just substitutes existing content in the worksheet, rather than inserting a row.

I know it's the auto-sort causing the issue because when I delete that bit of code then the Insert_Row part works perfectly.



VBA Code:
Private Sub OKButton_Click()

'starting insert row function
Call Insert_Row
End Sub

Public Sub Insert_Row()

    Dim r As Variant
    Dim userformDate As Date
    
    userformDate = CDate(Replace(StartDate.Value, ".", "-"))
   
    With Worksheets("CEE rolling calendar of events")
    
        r = Application.Match(CLng(userformDate), .Columns(1), 1)
        If IsError(r) Then
            r = 2
        ElseIf .Cells(r, 1).Value < userformDate Then
            r = r + 1
        End If
    
        .Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrBelow
        
        'remove the default text from start date and end date
If StartDate.Value = "DD/MM/YY" Then
StartDate.Value = ""
End If


If EndDate.Value = "DD/MM/YY" Then
EndDate.Value = ""
End If
 
Upvote 0
You could add
VBA Code:
If Target.CountLarge > 1 Then Exit Sub
to the very start of your change event. That way it will exit the sub if more than one cell is changed.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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