Consulting

Results 1 to 4 of 4

Thread: VBA second half repeats twice

  1. #1

    VBA second half repeats twice

    Hi good people!..

    I have a code which I put together from recording macros, when I run the code the second half repeats. I have noticed that the sorting of the values also only sorts in the second round. The code is:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("AN2") = 0 Then
    Exit Sub
    End If
    If Range("AN2") = 1 And Range("AN27") = 0 Then
    MsgBox "No Records Found"
    Range("AN2").FormulaR1C1 = 0
    End If
    If Range("AN2") = 1 And Range("AN27") = 1 Then
     application.ScreenUpdating = False
     Range("AO28:AO5028").Value = Range("AN28:AN5028").Value
     Range("T28:AO5028").Select
        ActiveWorkbook.Worksheets("COMPLETED WORK ORDERS").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("COMPLETED WORK ORDERS").Sort.SortFields.Add Key:= _
            Range("AO28:AO5028"), SortOn:=xlSortOnValues, Order:=xlDescending, _
            DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("COMPLETED WORK ORDERS").Sort.SortFields.Add Key:= _
            Range("T28:T5028"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
            :=xlSortNormal
        With ActiveWorkbook.Worksheets("COMPLETED WORK ORDERS").Sort
            .SetRange Range("T28:AO5028")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    MsgBox "All Data Retrieved"
    Range("AN2").FormulaR1C1 = 0
    Range("E8").Select
    End If
    End Sub
    Please help me to make the proper modification. All help will be greatly appreciated!

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You're selecting cells in a SelectionChange event. If you need to do that (and I can't see any need here) you must disable events first so that the event doesn't trigger itself recursively.
    Be as you wish to seem

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("AN2").Value = 0 Then
    Exit Sub
    Elseif Range("AN2").value = 1 and Range("AN27").value = 0 Then
    Msgbox "No Records Found"
    ElseIF Range("AN2").value = 1 and Range("AN27").value = 1 Then
    Application.ScreenUpdating = False
    Application.EnableEvents= False
    Range("AO28:AO5028".Value = Range("AN28":AN5028").value
    With ActiveWorkbook.Worksheets("COMPLETED WORK ORDERS").Sort 
    .SetRange Range("T28:AO5028") 
    .Header = xlGuess 
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = true
    MsgBox "All Data Retrieved"
    Range("AN2").Value = 0
    Range("E8").Activate
    End With
    End if
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Assuming this code is in the COMPLETED WORK ORDERS sheet:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        On Error GoTo catch
        If Range("AN2") = 0 Then Exit Sub
    
    
        If Range("AN2") = 1 Then
            If Range("AN27") = 0 Then
                MsgBox "No Records Found"
                Range("AN2").Value2 = 0
            ElseIf Range("AN27") = 1 Then
                With Application
                    .ScreenUpdating = False
                    .EnableEvents = False
                End With
        
                Range("AO28:AO5028").Value = Range("AN28:AN5028").Value
                With Me.Sort
                    With .SortFields
                        .Clear
                        .Add Key:=Me.Range("AO28:AO5028"), SortOn:=xlSortOnValues, _
                             Order:=xlDescending, DataOption:=xlSortNormal
                        .Add Key:=Me.Range("T28:T5028"), SortOn:=xlSortOnValues, _
                             Order:=xlAscending, DataOption:=xlSortNormal
                    End With
                    .SetRange Me.Range("T28:AO5028")
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                MsgBox "All Data Retrieved"
                Range("AN2").Value2 = 0
        
                With Application
                    .EnableEvents = True
                    .ScreenUpdating = True
                End With
        
            End If
            
        End If
        
    finally:
        Application.EnableEvents = True
        Exit Sub
        
    catch:
        MsgBox Err.Description
        Resume finally
    End Sub
    Be as you wish to seem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •