- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2013-9-13 16:05
| 只看該作者
本帖最後由 GBKEE 於 2013-9-13 16:25 編輯
回復 3# wxad0285
試試看- Option Explicit
- Sub firstrecord()
- Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
- Dim a1 As Integer, b1 As Integer, c1 As Integer, d1 As Integer, e1 As Integer, f1 As Integer, g1 As Integer, h1 As Integer, i1 As Integer
- Dim Sh As Worksheet, xRow As Integer, i As Integer
- Set Sh = Sheets("salary") '這裡設立工作表變數 '*** 如工作表要改名稱: 這裏改一次就可以 *****
- xRow = Application.CountA(Sh.Range("a:a")) '有資料儲存格的計數
- With Sh
- .Cells(xRow + 1, "A") = xRow - 1 '編號: 為有資料儲存格的計數-1
- For i = 1 To 12
- '將應支金額填入,並相對應excel的儲存格' 1-10
- If i <= 10 Then .Cells(xRow + 1, "A").Offset(, i) = UserForm1.Controls("Textbox" & i)
- '將應扣金額填入,並相對應excel的儲存格' 11-22
- .Cells(xRow + 1, "M").Offset(, i - 1) = UserForm1.Controls("Textbox" & i + 10)
- Next
- End With
- '將應支金額相加'
- With UserForm1
- '檢查程式碼卻總是出現"資料型態不正確"? ->'當控制項 為空白時 不可作四則運算
- a = Val(.TextBox3) ' a 也可以 = 工作表的儲存格 ,不必加 Val函數
- b = Val(.TextBox4) * Val(.TextBox5) '儲存格為空白時自動轉為 0 可作四則運算
- c = Val(.TextBox6) * Val(.TextBox7)
- d = Val(.TextBox8) * Val(.TextBox9)
- e = Val(.TextBox10)
- '顯示出應支金額小計'
- Sh.Range("L3").Value = a + b + c + d + e
- .Label19 = Sh.Range("L3")
- '顯示各項目小計'
- .Label35 = b
- .Label34 = c
- .Label13 = d
- '將應扣金額相加'
- a1 = Val(.TextBox11)
- b1 = Val(.TextBox12)
- c1 = Val(.TextBox13)
- d1 = Val(.TextBox14)
- e1 = Val(.TextBox15)
- f1 = Val(.TextBox22)
- g1 = Val(.TextBox16) * Val(.TextBox17)
- h1 = Val(.TextBox18) * Val(.TextBox19)
- i1 = Val(.TextBox20) * Val(.TextBox21)
- '顯示應扣金額小計'
- Sh.Range("Y3").Value = a1 + b1 + c1 + d1 + e1 + f1 + g1 + h1 + i1
- .Label42 = Sh.Range("Y3")
- '顯示實支金額'
- .Label16 = .Label19 - .Label42
- 'Sh.Range("Y3")-Sh.Range("L3") 儲存格為空白時自動轉為 0
- End With
- End Sub
複製代碼 |
|