Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA在excel的文本框中显示单元格内容_Vba_Excel - Fatal编程技术网

使用VBA在excel的文本框中显示单元格内容

使用VBA在excel的文本框中显示单元格内容,vba,excel,Vba,Excel,我有一系列包含数据的单元格。我想要一个文本框,当我单击文本框中的任何单元格时,显示单元格内容。这可能吗?谢谢将此添加到工作表中(请参见黑色箭头): 通常,如果要检查特定范围,可以在事件中定义范围: Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rngPreselected As Range Set rngPreselected = Range("A1:B10")

我有一系列包含数据的单元格。我想要一个文本框,当我单击文本框中的任何单元格时,显示单元格内容。这可能吗?谢谢

将此添加到工作表中(请参见黑色箭头):

通常,如果要检查特定范围,可以在事件中定义范围:

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
是定义的范围


这称为
事件
。在此处查看有关事件的更多信息:

您可以使用以下内容:

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)中的
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