引用Variant数组到列

我有防止更改到一个以上的单元格的代码。 然而,它将允许一次删除多个单元格。 下面是我使用的代码,它运作良好。

Dim vClear As Variant Dim vData As Variant 'This prevents more than one cell from being changed at once. 'If more than one cell is changed then validation checks will not work. If Target.Cells.Count > 1 Then vData = Target.Formula For Each vClear In vData If vClear <> "" Then 'If data is only deleted then more than one cell can be changed. MsgBox "Change only one cell at a time", , "Too Many Changes!" Application.Undo Exit For End If Next End If 

我想添加到它是什么时候数据被删除我想要检查哪些列数据被删除。 如果任何一列满足要求,那么我需要删除另一列的等价行中的数据。

这是我正在尝试做的一个例子。 有2列,我需要检查,他们是G&H。如果从这两列中的任何一个删除数据,然后我希望列I也被删除。 比方说,我selectD5:G10的范围,并从中删除内容。 由于G列是我希望I5:I10删除的要求之一。 如果我要删除D5:F10,那么它不会删除列I中的任何内容,因为没有selectG或H列。

下面是我正在尝试做的一个示例代码。 我知道这是不可能的,我的代码如下工作,这只是我想要做的一个简短的总结,我无法弄清楚如何获得变体也检查列。 请让我知道如果有人知道如何做到这一点。

 Dim vClear As Variant Dim vData As Variant 'This prevents more than one cell from being changed at once. 'If more than one cell is changed then validation checks will not work. If Target.Cells.Count > 1 Then vData = Target.Formula For Each vClear In vData If vClear <> "" Then 'If data is only deleted then more than one cell can be changed. MsgBox "Change only one cell at a time", , "Too Many Changes!" Application.Undo Exit For Else If vClear = "" Then If vClear.Column = 7 Or vClear.Column = 8 Then ActiveSheet.Cells(vClear.Row, 9) = "" End If End If End If Next End If 

我已经修改了你的代码,以确定列G或H是否在目标中。 如果是这样,列I中的相应行也被清除。 我还在For循环的Else部分删除了一个不必要的Iftesting。

 Dim vClear As Variant Dim vData As Variant Dim firstRow As Long Dim lastRow As Long 'This prevents more than one cell from being changed at once. 'If more than one cell is changed then validation checks will not work. If Target.Cells.Count > 1 Then vData = Target.Formula For Each vClear In vData If vClear <> "" Then 'If data is only deleted then more than one cell can be changed. MsgBox "Change only one cell at a time", , "Too Many Changes!" Application.Undo Exit For Else ' if the target includes columns G or H, we also clear column i If Not Intersect(Target, Columns("G:H")) Is Nothing Then ' get the first row in the target range firstRow = Target.Rows(1).Row ' get the last row in the target range lastRow = firstRow + Target.Rows.Count - 1 ' clear contents of corresponding rows in column i ActiveSheet.Range(Cells(firstRow, 9), Cells(lastRow, 9)).ClearContents End If End If Next End If