如何在同一工作表中使用2个差异代码块?

时间:2019-10-30 15:38:47

标签: excel vba

我想在同一工作表中运行2个不同的代码。

我以为它在尝试时可以工作,但是当我运行第一段代码时,它不起作用,并在第二段代码中停止了我的调试工作。

第二段代码工作正常。

这是我的2个代码块:

第一个代码是将名称从一张纸复制到另一张纸。第二个代码是如果单元格AE = D

,则删除单元格Oui中的信息。
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

'THIS IS MY FIRST BLOCK OF CODE

    If Target.Column = 2 Or Target.Column = 3 Then
        Dim wsSuiviDeProjet As Worksheet
        Set wsSuiviDeProjet = ActiveWorkbook.Sheets("SuiviDeProjet")     
     On Error GoTo EH
         Dim NewRowCount As Long
         NewRowCount = Target.Row
         If wsSuiviDeProjet.Cells(NewRowCount, 2).Value <> "" And wsSuiviDeProjet.Cells(NewRowCount, 3).Value <> "" Then                    
             Dim wsMonitoring As Worksheet
             Set wsMonitoring = ActiveWorkbook.Sheets("Monitoring")
             Dim wsCoordonnées As Worksheet
             Set wsCoordonnées = ActiveWorkbook.Sheets("coordonnées")           
             If wsSuiviDeProjet.Cells(NewRowCount, 3).Value = "oui" Then                        
                 Dim LastRowMonitoringSheet As Integer
                 LastRowMonitoringSheet = wsMonitoring.Cells(wsMonitoring.Rows.Count, 1).End(xlUp).Row                           
                 Dim dataExists As Boolean
                 dataExists = False
                 Dim ii As Integer
                     For ii = 2 To LastRowMonitoringSheet
                         If wsMonitoring.Cells(ii, 1).Value = wsSuiviDeProjet.Cells(NewRowCount, 2).Value Then
                             dataExists = True
                             Exit For
                         End If
                     Next ii               
                         If Not dataExists Then
                             wsMonitoring.Rows(LastRowMonitoringSheet).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                             LastRowMonitoringSheet = wsMonitoring.Cells(wsMonitoring.Rows.Count, 1).End(xlUp).Row                                           
                             wsMonitoring.Cells(LastRowMonitoringSheet - 1, 1).Value = wsSuiviDeProjet.Cells(NewRowCount, 2).Value                                         
                         Else                                           
                         End If              
                 End If               
                 Dim LastRowCoordinatesSheet As Integer
                 LastRowCoordinatesSheet = wsCoordonnées.Cells(wsCoordonnées.Rows.Count, 1).End(xlUp).Row                    
                 Dim dataExistsM As Boolean
                 dataExistsM = False                           
                 Dim i As Integer
                     For i = 5 To LastRowCoordinatesSheet
                         If wsCoordonnées.Cells(i, 1).Value = wsSuiviDeProjet.Cells(NewRowCount, 2).Value Then
                             dataExistsM = True
                             Exit For
                         End If
                     Next i                            
                     If Not dataExistsM Then
                         wsCoordonnées.Cells(LastRowCoordinatesSheet + 1, 1).Value = wsSuiviDeProjet.Cells(NewRowCount, 2).Value
                         'MsgBox ("data inserted in COORDINATES SHEET")                                
                     Else                                
                     End If                                                       
             End If

    EH:

         Call CopyHighlightedData_Click
     End If   

    'THIS IS MY SECOND BLOCK OF CODE

     If Not Intersect(Target, Range("D:D")) Is Nothing Then
         If Target = "oui" Then
             Range("AE" & Target.Row).Clear
         End If
     End If

End Sub

0 个答案:

没有答案