VBA/宏代码获取文本框的值 [英] VBA/Macro code to get the value of textbox
问题描述
Sub CopyRandomRows()
Windows("sample rnd.xlsm").Activate
Rows("1:1").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Windows("rnd sample draft.xlsm").Activate
Sheets("Random Sample").Select
Rows("1:1").Select
ActiveSheet.Paste
Dim source As Range, target As Range, randCount&, data(), value, r&, rr&, c&
Set source = Workbooks("sample rnd.xlsm").Worksheets("Sheet1").Range("A2:L5215")
Set target = Workbooks("rnd sample draft.xlsm").Worksheets("Random Sample").Range("A2")
randCount = 5
data = source.value
For r = 1 To randCount
rr = 1 + Math.Round(VBA.rnd * (UBound(data) - 1))
For c = 1 To UBound(data, 2)
value = data(r, c)
data(r, c) = data(rr, c)
data(rr, c) = value
Next
Next
target.Resize(randCount, UBound(data, 2)) = data
End Sub
这是我的代码.我的问题是,如果我更改代码randCount = 5
,则只能更改所需的数据数量.我希望能够使用我的TextBox并将其用于定义要获取的数据量.我尝试了randCound = TextBox1.value
和randCount = TextBox1.Text
,但是似乎没有用.我想念什么?我该如何工作.预先感谢
This is my code. My problem is that I can only change the number of data i want if i change the code randCount = 5
. I want to be able to use my TextBox and use it for defining how many data to get. I tried randCound = TextBox1.value
and randCount = TextBox1.Text
but does not seem to work. What am i missing? How do i get it work. thanks in advance
推荐答案
如果TextBox1位于名为Main的工作表上,则可以使用以下方法:
If TextBox1 is on a sheet named Main, then you can use this:
Worksheets("Main").TextBox1.Value
更好的是,您可以给工作表一个类似shtMain
之类的CodeName
,然后使用
Better yet, you can give the sheet a CodeName
of something like shtMain
and then use
shtMain.TextBox1.Value
最后,您还可以通过shapes集合进入文本框(但最好使用上述方法)...
And finally, you can also get to the textbox through the shapes collection (but the methods above are preferable)...
Worksheets("Main").Shapes("TextBox1").OLEFormat.Object.Object.Value
这篇关于VBA/宏代码获取文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!