vba - Worksheet_change : entire column value deleted, 在此操作之前识别非空单元格

标签 vba excel comments worksheet

我有一个带有用于输入的主工作表的工作簿,并且根据主工作表中“类型”列的单元格值将主工作表中的值复制到 2 个子工作表。

针对这些复制的单元格的子表中的“评论”列中的任何值都作为评论添加到主表的相应行中。当子表中的“评论”列中的值被立即删除时,我想确定此操作之前的非空单元格并删除主表中的相应注释。

目前,如果在子表的“注释”列中添加/删除一个值,我已经编写了代码,然后将在主表的相应条目中添加/删除注释。

    Private Sub Worksheet_Change(ByVal Target As Range)

Dim temp As String
Dim tem As String
With Target
       If .Count = 1 And .Column = 8 And .Row < 600 Then
       tem = .Row
             If Sheets("Parts- input").Cells(tem, 8).Comment Is Nothing Then
                If Sheets("Pins").Cells(.Row, .Column).Value = "" Then
                   Sheets("Parts- input").Cells(tem, 8).Comment.Delete
              Else
               Sheets("Parts- input").Cells(tem, 8).AddComment "Lifts Sheet: " & Sheets("Pins").Cells(.Row, .Column).Value
               End If
             Else
              If Sheets("Pins").Cells(.Row, .Column).Value = "" Then
              Sheets("Parts- input").Cells(tem, 8).Comment.Delete
              Else
              Sheets("Parts- input").Cells(tem, 8).Comment.Text "Lifts Sheet: " & Sheets("Pins").Cells(.Row, .Column).Value
              End If
            End If
   End If
End With
End Sub

最佳答案

只是玩你的代码,我同意这个:

Private Sub Worksheet_Change(ByVal Target As Range)
  With Target
    If .Count = 1 And .Column = 8 And .row < 600 Then
      If Sheets("Pins").Cells(.row, .Column).Value = "" Then
        Sheets("Parts- input").Cells(.row, 8).Comment.Delete
      Else
        If Sheets("Parts- input").Cells(.row, 8).Comment Is Nothing Then
          Sheets("Parts- input").Cells(.row, 8).AddComment "Lifts Sheet: " & Sheets("Pins").Cells(.row, .Column).Value
        Else
          Sheets("Parts- input").Cells(.row, 8).Comment.Text "Lifts Sheet: " & Sheets("Pins").Cells(.row, .Column).Value
        End If
      End If
    Else
      If Not Intersect(Target, Target.Parent.Range("H1:H599")) Is Nothing Then
        Dim runner As Range, rng As Range
        For Each runner In Intersect(Target, Target.Parent.Range("H1:H599")).Cells
          If Sheets("Pins").Cells(runner.row, 8).Value = "" Then
            If rng Is Nothing Then
              Set rng = Sheets("Parts- input").Cells(runner.Rows, 8)
            Else
              Set rng = Union(rng, Sheets("Parts- input").Cells(runner.Rows, 8))
            End If
          End If
        End If
      Next
      rng.Comment.Delete
    End If
  End With
End Sub

你可以直接删除它们,但是有很多单元格,一步完成会更快:)

编辑 包括Intersect提高速度

关于vba - Worksheet_change : entire column value deleted, 在此操作之前识别非空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263294/

相关文章:

excel - 使用 VBA 从 Word 获取文本到 Excel

html - Excel Web Scraping - ul 列表项上的 fireEvent

regex - 删除多行 C 样式/*注释*/使用 Perl 正则表达式

excel - 需要循环遍历 AI 列,如果单元格不为空,则查看 W 列并在下一列中移动数字

ms-access - Access - 超链接未链接

xml - 如何忽略 XML 命名空间

javascript - 是否有关于如何用注释记录 JavaScript 文件的约定?比如函数签名、示例等

mysql - 查询数据库后,如何在 Excel 中打印值列表?

file - 重新初始化 "ThisWorkbook.Path"

wcf - 如何使用 WSDL 和 XSD 的注释生成 .NET 类