且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA - 活动单元格中TextBox的文本

更新时间:2023-02-13 16:27:13

以下是一段代码,当您点击单元格时将运行

  Private Sub Worksheet_SelectionChange(ByVal Target As Range)
如果Selection.Count = 1然后
如果不相交(目标,范围(A1))是没有,然后
'userform的名称。显示
结束如果
结束如果
结束Sub

代码中有一个注释,你需要你的userform的名字,然后是。显示



输入文字,可以按照您的建议说A1:

  Private Sub CommandButton1_Click()

TextValue = TextBox1.Text

[A1] .Value = TextValue

End Sub
pre>

将$ code> CommandButton1 和 TextBox1 命名他们(如果你命名他们,你应该)



如果你需要任何澄清只要问


I would like to build a simple UserForm with a TextBox and a CommandButton, which opens itself by clicking inside a empty cell in a special column (for example in the column A, the cell A1) inside the worksheet. When I enter a text and hit the button, the entered text should be pasted in the active cell (A1).

The building of the UserForm is not the problem, but the actions which are behind it.

Do you guys have an idea how to solve this? Or even better pieces of code?

Thanks in advance!

Here is a piece of code that will run when you click a cell

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A1")) Is Nothing Then
            'name of userform .Show
        End If
    End If
End Sub

Where the code has a comment you need the name oof your userform and then .Show

Entering the text into, lets say A1 as you suggested, would be:

Private Sub CommandButton1_Click()

TextValue = TextBox1.Text

[A1].Value = TextValue

End Sub

Change the CommandButton1 and TextBox1 to whatever you named them(if you named them, which you should)

If you need anything clarified just ask