VBA - Excel TextBox1将对象分配给可变 [英] VBA - Excel TextBox1 assign object to varaible

查看:196
本文介绍了VBA - Excel TextBox1将对象分配给可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Sub TB1()
Dim X作为TextBox
Dim Y As String
X = TextBox1
如果Len(X)< 4然后
Y = X
Do
Y =0& Y
循环直到Len(Y)> = 4
X = Y
结束If
End Sub


解决方案

你有几个问题。有关详细信息,请参阅代码中的注释

  Sub TB1()
Dim X As TextBox
Dim Y As String

设置X = Me.TextBoxes(Textbox 1)
'您需要有某种引用才能进入文本框。
'我在这种情况下是我在Sheet1模块中测试的工作表对象。
'它有一个文本框的集合,您可以通过名称引用。单击excel中的文本框,查看左上角的名称。
'对于

,必须使用`Set`语法。如果Len(X.Text)< 4然后
Y = X.Text
'必须显式引用文本框中的文本,因为它有其他属性可以改变像高度和宽度
Do
Y = 0& Y
循环直到Len(Y)> = 4
X.Text = Y'显式地引用文本框文本重新分配
End If
End Sub


I get error when I try to assign TextBox1 to variable X.

Sub TB1()
   Dim X As TextBox
   Dim Y As String
   X = TextBox1
   If Len(X) < 4 Then
     Y = X
     Do
        Y = "0" & Y
     Loop Until Len(Y) >= 4
     X = Y
   End If
End Sub

解决方案

You've got a few issues. See comments for details in the code

Sub TB1()
   Dim X As TextBox
   Dim Y As String

   Set X = Me.TextBoxes("Textbox 1")   
   'You need to have some sort of reference to get to the textbox.  
   'Me in this case is a worksheet object I tested in the Sheet1 module.  
   'It has a collection of textboxes which you can refer to by name.  Click on your textbox in excel to see the name in the upper left corner.
   'The `Set` syntax is necessary for objects 

   If Len(X.Text) < 4 Then
     Y = X.Text 
     'Have to explicitly refer to the text in the textbox since it has other properties you can change like height and width
     Do
        Y = "0" & Y
     Loop Until Len(Y) >= 4
     X.Text = Y 'Explicitly refer to the textbox text again to reassign
   End If
End Sub

这篇关于VBA - Excel TextBox1将对象分配给可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