Exemplo do método SetFocus, das propriedades LineCount, TextLength

O exemplo a seguir conta os caracteres e o número de linhas de texto em um TextBox usando as propriedades LineCount e TextLength e o método SetFocus .

In this example, the user can type into a TextBox, and can retrieve current values of the LineCount and TextLength properties.

Para usar este exemplo, copie esta amostra de código para a parte Declarações de um formulário. Make sure that the form contains the following controls:

  • A TextBox named TextBox1.
  • A CommandButton named CommandButton1.
  • Two Label controls named Label1 and Label2.
'Type SHIFT+ENTER to start a new line in the text box. 
 
Private Sub CommandButton1_Click() 
 'Must first give TextBox1 the focus to get line 
 'count 
 TextBox1.SetFocus 
 Label1.Caption = "LineCount = " _ 
 & TextBox1.LineCount 
 Label2.Caption = "TextLength = " _ 
 & TextBox1.TextLength 
End Sub 
 
Private Sub UserForm_Initialize() 
 CommandButton1.WordWrap = True 
 CommandButton1.AutoSize = True 
 CommandButton1.Caption = "Get Counts" 
 
 Label1.Caption = "LineCount = " 
 Label2.Caption = "TextLength = " 
 
 TextBox1.MultiLine = True 
 TextBox1.WordWrap = True 
 TextBox1.Text = "Enter your text here." 
End Sub

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.