Viết hàm lặp cho form trong VBA

Liên hệ QC

chidung2009

Thành viên hoạt động
Tham gia
12/9/12
Bài viết
123
Được thích
8
Mình có một form chứa rất nhiều checkbox.
Khi bấm chọn checkBox = True và lưu thì ghi lên Sheet từ A4 đến F4
Mỗi khi mở form thì sẽ load dữ liệu lên form CheckBox đã chọn hay chưa.
Mình đã cố gắng viết hàm lặp nhưng đều thất bại, mong ACE giúp mình với

Mình xin trân trọng cảm ơn


Mã:
Private Sub cbSave_Click()

'Dim i As Integer
'Dim j As Integer
'For i = 1 To 5  'Biên de chay CheckBox1
'For j = 1 To 6  ' Bien de chay cot
'If "CheckBox" & i.Value = True Then
'    Rows(j, 4) = True
'Else: Rows(j, 4) = False
'Next j
'Next i

'---------------

If CheckBox1.Value = True Then
     Range("A4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("A4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox2.Value = True Then
     Range("B4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("B4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox3.Value = True Then
     Range("C4").Select
     ActiveCell.FormulaR1C1 = True
Else
    Range("C4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox4.Value = True Then
    Range("D4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("D4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox5.Value = True Then
     Range("E4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("E4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox6.Value = True Then
     Range("F4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("F4").Select
     ActiveCell.FormulaR1C1 = False
End If
Unload Me

End Sub
 

File đính kèm

  • CheckBox.xlsm
    23.9 KB · Đọc: 6
Lần chỉnh sửa cuối:
Mình có một form chứa rất nhiều checkbox.
Khi bấm chọn checkBox = True và lưu thì ghi lên Sheet từ A4 đến F4
Mỗi khi mở form thì sẽ load dữ liệu lên form CheckBox đã chọn hay chưa.
Mình đã cố gắng viết hàm lặp nhưng đều thất bại, mong ACE giúp mình với

Mình xin trân trọng cảm ơn


Mã:
Private Sub cbSave_Click()

'Dim i As Integer
'Dim j As Integer
'For i = 1 To 5  'Biên de chay CheckBox1
'For j = 1 To 6  ' Bien de chay cot
'If "CheckBox" & i.Value = True Then
'    Rows(j, 4) = True
'Else: Rows(j, 4) = False
'Next j
'Next i

'---------------

If CheckBox1.Value = True Then
     Range("A4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("A4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox2.Value = True Then
     Range("B4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("B4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox3.Value = True Then
     Range("C4").Select
     ActiveCell.FormulaR1C1 = True
Else
    Range("C4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox4.Value = True Then
    Range("D4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("D4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox5.Value = True Then
     Range("E4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("E4").Select
     ActiveCell.FormulaR1C1 = False
End If

If CheckBox6.Value = True Then
     Range("F4").Select
     ActiveCell.FormulaR1C1 = True
Else
     Range("F4").Select
     ActiveCell.FormulaR1C1 = False
End If
Unload Me

End Sub
Tôi nghĩ ít nhất bạn cũng viết được code của sự kiện này chứ:
Mã:
Private Sub UserForm_Activate()
  Dim n As Long
  For n = 1 To 6
    Me.Controls("CheckBox" & n).Value = Cells(4, n).Value
  Next
End Sub
Sự kiện cbSave_Click bạn làm tương tự nhé
 
Upvote 0
...........................................
 
Upvote 0
Tôi nghĩ ít nhất bạn cũng viết được code của sự kiện này chứ:
Mã:
Private Sub UserForm_Activate()
  Dim n As Long
  For n = 1 To 6
    Me.Controls("CheckBox" & n).Value = Cells(4, n).Value
  Next
End Sub
Sự kiện cbSave_Click bạn làm tương tự nhé
Mình cảm ơn bạn đã giúp đỡ
 
Upvote 0
Web KT
Back
Top Bottom