每当你改变你正在编辑的行时,运行一个VBA代码

我需要在excel VBA中运行代码,只要你改变你正在处理的行。

我的代码更新其他工作簿,每当你编辑的东西,但由于所有的信息在行中应该更新1工作簿,我希望它打开并更新其他工作簿只有当你停止工作在一行(任何行)

现在我有一个代码更新另一个工作簿,当我改变一个单元格,所以这就是我所拥有的。

Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range Set KeyCells = Range("A1:C10") Dim WB As Workbook Dim dirFile As String Dim strFile As String Dim Actual As Workbook Static lngRow As Long Dim linea Set Actual = ActiveWorkbook If Not Application.Intersect(KeyCells, Range(Target.Address)) _ Is Nothing Then MsgBox "Cell " & Target.Address & " has changed." dirFile = "N:\Otros\sebastian\TEST 2\" strFile = Trim(Cells(Target.Row, 1).Value) If Len(Dir(dirFile & strFile & ".xlsx")) = 0 Then Application.Visible = False linea = Target.Row Set WB = Workbooks.Add ' ESTILO ' __________________________ WB.Worksheets(1).Columns("A").ColumnWidth = 28 ' ___________________________ 'DATOS '____________________________ WB.Worksheets(1).Cells(1, 1).Value = "Titulo de Proyecto" WB.Worksheets(1).Cells(1, 2).Value = Actual.Worksheets(1).Cells(linea, 2) WB.SaveAs (dirFile & strFile & ".xlsx") WB.Close Application.Visible = True End If End If End Sub 

在工作表模块中使用一个全局variables来实现这一点。 当纸张被激活时(如果切换到另一张纸张)currRowvariables设置为0。 然后检查目标行与之前的行。

 Private currRow As Long Private Sub Worksheet_Activate() currRow = 0 End Sub Private Sub Worksheet_Change(ByVal Target As Range) If currRow = Target.Row Then 'Do Nothing Else If currRow <> 0 Then 'Do Rest of code then set the target to the new row End If currRow = Target.Row End If End Sub