Solved Cant figure out how to trigger second part of code.

April 25, 2017 at 17:50:44
Specs: Windows 7
i have this code that applies a filter refresh if a certain condition is met and then it goes to "EventsTrue" i am having trouble introducing if a second condition is met instead how to get "EventsTrueAlso" to work. I am a novice at Macro so please bare with my explanations. Here is the code i am working with

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        If Target.Column = 12 Then
            If Target = "Complete" Then
            On Error GoTo EventsTrue
   
EventsTrue:
    
    ActiveSheet.AutoFilter.ApplyFilter
    Sheets("Gantt").AutoFilter.ApplyFilter

EventsTrueAlso:
    Sheets("Gantt").AutoFilter.ApplyFilter
    
    End If
        End If
            End If
 
End Sub

message edited by MattExcel


See More: Cant figure out how to trigger second part of code.

Report •

#1
April 26, 2017 at 06:54:10
✔ Best Answer
Your code makes no sense, especially when it comes to your labels. It looks like you copied some previous code - where the labels meant something - and just used them again. Without trying to sound harsh, that's very sloppy coding.

The EventsTrue: label from the other macro was a label used to direct the code to the instructions that enabled Events either because of an error produced by the code or when the code exiting cleanly. They mean nothing in this case. If you are going to use labels then the string used as the label should mean something that the reader can understand. A label is nothing more than a string created by the author of the code, so it can be almost anything you like, as long as it's not a reserved word. Something like ApplyMyFilter: might be an appropriate label in this case because it tells the reader what the next section of code is going to do.

That said, I don't understand what you mean when you say that you are "having trouble introducing if a second condition is met instead how to get "EventsTrueAlso" to work". I don't see a second condition or an Else statement.

You need to explain what you are trying to do in more detail.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#2
April 26, 2017 at 10:53:16
Yeah, so far the code you've written down boils down to this:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Cells.Count = 1 And Target.Column = 12 And Target(1) = "Complete" Then
    ActiveSheet.AutoFilter.ApplyFilter
    Sheets("Gantt").AutoFilter.ApplyFilter
    Sheets("Gantt").AutoFilter.ApplyFilter
  End If
End Sub

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#3
April 27, 2017 at 17:09:48
Sorry Guys you are right, but as i have said i am a novice trying to learn Macro on my own so some of the things i may do are not conventional and possibly make no sense and that's why there are forums like this to get help and to provide help. The code i borrowed was from another post that you helped me out with DerbyDad, but i agree i should have been a little more creative when it came to the labels. i figured out how to get the second situation to work which was to have it after the "end ifs" of the first situation and it seems to work but please have a look at it and let me know if 1.) it makes sense, and 2.) if there is a simplified way to write it.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        If Target.Column = 12 Then
            If Target = "Complete" Then
            On Error GoTo RefreshFilter
            
           
RefreshFilter:
    
    ActiveSheet.AutoFilter.ApplyFilter
    Sheets("Gantt").AutoFilter.ApplyFilter
  
    End If
        End If
            End If
               
        If Target.Cells.Count = 1 Then
        If Target.Column = 12 Then
            If Target = "In Progress" Then
            On Error GoTo RefreshFilterGanttOnly


Report •
Related Solutions


Ask Question