0

我有以下代码:

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 的值是整数,它会起作用,但如果我使用文本或长数字,文本和数字的组合(此处输入的数据将采用这种形式),那么它不会工作。

4

1 回答 1

1

更改两行:

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
于 2013-08-23T12:03:22.013 回答