【问题标题】:Only executing a worksheet change when a cell goes from blank to populated仅当单元格从空白变为已填充时才执行工作表更改
【发布时间】:2021-02-19 14:55:35
【问题描述】:

我在尝试为列表中的新项目编号并在同一行中为相关列填充相关默认值时遇到了一个问题。如果单元格已经填充了数据,我不希望其他列中的单元格发生变化。即,如果目标列中的目标单元格中​​有信息并且有人编辑了信息,我不希望它删除该行其他列中的信息。

我已经编写了以下代码来实现这一点,但我不确定如何检查目标单元格中​​是否已经有文本,因为我尝试过的方法不起作用(如果 IsEmpty(Target.Value) = True Then)。

If IsEmpty(Target.Value) = True Then

Dim Description_Column_No As Integer
Dim Default_Start As String

'Inputs
Default_Start = "N/A"
Description_Column_No = 3

'Output Columns
Output_Col_Item = Description_Column_No - 1
Output_Col_1 = Description_Column_No + 6
Output_Col_2 = Description_Column_No + 18
Output_Col_3 = Description_Column_No + 28
Output_Col_4 = Description_Column_No + 38
Output_Col_5 = Description_Column_No + 44
Output_Col_6 = Description_Column_No + 55
Output_Col_7 = Description_Column_No + 62


'Adding the date to the relevant cell

'Setting the Item Number for the new Item
With _
Target(Target.Row, Output_Col_Item).Value = Target.Row - 5
End With

'Setting the dropdowns to default values

With _
Target(Target.Row, Output_Col_1).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_2).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_3).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_4).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_5).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_6).Value = Default_Start
End With
With _
Target(Target.Row, Output_Col_7).Value = Default_Start
End With

End If

【问题讨论】:

  • 可能在行中的其他单元格上使用IsEmpty,而不是Target
  • 我想对其进行设置,以便用户只需输入项目的名称,然后自动为其分配一个数字并填充默认列。所以它实际上只是我想要应用代码的那个单元格。
  • "如果单元格中已经填充了数据,我不希望其他列中的单元格发生变化。"... 然后你必须检查 other 单元格在修改它们之前。
  • 好主意!我试过: If IsEmpty(Me.Range(Status_Col_2 & Target.Row)) = True 或 IsEmpty(Me.Range(Status_Col_2 & Target.Row)) = True 然后
  • 您的 With 块的结构都很奇怪,因此这可能是问题的一部分 - 块没有内容,因为行继续使“内容”成为 With 的一部分

标签: excel vba


【解决方案1】:

先拆包:

With _
Target(Target.Row, Output_Col_1).Value = Default_Start
End With

这是一个空的 With 块(如果您删除 _ 会更明显)

With Target(Target.Row, Output_Col_1).Value = Default_Start
   'nothing here...
End With

这个:

Target(Target.Row, Output_Col_1)

一样
Target.Cells(Target.Row, Output_Col_1)

但可能会产生意想不到的结果,因为Cells() 总是运行相对于它被调用的对象,例如,如果Target 是(例如)B5,那么上面的表达式与[B5].Cells(5, 9) 或 J9...

最后,当With 块运行时,看起来像一个值赋值(“将范围值设置为'N/A'”)作为值运行比较 em> ("is the range value 'N/A' ?") ,所以它不会导致您的工作表有任何更新。

也就是说,感觉我们在其余未显示的代码中遗漏了一些上下文,但您发布的代码可以改进/压缩:

Const DESCR_COL As Long = 3
Const DEFAULT_VAL As String = "N/A"

Dim v, c As Range
'...
'...
If IsEmpty(target.Value) Then
    With target.EntireRow
        .Cells(DESCR_COL - 1).Value = .Row - 5
        For Each v In Array(6, 18, 28, 38, 44, 55, 62)
            Set c = .Cells(DESCR_COL + v)
            If Len(c.Value) = 0 Then c.Value = DEFAULT_VAL
        Next v
    End With
End If

使用行号来分配“Id”是一个问题 - 重新排序或插入/删除行将导致下一个条目出现问题。

【讨论】:

  • 这个替代代码效果很好,我只需要在“If IsEmpty(target.Value) Then”的开头添加“not”并将列号更改为 -1。感谢您提供优雅的解决方案!
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 2015-05-13
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2017-03-20
相关资源
最近更新 更多