每当您更改正在处理的行时,我都需要在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
答案 0 :(得分:1)
在工作表模块中使用全局变量来完成此操作。激活工作表时,currRow变量设置为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