Form thay đổi khi ShowModal = False (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

ThaiDieuAnh

Thành viên hoạt động
Tham gia
8/8/16
Bài viết
139
Được thích
24
Nghề nghiệp
Xây dựng
Em có Form như file đính kèm. Em muốn sau khi Propeties ShowModal = False và di chuyển cell trên cột B thì giá trị trong Textbox1 sẽ thay đổi theo ô đó, mong các anh chị giúp đỡ, em xin cảm ơn!
 

File đính kèm

Em có Form như file đính kèm. Em muốn sau khi Propeties ShowModal = False và di chuyển cell trên cột B thì giá trị trong Textbox1 sẽ thay đổi theo ô đó, mong các anh chị giúp đỡ, em xin cảm ơn!
Chèn thêm 1 module với code:
Mã:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Function IsFormLoaded(ByVal frmCaption As String) As Boolean
  IsFormLoaded = FindWindow("ThunderDFrame", frmCaption) > 0
End Function
Sửa code sự kiện BeforeRightClick trong file thành:
Mã:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If IsFormLoaded("UserForm1") Then
    If Not Intersect(Range("B4:B" & [B65500].End(xlUp).Row), Target) Is Nothing Then
      If Target.Count = 1 Then
        If IsDate(Target.Value)  Then
          UserForm1.TextBox1 = Target.Value
          Cancel = True
        End If
      End If
    End If
  End If
End Sub
 

File đính kèm

Upvote 0
Chèn thêm 1 module với code:
Mã:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Function IsFormLoaded(ByVal frmCaption As String) As Boolean
  IsFormLoaded = FindWindow("ThunderDFrame", frmCaption) > 0
End Function
Sửa code sự kiện BeforeRightClick trong file thành:
Mã:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If IsFormLoaded("UserForm1") Then
    If Not Intersect(Range("B4:B" & [B65500].End(xlUp).Row), Target) Is Nothing Then
      If Target.Count = 1 Then
        If IsDate(Target.Value)  Then
          UserForm1.TextBox1 = Target.Value
          Cancel = True
        End If
      End If
    End If
  End If
End Sub
Mã:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Chỗ này không được anh ạ, dù em đã bỏ PtrSafe, em dùng Officel 2007
Nếu mở bằng excel 2016 thì được anh ạ, nhưng khi di chuyển trên cells thì Textbox vẫn không nhận giá trị của cell
 
Lần chỉnh sửa cuối:
Upvote 0
Mã:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Chỗ này không được anh ạ, dù em đã bỏ PtrSafe, em dùng Officel 2007
Giờ mà vẫn còn có người xài 2007 sao ta?
Thôi thì dùng cái cũ:
Mã:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
Upvote 0
Giờ mà vẫn còn có người xài 2007 sao ta?
Thôi thì dùng cái cũ:
Mã:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Khi di chuyển trên cells thì Textbox không nhận giá trị của cell và sự kiện BeforeRightClick không chạy anh ạ!
 
Lần chỉnh sửa cuối:
Upvote 0
Ah! Hay là bạn muốn click phải chuột thì form tự show và điền giá trị?
Nếu là thế thì sửa code sự kiện thành vầy:
Mã:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Range("B4:B" & [B65500].End(xlUp).Row), Target) Is Nothing Then
    If Target.Count = 1 Then
      If IsDate(Target.Value) = True Then
        If IsFormLoaded("UserForm1") = False Then UserForm1.Show
        UserForm1.TextBox1 = Target.Value
        Cancel = True
      End If
    End If
  End If
End Sub
 
Upvote 0
Ah! Hay là bạn muốn click phải chuột thì form tự show và điền giá trị?
Nếu là thế thì sửa code sự kiện thành vầy:
Mã:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Range("B4:B" & [B65500].End(xlUp).Row), Target) Is Nothing Then
    If Target.Count = 1 Then
      If IsDate(Target.Value) = True Then
        If IsFormLoaded("UserForm1") = False Then UserForm1.Show
        UserForm1.TextBox1 = Target.Value
        Cancel = True
      End If
    End If
  End If
End Sub
Vâng, cảm ơn anh nhiều ạ, chúc anh sức khỏe :):):)
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom