VBA如果Not Intersect不适用于worksheet_change上的较大值

时间:2013-08-23 11:35:55

标签: vba excel-vba excel

我有以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim btn As Button
  Dim t As Range
  Dim i As Integer
  i = Target.Row
  If Not Intersect(Target, Range("$B10:$B103")) Then
    If Target.Value <> "" Then
      For Each btn In ActiveSheet.Buttons
        If btn.Name = "I" & i Then
          btn.Delete
        End If
      Next btn
      Set t = ActiveSheet.Range(Cells(i, 9), Cells(i, 9))
      Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
      With btn
        .OnAction = "imageshow"
        .Caption = "View Images"
        .Name = "I" & i
      End With
    Else
      For Each btn In ActiveSheet.Buttons
        If btn.Name = "I" & i Then
          btn.Delete
        End If
      Next btn
    End If
  End If
End Sub

当我运行它时,如果输入B10:B103的值是一个整数,它会起作用,但如果我使用文本或长数字组合文本和数字(这里输入的数据将是这种形式)然后它不会起作用。

有没有人有任何想法?

干杯

1 个答案:

答案 0 :(得分:0)

更改两行:

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim btn As Button
  Dim t As Range
  Dim i As Long
  i = Target.Row
  If Not Intersect(Target, Range("$B10:$B103")) Is Nothing Then
    If Target.Value <> "" Then
      For Each btn In ActiveSheet.Buttons
        If btn.Name = "I" & i Then
          btn.Delete
        End If
      Next btn
      Set t = ActiveSheet.Range(Cells(i, 9), Cells(i, 9))
      Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
      With btn
        .OnAction = "imageshow"
        .Caption = "View Images"
        .Name = "I" & i
      End With
    Else
      For Each btn In ActiveSheet.Buttons
        If btn.Name = "I" & i Then
          btn.Delete
        End If
      Next btn
    End If
  End If
End Sub