+ Reply to Thread
Results 1 to 4 of 4

vba "evaluate" code not working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    vba "evaluate" code not working

    Hello, I am making a logbook, where I enter flight details in the AllData sheet, and when I select "ok" in column L, the code should automatically copy the information to the pilots sheets and make some adjustments.
    The code I wrote with the help of the members of this forum works good for this part.
    Now if I select CLR in AllData sheet, let's say in row 6, the code should go to the pilot sheet (pilot3) and check which row has the same information as this one. (in this case it is "row 5" in "pilot3" )
    Then it should send a message box with the value of the row number.
    But the code is not working for this part, it always returns a message box with the number 0...I think the error is in the " (destRow3 = Evaluate...) part " perhaps there is something wrong or something missing, and I am not able to continue, so your help is much appreciated, thanks.
    This is the code I used, and I uploaded a sample of the workbook.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myDestSheet As Worksheet, reply As String
        Dim myDestSheet2 As Worksheet, destRow As Long, destRow2 As Long
        Dim myDestSheet3 As Worksheet, destRow3 As Long
        With ThisWorkbook
            If Target.Cells.Count = 1 And Target.Column = 12 And Target = "ok" Then
                On Error Resume Next
                Set myDestSheet = Worksheets(Target.Offset(, -11).Value)
                
                If Target.Value = "ok" Then
                    reply = MsgBox("Are you sure about the information?" & vbNewLine & "Press YES to confirm" & vbNewLine & "Or Press NO to cancel and edit again", vbYesNo)
                End If
                
                If reply = vbYes Then
                
                If Err.Number = 9 Then
                On Error Resume Next
                Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                    MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                    Err.Clear
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                        Err.Clear
    
                    
                    Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist"
                    Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -10).Value
    Err.Clear
                    End If
                        Else
                    destRow = myDestSheet.Cells(Rows.Count, "a").End(xlUp).Row + 1
                    Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist"
                        Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -11).Value
    Err.Clear
                        Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                    End If
                    End If
                    
                     Else
                           Target.Value = ""
                    
                     End If
            Else
           If Target.Cells.Count = 1 And Target.Column = 12 And Target = "CLR" Then
                Set myDestSheet3 = Worksheets(Target.Offset(, -11).Value)
    destRow3 = Evaluate("=SUMPRODUCT(--('" & myDestSheet3.Name & "'!A3:A10007=""" & Target.Parent.Cells(Target.Row, 3).Value & """),--('" & myDestSheet3.Name & "'!B3:B10007=""" & Target.Parent.Cells(Target.Row, 4).Value & """),--('" & myDestSheet3.Name & "'!d3:d10007=""" & Target.Parent.Cells(Target.Row, 2).Value & """),ROW(d3:d10007))")
    Application.EnableEvents = False
    With myDestSheet3
    Rows(destRow3 & ":" & destRow3).Select
    Selection.ClearContents
     End With
     Application.EnableEvents = True
    
     End If
    End If
            
        End With
    End Sub
    Attached Files Attached Files
    Last edited by chiidzzz; 01-25-2013 at 05:51 PM.

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: vba "evaluate" code not working

    the entire macro is governed by Target being "ok" only (since it is part of an ...AND ... clause) so if the target is set to CLR it does execute it just exits.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,658

    Re: vba "evaluate" code not working

    Your new "CLR" code in post#1 was not included in the attached file. That was confusing.

    This uses Autofilter to find the multi-criteria match and then clears the row

            ElseIf Target.Cells.Count = 1 And Target.Column = 12 And Target = "CLR" Then
                Application.EnableEvents = True
                With Worksheets(Target.Offset(, -11).Value)
                    With .Range("A2", .Range("A" & Rows.Count).End(xlUp)).EntireRow
                        .AutoFilter Field:=1, Criteria1:=Format(Range("C" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=2, Criteria1:=Range("D" & Target.Row).Value
                        .AutoFilter Field:=4, Criteria1:=Range("B" & Target.Row).Value
                        .Offset(1).ClearContents
                        '.Offset(1).Delete  ' delete rows instead of clear
                    End With
                    .AutoFilterMode = False
                End With
            End If
            Application.EnableEvents = True
        End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: vba "evaluate" code not working

    thank you alphafrog, it works
    but i need to expand it a little bit, so it searches for the entire row before deleting it, so will this one work?
              ElseIf Target.Cells.Count = 1 And Target.Column = 12 And Target = "CLR" Then
                Application.EnableEvents = True
                With Worksheets(Target.Offset(, -11).Value)
                    With .Range("A2", .Range("A" & Rows.Count).End(xlUp)).EntireRow
                        .AutoFilter Field:=1, Criteria1:=Format(Range("C" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=2, Criteria1:=Range("D" & Target.Row).Value
                        .AutoFilter Field:=4, Criteria1:=Range("B" & Target.Row).Value
                        .AutoFilter Field:=5, Criteria1:=Format(Range("E" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                                                                 
                        .AutoFilter Field:=6, Criteria1:=Format(Range("F" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=7, Criteria1:=Format(Range("G" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=8, Criteria1:=Format(Range("I" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=9, Criteria1:=Format(Range("K" & Target.Row).Value, _
                                                                 .Parent.Range("A3").NumberFormat)
                        .AutoFilter Field:=10, Criteria1:=Range("H" & Target.Row).Value
                        .AutoFilter Field:=11, Criteria1:=Range("J" & Target.Row).Value
                        .Offset(1).ClearContents
                        '.Offset(1).Delete  ' delete rows instead of clear
                    End With
                    .AutoFilterMode = False
                End With
            End If
            Application.EnableEvents = True
        End With
    End Sub
    and how can i make it delete the same row for the second pilot also?
    Worksheets(Target.Offset(, -10).Value)
    thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1