excel vba 如何在Moduled裡面寫UserForm的控制項?

2017-02-16 1:09 pm
我的UserForm有
TextBox1 ,TextBox2 ~TextBox10
我想把程式寫在Moduled裡執行
如下:
.Cells(1, "A") = TextBox1.Text
.Cells(1, "B") = TextBox2.Text
.
.
.
.
.Cells(1, "J") = TextBox10.Text
請問各位我該怎麼寫

回答 (3)

2017-02-17 5:28 am
增加一個按鈕

Private Sub CommandButton1_Click()
Call txx
End Sub

Sub txx()
With UserForm1
For i = 1 To 10
Cells(1, i) = .Controls("TextBox" & i).Text
Next
End With
End Sub


Sub txx()
Dim ta As Long
Dim tb As Long
ta = UserForm1.TextBox2.Text
tb = UserForm1.TextBox3.Text
UserForm1.TextBox1 = (ta + tb) * 3
End Sub
2017-02-16 11:45 pm
這是一個例子
因為我寫的UserForm控制項元件很多,不只有TextBox
目前完成只寫1/10程式已經200多行了,如果全部寫完會太肥
所以我想把一寫運算式個別寫在一些Moduled裡
但是Moduled裡面無法做類是像:
TextBox1=(TextBox2 + TextBox3) * 3
...
這樣的運算,TextBox 不在Moduled裡
2017-02-16 4:13 pm
但係點解要用module呢?其實Sheet1 form都得。

' VBA: Insert | Module
Sub btnExecute()
UserForm1.Show
End Sub

' Forms | UserForm1
Private Sub btnSave_Click()
Dim wkb As Workbook
Dim wks As Worksheet

Set wkb = ActiveWorkbook
Set wks = wkb.ActiveSheet
wks.Cells(1, "a") = Me.TextBox1.Text
' ...
End Sub

' Developer | Insert | Form Controls | Button (Form Control)
' Right-click new added button | Assign Macro | Select the macro name: btnExecute


收錄日期: 2021-05-03 13:52:13
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20170216050930AApseYE

檢視 Wayback Machine 備份