excel儲存格連續讀取textbox所輸入的字

2015-07-18 10:38 pm
Private Sub CommandButton1_Click()

For i = 1 To 13

Sheet3.Cells(i, 1) = TextBox(i).Value

Next

End Sub

問題1
假設有 i 個儲存格A i
textbox i
要如何讓A i讀textbox i 的值依此類推

問題2
若textbox輸入的值為個位數(1), 如何讀取時補0,變為01,
是要真的01(二個字元),而不是看起來是01

回答 (3)

2015-07-19 6:18 am
✔ 最佳答案
Private Sub CommandButton1_Click()
Dim TBarray(1 To 13) As Control
For i = 1 To 13
Set TBarray(i) = Controls("TextBox" & i)
TBarray(i) = Application.Text(TBarray(i).Text, "00")

2015-07-18 15:14:48 補充:
Sheets("Sheet3").Cells(i, 1).NumberFormatLocal = "@"
Sheets("Sheet3").Cells(i, 1) = TBarray(i).Text
Next i
End Sub
將上面2段組合試試

2015-07-18 22:18:55 補充:
Private Sub CommandButton1_Click()
Dim TBarray(1 To 13) As Control
For i = 1 To 13
Set TBarray(i) = Controls("TextBox" & i)
TBarray(i) = Application.Text(TBarray(i).Text, "00")
Sheets("Sheet3").Cells(i, 1).NumberFormatLocal = "@"
Sheets("Sheet3").Cells(i, 1) = TBarray(i).Text
Next i
End Sub
試試補充說明:
1.因為版大的TextBox控制項共13個,為省去一個一個的程式設定,故以陣列的方式來達成
2.Dim TBarray(1 To 13) As Control ->定義一個共有13個控制項的陣列
3.Set TBarray(i) = Controls("TextBox" & i) ->把陣列指定為TextBox控制項
4.TBarray(i) = Application.Text(TBarray(i).Text, "00") ->把TextBox控制項的值轉為2位數
5.Sheets("Sheet3").Cells(i, 1).NumberFormatLocal = "@" ->因TextBox控制項的值複製到Sheet3的儲存格時會是數值各式,透過這行把它轉為文字格式
2015-07-19 12:54 am
For i = 1 To 13
With Sheet3.Cells(i, 1)
  .NumberFormatLocal = "@"
  .Value = Format(OLEObjects("TextBox" & i).Object.Text, "00")
End With
Next

參考即可~~

2015-07-18 17:08:47 補充:
如果是使用Form輸入,表單名為Form1
Format(Form1("TextBox" & i).Text, "00")
2015-07-18 11:45 pm
水哦~~~
可否煩請C大稍微解說一下, 下面各行的意思, 後請移駕回答區, 以便答贈, 謝謝~~

Dim TBarray(1 To 13) As Control

Set TBarray(i) = Controls("TextBox" & i)
TBarray(i) = Application.Text(TBarray(i).Text, "00")

Sheets("Sheet3").Cells(i, 1).NumberFormatLocal = "@"


收錄日期: 2021-04-23 23:52:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20150718000016KK03692

檢視 Wayback Machine 備份