访问VBA -从函数(ByRef)内更改文本框值



我试图写一个子,将得到两个参数-一个文本框的形式和文本。我的意图是该函数将文本附加到任何文本框中。

Sub AppendTextBox([any textbox in my form], text As String)
[code that appends the text parameter to the textbox]
End Sub

请注意,我并没有试图将文本附加到特定的文本框中,而是创建一个函数,该函数可以接收表单中的任何文本框并将其附加到任何文本。

谢谢你的帮助。

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

Private Sub AAA(A)
A.Value = "Desired text"
End Sub

或者如果你想添加:

Private Sub AAA(A)
A.Value = A.Value & vbnewline & "Desired text"
End Sub

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

可以通过以下方式完成:

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

相关内容

  • 没有找到相关文章

最新更新