返回列表 上一主題 發帖

[發問] VB程式碼寫法簡化 - 指定範圍輸入

[發問] VB程式碼寫法簡化 - 指定範圍輸入

各位大大們好!
目前正以VB userform 來輸入指定範圍的數值,想請教大大們以下程式碼是否能加以簡化呢?
對VB還不是很熟,還請大大們教教我!
感謝大家!

程式碼:
Private Sub CommandButton2_Click()
Range("B11").Value = Val(TextBox1)
Range("C11").Value = Val(TextBox7)
Range("D11").Value = Val(TextBox13)
Range("E11").Value = Val(TextBox19)
Range("B12").Value = Val(TextBox2)
Range("C12").Value = Val(TextBox8)
Range("D12").Value = Val(TextBox14)
Range("E12").Value = Val(TextBox20)
Range("B13").Value = Val(TextBox3)
Range("C13").Value = Val(TextBox9)
Range("D13").Value = Val(TextBox15)
Range("E13").Value = Val(TextBox21)
Range("B14").Value = Val(TextBox25)
Range("C14").Value = Val(TextBox26)
Range("D14").Value = Val(TextBox27)
Range("E14").Value = Val(TextBox28)
Range("B15").Value = Val(TextBox4)
Range("C15").Value = Val(TextBox10)
Range("D15").Value = Val(TextBox16)
Range("E15").Value = Val(TextBox22)
Range("B16").Value = Val(TextBox5)
Range("C16").Value = Val(TextBox11)
Range("D16").Value = Val(TextBox17)
Range("E16").Value = Val(TextBox23)
Range("B17").Value = Val(TextBox6)
Range("C17").Value = Val(TextBox12)
Range("D17").Value = Val(TextBox18)
Range("E17").Value = Val(TextBox24)
Unload Me
End Sub

回復 2# Hsieh

Hsieh超級版主大大真得很感謝你!
但對於以下這三行我不是很了解
抱歉因為是新手想多學一點,所以問題比較多
不知道能否稍微和我解釋一下
感謝!
For i = 0 To UBound(ay)
r = Int(i / 4): c = i Mod 4
  [B11].Offset(r, c) = Controls("TextBox" & ay(i)).Value

TOP

        靜思自在 : 待人退一步,愛人寬一寸,就會活得很快樂。
返回列表 上一主題