我目前正在使用VBA来检查特定列中的单元格何时发生更改,因此我可以调用另一个宏来对它们进行排序。这非常有效,除了每当我插入新行时它也会触发。因此,使用IsEmpty,我添加了一个检查,以查看相关单元格是否为空。但我显然做错了,因为每当我插入一行时我的宏仍会被调用。我做错了什么?
在单元格更改时触发的VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("A:A")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If Not IsEmpty(KeyCells) Then
Call SortByDate
End If
End If
End Sub
答案 0 :(得分:2)
您可以通过检查接收更改的单元格数来过滤掉行插入。在行插入的情况下,这大于或等于工作表的System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
at System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreCommandAsync(TransactionalBehavior transactionalBehavior, String commandText, CancellationToken cancellationToken, Object[] parameters)
at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommandAsync(TransactionalBehavior transactionalBehavior, String sql, CancellationToken cancellationToken, Object[] parameters)
at System.Data.Entity.Database.ExecuteSqlCommandAsync(TransactionalBehavior transactionalBehavior, String sql, CancellationToken cancellationToken, Object[] parameters)
at System.Data.Entity.Database.ExecuteSqlCommandAsync(String sql, CancellationToken cancellationToken, Object[] parameters)
at System.Data.Entity.Database.ExecuteSqlCommandAsync(String sql, Object[] parameters)
at Common.Dal.AuditDbContext.<>c__DisplayClass20_0.<WriteAuditsParallelAsync>b__0(Audit x) in
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.Threading.Tasks.Task.WhenAll[TResult](IEnumerable`1 tasks)
at Common.Dal.AuditDbContext.<WriteAuditsParallelAsync>d__20.MoveNext() in
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Common.Dal.AuditDbContext.<SaveToDatabaseAsync>d__18.MoveNext() in
。如果您要更改该工作表上的任何内容,请在开始更改任何内容之前使用public override Task<Int32> SaveChangesAsync( CancellationToken cancellationToken )
{
var modified = this.GetModifiedOrDeletedEntities();
var added = this.GetAddedEntities();
var audits = AuditService.GetAudits( GetObjectStateManager(), modified );
//Call SaveChangesAsync
var result = await base.SaveChangesAsync( cancellationToken );
audits.AddRange( AuditService.GetAudits( GetObjectStateManager(), added ) );
//Call stored prcedures
await WriteAuditsAsync( audits, user );
return result;
}
private async Task WriteAuditsAsync(List<Audit> audits, String user)
{
foreach ( var audit in audits)
{
try
{
...
//Execute SQL command
await Database.ExecuteSqlCommandAsync(myCommand, myParameters);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
,并在离开该子网之前使用columns.count
。
application.enableevents = false
未能禁用事件处理并随后更改工作表上的任何内容将触发另一个更改事件,Worksheet_Change事件宏将尝试在其自身上运行。