Ẩn TextBox và ComboBox

Liên hệ QC

NguyenthiH

Thành viên mới đăng ký
Tham gia
11/12/16
Bài viết
929
Được thích
168
Giới tính
Nữ
Chào các anh chị.
Em có tạo 1 TextBox và 1 Combobox trên sheet (Active X Control). KHi em gõ chữ vào TexBox thì nạp dữ liệu tím được vào ComboBox, và khi Enter ỡ ComboBox thì lọc dữ liệu vào Sheet, em muốn khi đã nạp dữ liệu vào Sheet thì TextBox và ComboBox ẩn đi , dạng Visible = false, nhưng thấy TextBox và CoboBox không ẩn. Mong các anh chị chỉ giáo.
 
Chào các anh chị.
Em có tạo 1 TextBox và 1 Combobox trên sheet (Active X Control). KHi em gõ chữ vào TexBox thì nạp dữ liệu tím được vào ComboBox, và khi Enter ỡ ComboBox thì lọc dữ liệu vào Sheet, em muốn khi đã nạp dữ liệu vào Sheet thì TextBox và ComboBox ẩn đi , dạng Visible = false, nhưng thấy TextBox và CoboBox không ẩn. Mong các anh chị chỉ giáo.
Đưa code, file lên để xem lệnh Visible = False nằm ở đâu mới biết được.
 
Upvote 0
À, em thấy nó ẩn rồi, nhưng phải có chọn sheet thì nò mới ẩn.
Mã:
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim FindStr As String
 Application.ScreenUpdating = False
 Application.EnableEvents = False
 If KeyCode = 9 Then
 TextBox1 = Empty
 TextBox1.Activate
ComboBox1 = Empty
 Exit Sub
 End If
If KeyCode <> 13 Then Exit Sub
   Dim arr
   FindStr = UCase(TextBox1.Text) & "*"
   arr = Filter2DArray(aDuLieu, 1, FindStr, False)
  If FindStr = Empty Then Sheet6.ComboBox1.Clear
   If IsArray(arr) Then
      Sheet6.Range("C3").Value = Sheet6.ComboBox1.List(, 0)
      Sheet6.Range("F3").Value = Sheet6.ComboBox1.List(, 1)
   Else
     Sheet6.Range("C3").Value = Empty
     Sheet6.Range("F3").Value = Empty
   End If
   Call TrichLocTheKho
   Sheet6.TextBox1.Visible = False
   Sheet6.ComboBox1.Visible = False
   Sheet6.Range("B2").Select'.....-------------phải có chổ này mới ẩn
Application.ScreenUpdating = False
Application.EnableEvents = True
End Sub
 
Upvote 0
À, em thấy nó ẩn rồi, nhưng phải có chọn sheet thì nò mới ẩn.
Mã:
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim FindStr As String
Application.ScreenUpdating = False
Application.EnableEvents = False
If KeyCode = 9 Then
TextBox1 = Empty
TextBox1.Activate
ComboBox1 = Empty
Exit Sub
End If
If KeyCode <> 13 Then Exit Sub
   Dim arr
   FindStr = UCase(TextBox1.Text) & "*"
   arr = Filter2DArray(aDuLieu, 1, FindStr, False)
  If FindStr = Empty Then Sheet6.ComboBox1.Clear
   If IsArray(arr) Then
      Sheet6.Range("C3").Value = Sheet6.ComboBox1.List(, 0)
      Sheet6.Range("F3").Value = Sheet6.ComboBox1.List(, 1)
   Else
     Sheet6.Range("C3").Value = Empty
     Sheet6.Range("F3").Value = Empty
   End If
   Call TrichLocTheKho
   Sheet6.TextBox1.Visible = False
   Sheet6.ComboBox1.Visible = False
   Sheet6.Range("B2").Select'.....-------------phải có chổ này mới ẩn
Application.ScreenUpdating = False
Application.EnableEvents = True
End Sub
chưa cập nhật lại màn hình bạn
 
Upvote 0
Sao em thêm đoạn code này để hiện lại TextBox1, thì TextBox1 có hiện rồi mà SelStart với Sellength thì không có.
Mong các anh chị xem giúp.
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheet6.Range("C2").Select Then
TextBox1.Visible = True
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End If
End Sub
 
Upvote 0
TextBox1 chỉ được hiển thị chứ có Activate đâu mà nhìn thấy.
Ngoài ra nếu tôi hiểu được lôgíc thì phải là, ngược lại thì không bao giờ chọn được ô khác C2 để nhập liệu.
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count = 1 And Target.Address = "$C$2" Then
        TextBox1.Visible = True
        TextBox1.Activate
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(TextBox1.Text)
    End If
End Sub
 
Upvote 0
Dạ em cám ơn Thầy @batman1 , Thầy nói em mới để ý thấy code em cứ click chuột đi đâu nó cứ nhảy vào cell C2 không à.
 
Upvote 0
À Thầy @batman1 ơi, code của Thầy khi em click vào góc trái phía trên của bảng tính để chọn toàn bàng tính thì có lỗi số 6 - overflow.
 
Upvote 0
À Thầy @batman1 ơi, code của Thầy khi em click vào góc trái phía trên của bảng tính để chọn toàn bàng tính thì có lỗi số 6 - overflow.
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Rows.Count = Rows.Count Then Exit Sub
    If Target.Count = 1 And Target.Address = "$C$2" Then
        TextBox1.Visible = True
        TextBox1.Activate
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(TextBox1.Text)
    End If
End Sub
hoặc tốt nhất là
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo end_
    If Target.Count = 1 And Target.Address = "$C$2" Then
        TextBox1.Visible = True
        TextBox1.Activate
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(TextBox1.Text)
    End If
end_:
End Sub
 
Upvote 0
Web KT
Back
Top Bottom