Code FORM bị lỗi ở đâu

Liên hệ QC

toandiennuoc123

Thành viên thường trực
Tham gia
7/3/12
Bài viết
239
Được thích
9
Nhờ các bạn xem hộ tôi code này bị sai ở đâu và sửa giúp nhé, xin cám ơn mọi người.
Private Sub CommandButton1_Click()
With Sheet1
If Me.Opt1 = True Then
.Range("A2") = Me.TextBox1.Value
.Range("A3") = Me.TextBox1.Value
.Range("A4") = Me.TextBox1.Value
.Range("A5") = Me.TextBox1.Value
ElseIf Me.Opt2 = True Then
.Range("B4") = Me.TextBox1.Value
.Range("B5") = Me.TextBox1.Value
.Range("B6") = Me.TextBox1.Value
.Range("B7") = Me.TextBox1.Value
ElseIf Me.Opt3 = True Then
.Range("C6") = Me.TextBox1.Value
.Range("C7") = Me.TextBox1.Value
.Range("C8") = Me.TextBox1.Value
.Range("C9") = Me.TextBox1.Value
If Me.Opt4 = True Then
.Range("D6:D9") = Me.TextBox1.Value
End If
Me.TextBox1 = ""
Me.TextBox1.SetFocus
End With
End Sub
 
Nhờ các bạn xem hộ tôi code này bị sai ở đâu và sửa giúp nhé, xin cám ơn mọi người.
Private Sub CommandButton1_Click()
With Sheet1
If Me.Opt1 = True Then
.Range("A2") = Me.TextBox1.Value
.Range("A3") = Me.TextBox1.Value
.Range("A4") = Me.TextBox1.Value
.Range("A5") = Me.TextBox1.Value
ElseIf Me.Opt2 = True Then
.Range("B4") = Me.TextBox1.Value
.Range("B5") = Me.TextBox1.Value
.Range("B6") = Me.TextBox1.Value
.Range("B7") = Me.TextBox1.Value
ElseIf Me.Opt3 = True Then
.Range("C6") = Me.TextBox1.Value
.Range("C7") = Me.TextBox1.Value
.Range("C8") = Me.TextBox1.Value
.Range("C9") = Me.TextBox1.Value
If Me.Opt4 = True Then
.Range("D6:D9") = Me.TextBox1.Value
End If
Me.TextBox1 = ""
Me.TextBox1.SetFocus
End With
End Sub

Khi có lỗi thì Excel chắc chắn báo lỗi. Bạn không đọc nó thông báo gì à? Đọc không hiểu? Không tra từ điển? Không nhờ Google dịch? Tuy dịch không ra gì nhưng có còn hơn không.
Nếu về mặt "ngữ pháp thì sai:
If Me.Opt4 = True Then
.Range("D6:D9") = Me.TextBox1.Value
Hoặc sửa lại thành:
If Me.Opt4 = True Then
.Range("D6:D9") = Me.TextBox1.Value
END IF <--
Hoặc:
If Me.Opt4 = True Then .Range("D6:D9") = Me.TextBox1.Value
Đấy là về mặt ngữ pháp. Tuy code không có lỗi nhưng chắc không làm theo ý muốn của bạn. Tôi nghĩ là bạn muốn làm thế này (chắc là opt3 và opt4 bạn để cùng nhóm nên không chọn cùng lúc được):
With Sheet1
If Me.Opt1 = True Then
.Range("A2") = Me.TextBox1.Value
.Range("A3") = Me.TextBox1.Value
.Range("A4") = Me.TextBox1.Value
.Range("A5") = Me.TextBox1.Value
ElseIf Me.Opt2 = True Then
.Range("B4") = Me.TextBox1.Value
.Range("B5") = Me.TextBox1.Value
.Range("B6") = Me.TextBox1.Value
.Range("B7") = Me.TextBox1.Value
ElseIf Me.Opt3 = True Then
.Range("C6") = Me.TextBox1.Value
.Range("C7") = Me.TextBox1.Value
.Range("C8") = Me.TextBox1.Value
.Range("C9") = Me.TextBox1.Value
ELSEIf Me.Opt4 = True Then
.Range("D6:D9") = Me.TextBox1.Value
End If
Me.TextBox1 = ""
Me.TextBox1.SetFocus
End With
 
