2017-05-26 791 views

回答

1

你可以只使用這樣的事情:

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

Dim i As Long 
Dim lRow As Long 

lRow = Cells(Rows.Count, 1).End(xlUp).Rows 

For i = 1 To lRow 
If Cells(i, 1).Count = 1 Then 
If Cells(i, 1) = "" Then 
Else 
If Not Intersect(Target, Cells(i, 1)) Is Nothing Then 
MsgBox (i) 
End If 
End If 
End If 
Next i 
End Sub 

這將顯示在消息框中的值,而不是一個文本框。不知道爲什麼你需要一個文本框。

我指的是行,改變lRow = Cells(Rows.Count, 1).End(xlUp).Rows1你在

0

一下添加到工作表(見黑色箭頭):enter image description here

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    MsgBox Target.Value 

End Sub 

在一般情況下,如果要檢查特定的範圍,你可以定義事件的範圍:

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    Dim rngPreselected As Range 
    Set rngPreselected = Range("A1:B10") 

    If Not Intersect(Target, rngPreselected) Is Nothing Then 
     MsgBox Target.Value 
    End If 

End Sub 

在這種情況下,A1:B10是定義的範圍。

這就是所謂的Event。查看更多有關事件的位置:http://www.cpearson.com/excel/events.aspx

相關問題