访问VBA - 从函数内更改TextBox值(ByRef)

时间:2013-06-04 22:06:29

标签: function vba textbox byref

我正在尝试编写一个子类,它将获得两个参数 - 表单中的文本框和文本。 我的意图是该函数会将文本附加到任何文本框中。

Sub AppendTextBox([any textbox in my form], text As String)

[code that appends the text parameter to the textbox]

End Sub

请注意,我不是要尝试将文字附加到特定文本框,而是创建一个可以接收表单中任何文本框的函数,并附加任何文本。

感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

我找到了答案,它比我想象的要简单得多:

Private Sub AAA(A)

A.Value = "Desired text"

End Sub

或者如果你想追加:

Private Sub AAA(A)

A.Value = A.Value & vbnewline & "Desired text"

End Sub

答案 1 :(得分:0)

您好Zephram有没有找到解决方案? 我有一个,但有点重,因为它使用循环。如果你有一个更好的让我知道。

Private Function change_TextBox2(altera As String, textbox As ctl, valor As Variant)
Dim ctl As Control
If altera = "Popula" Then
    For Each ctl In Me.Controls
        With ctl
         If (InStr(.Name, textbox)) > 0 Then
             .Value = valor
         End If
       End With
    Next ctl
ElseIf altera = "hide" Then
    For Each ctl In Me.Controls
        With ctl
         If (InStr(.Name, textbox)) > 0 Then
             .Visible = False
         End If
       End With
    Next ctl
End If
End Function

答案 2 :(得分:0)

可以通过以下方式完成:

dim appendTxt as String:  appendTxt = "appended text"
dim ws as Worksheet:  for each ws in ActiveWorkbook.Worksheets
    dim shape as Shape:  for each shape in ws.Shapes
        if shape.Type = msoTextBox then
            'you can move this code parameterized to a separate function then as req by OP:
            with shape.TextEffect:  .Text = .Text & appendTxt
        end if
    next shape
next ws