Upvote 0
Nhờ các bạn xem hộ tôi code này bị sai ở đâu và sửa giúp nhé, xin cám ơn mọi người.
Sai ở chổ bạn đặt mấy cái Opt không đúng theo code. Bạn sửa và đặt tên cho nó đúng theo code là được. Riêng file bạn dùng phím Tab và ấn nút Enter được rồi đây.
 
Upvote 0
Theo mình bạn để code khá dài khó quản lý nên để code ngắn hơn cho tiện:
PHP:
Private Sub COM1_Click()


    Dim Mystring As String, cell As String
    Dim j As Long, nRow As Long, i As Long

    Mystring = TextBox1.Value

    Select Case True
        Case Opt11: cell = "A": i = 2: j = 5
        Case Opt2: cell = "B": i = 4: j = 7
        Case Opt3: cell = "C": i = 6: j = 9
        Case OptionButton1: cell = "D": i = 6: j = 9
        Case OptionButton2: cell = "E": i = 6: j = 9
        Case OptionButton3: cell = "F": i = 6: j = 9
        Case OptionButton4: cell = "G": i = 6: j = 9
        Case OptionButton5: cell = "H": i = 6: j = 9
        Case OptionButton6: cell = "J": i = 6: j = 9
        Case OptionButton7: cell = "K": i = 6: j = 9
        Case OptionButton6: cell = "L": i = 6: j = 9
        Case Else: Exit Sub
    End Select
 
    For nRow = i To j

        Sheet1.Range(cell & nRow) = Mystring
 
    Next

    TextBox1 = ""
    TextBox1.SetFocus

End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Cám ơn bạn Phanngoclan rất nhiều, cho tôi hỏi thêm chút nữa: nếu giá trị của Textbox1 được nhập vào vị trí của Sheet khác nhưng cùng vị trí cell thì làm thế nào ??? "Case OptionButton1: Sheet2! = "D": i = 6: j = 9" .
 
Upvote 0
Cám ơn bạn Phanngoclan rất nhiều, cho tôi hỏi thêm chút nữa: nếu giá trị của Textbox1 được nhập vào vị trí của Sheet khác nhưng cùng vị trí cell thì làm thế nào ??? "Case OptionButton1: Sheet2! = "D": i = 6: j = 9" .
Khi đó bạn nên khai báo thêm biến MySheet As Worksheet
PHP:
Private Sub COM1_Click()

    Dim MySheet As Worksheet, NameSheet As String
    Dim Mystring As String, cell As String
    Dim j As Long, nRow As Long, i As Long

    Mystring = TextBox1.Value

    Select Case True
        Case Opt11: cell = "A": i = 2: j = 5: NameSheet = "Sheet1"
        Case Opt2: cell = "B": i = 4: j = 7: NameSheet = "Sheet2"
        Case Opt3: cell = "C": i = 6: j = 9: NameSheet = "Sheet3"
        Case OptionButton1: cell = "D": i = 6: j = 9: NameSheet = "Sheet4"
        Case OptionButton2: cell = "E": i = 6: j = 9: NameSheet = "Sheet5"
        Case OptionButton3: cell = "F": i = 6: j = 9: NameSheet = "Sheet6"
        Case OptionButton4: cell = "G": i = 6: j = 9: NameSheet = "Sheet7"
        Case OptionButton5: cell = "H": i = 6: j = 9: NameSheet = "Sheet8"
        Case OptionButton6: cell = "J": i = 6: j = 9: NameSheet = "Sheet9"
        Case OptionButton7: cell = "K": i = 6: j = 9: NameSheet = "Sheet10"
        Case OptionButton6: cell = "L": i = 6: j = 9: NameSheet = "Sheet11"
        Case Else: Exit Sub
    End Select
    
    Set MySheet = ThisWorkbook.Sheets(NameSheet)
    
    For nRow = i To j

        MySheet.Range(cell & nRow) = Mystring
 
    Next
    
    Set MySheet = Nothing

    TextBox1 = ""
    TextBox1.SetFocus

End Sub
 
Upvote 0
Web KT
Back
Top Bottom