如何用Excel VBA代码解决删除按钮的问题?

nzkunb0c  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(456)
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim cell As Range
    
    ' Check if any cell in column A is modified
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        ' Set the range from the modified cell in column A to column D
        Set rng = Range(Target, Cells(Target.Row, "D"))
        
        ' Remove existing borders if the corresponding cell is empty
        If IsEmpty(Target) Then
            rng.Borders.LineStyle = xlNone
        Else
            ' Add new borders to each cell
            For Each cell In rng
                With cell.Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
                With cell.Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
                With cell.Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
                With cell.Borders(xlEdgeRight)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
            Next cell
        End If
    End If
End Sub

我希望即使我一次删除多个单元格的值,边框也应该自动删除。

h9vpoimq

h9vpoimq1#

您可以使用以下结构作为模板,用相关代码替换注解行:

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim targetrange As Range, targetcell As Range
    Set targetrange = Intersect(Target, Range("A:A"))
    
     If Not targetrange Is Nothing Then
    
        For Each targetcell In targetrange
        
            '.. do something with targetcell
        
        Next

    End If

End Sub
mzsu5hc0

mzsu5hc02#

使用下面的代码,您可以同时粘贴多个值,也可以一次删除多个单元格。在粘贴过程中,某些单元格可能会增加值,而其他单元格可能会丢失值...

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim ir As Range, ar As Range, cell As Range
   Dim allEmpty As Range, allValue As Range
   ' Check if any cell in column A is modified
   Set ir = Intersect(Target, Range("A:A"))
   If Not ir Is Nothing Then
      ' Something changed... make two ranges one with all empty cells
      ' and one of cells with value
      For Each cell In ir
         If cell.Value2 = vbNullString Then
            If allEmpty Is Nothing Then
               Set allEmpty = cell
            Else
               Set allEmpty = Union(allEmpty, cell)
            End If
         Else
            If allValue Is Nothing Then
               Set allValue = cell
            Else
               Set allValue = Union(allValue, cell)
            End If
         End If
      Next
      'I loop through the Areas and not the cells
      ' of empty and filled cells
      If Not allEmpty Is Nothing Then
         For Each ar In allEmpty.Areas
            ar.Resize(ar.rows.CountLarge, 4).Borders.LineStyle = xlNone
         Next
      End If
      If Not allValue Is Nothing Then
         For Each ar In allValue.Areas
            With ar.Resize(ar.rows.CountLarge, 4).Borders
               .LineStyle = xlContinuous
               .Weight = xlThin
            End With
         Next
      End If
   End If
End Sub

相关问题