Private Sub txtPrice1_Change() txtAmount1.Value = Val(txtQuantity1.Value) * Val(txtPrice1.Value) End Sub
Tương tự với txtPrice2_Change và txtPrice3_Change
Vậy làm thế nào để tự động thêm txtAmount2, txtAmount3, txtQuantity2, txtQuantity3, … và không phải code lặp đi lặp lại như vậy?
Chúng ta có thể xem thêm tại Nhập dữ liệu với Controls động trong Userform bằng VBA
Private Sub cmdSubmit_Click() Dim intHeader As Integer Dim intIDRows As Integer Dim intID As Integer Dim intNoRows As Integer intHeader = 8 intIDColumns = 2 If Len([tblOrder].Rows(1).Columns(1).Value) = 0 Then intID = 1 Else intNoRows = [tblOrder].Rows().Count intID = [tblOrder].Rows(intNoRows).Columns(1).Value + 1 End If For intI = 1 To 3 Step 1 Cells(intHeader + intNoRows + intI, intIDColumns).Value = intID Cells(intHeader + intNoRows + intI, intIDColumns + 1).Value = txtCustomer.Value Cells(intHeader + intNoRows + intI, intIDColumns + 2).Value = txtMobile.Value Cells(intHeader + intNoRows + intI, intIDColumns + 3).Value = txtEmail.Value Cells(intHeader + intNoRows + intI, intIDColumns + 4).Value = Controls("txtQuantity" & intI).Value Cells(intHeader + intNoRows + intI, intIDColumns + 5).Value = Controls("txtPrice" & intI).Value Cells(intHeader + intNoRows + intI, intIDColumns + 6).Value = Controls("txtAmount" & intI).Value Next ' intI End Sub
Download file Input.xlsm
Đăng ký ngay Khóa học VBA Cơ bản đến Nâng cao để Ứng dụng hiệu quả Macro & VBA trong nâng cao hiệu xuất công việc