Mọi người giúp em về sự kiện WORKSHEET_CHANGE với ạ!!! (1 người xem)

Liên hệ QC

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

khanhhero

Thành viên hoạt động
Tham gia
28/7/11
Bài viết
144
Được thích
36
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
      On Error GoTo out
Application.ScreenUpdating = False
Application.EnableEvents = False
If Target.Cells = [A1] Then
For i = 3 To 38
If Target.Cells.Value = "*" Or Target.Cells.Value > 12 Then
   Cells(3, i).EntireColumn.Hidden = False
   Target.Cells.Value = "*"
End If
     If Target.Cells.Value <= 12 Then
          If Cells(3, i) = Target.Cells.Value Or Cells(3, i) = Target.Cells.Value - 1 Then
             Cells(3, i).EntireColumn.Hidden = False
                Else: Cells(3, i).EntireColumn.Hidden = True
          End If
      End If
Next i
End If
out: Exit Sub
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Em có viết code như trên để hide những tháng khác với yêu cầu, nhưng khi e copy hay xử lý dữ liệu với 1 range là nó exit sub luôn và ko return lại được nữa, mọi người có cách nào xử lý vấn đề này ko ạ
 

File đính kèm

Bạn bỏ chữ exit sub đi xem được không.
 
Upvote 0
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
      On Error GoTo out
Application.ScreenUpdating = False
Application.EnableEvents = False
If Target.Cells = [A1] Then
For i = 3 To 38
If Target.Cells.Value = "*" Or Target.Cells.Value > 12 Then
   Cells(3, i).EntireColumn.Hidden = False
   Target.Cells.Value = "*"
End If
     If Target.Cells.Value <= 12 Then
          If Cells(3, i) = Target.Cells.Value Or Cells(3, i) = Target.Cells.Value - 1 Then
             Cells(3, i).EntireColumn.Hidden = False
                Else: Cells(3, i).EntireColumn.Hidden = True
          End If
      End If
Next i
End If
out: Exit Sub
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Em có viết code như trên để hide những tháng khác với yêu cầu, nhưng khi e copy hay xử lý dữ liệu với 1 range là nó exit sub luôn và ko return lại được nữa, mọi người có cách nào xử lý vấn đề này ko ạ
Bạn thêm một cái if nữa
If Target.Count = 1 Then
If Target.Cells = [A1] Then
 
Upvote 0
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
      On Error GoTo out
Application.ScreenUpdating = False
Application.EnableEvents = False
If Target.Cells = [A1] Then
For i = 3 To 38
If Target.Cells.Value = "*" Or Target.Cells.Value > 12 Then
   Cells(3, i).EntireColumn.Hidden = False
   Target.Cells.Value = "*"
End If
     If Target.Cells.Value <= 12 Then
          If Cells(3, i) = Target.Cells.Value Or Cells(3, i) = Target.Cells.Value - 1 Then
             Cells(3, i).EntireColumn.Hidden = False
                Else: Cells(3, i).EntireColumn.Hidden = True
          End If
      End If
Next i
End If
out: Exit Sub
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Em có viết code như trên để hide những tháng khác với yêu cầu, nhưng khi e copy hay xử lý dữ liệu với 1 range là nó exit sub luôn và ko return lại được nữa, mọi người có cách nào xử lý vấn đề này ko ạ
Bạn sửa code như sau:
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Out
    If Target.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target.Cells = [A1] Then
    For i = 3 To 38
    If Target.Cells.Value = "*" Or Target.Cells.Value > 12 Then
       Cells(3, i).EntireColumn.Hidden = False
       Target.Cells.Value = "*"
    End If
         If Target.Cells.Value <= 12 Then
              If Cells(3, i) = Target.Cells.Value Or Cells(3, i) = Target.Cells.Value - 1 Then
                 Cells(3, i).EntireColumn.Hidden = False
                    Else: Cells(3, i).EntireColumn.Hidden = True
              End If
          End If
    Next i
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
Out:
End Sub
- Câu lệnh If Target.Count > 1 Then Exit Sub nhằm mục đích khắc phục trường hợp làm việc với 1 range.
- Nhãn Out đưa ra sau 2 câu lệnh Application.... để nó bật 2 chức năng này trước khi dừng code, khắc phục hiện tượng "ko return lại được nữa".
 
Upvote 0
Bạn sửa code như sau:
Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Out
    If Target.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Target.Cells = [A1] Then
    For i = 3 To 38
    If Target.Cells.Value = "*" Or Target.Cells.Value > 12 Then
       Cells(3, i).EntireColumn.Hidden = False
       Target.Cells.Value = "*"
    End If
         If Target.Cells.Value <= 12 Then
              If Cells(3, i) = Target.Cells.Value Or Cells(3, i) = Target.Cells.Value - 1 Then
                 Cells(3, i).EntireColumn.Hidden = False
                    Else: Cells(3, i).EntireColumn.Hidden = True
              End If
          End If
    Next i
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
Out:
End Sub
- Câu lệnh If Target.Count > 1 Then Exit Sub nhằm mục đích khắc phục trường hợp làm việc với 1 range.
- Nhãn Out đưa ra sau 2 câu lệnh Application.... để nó bật 2 chức năng này trước khi dừng code, khắc phục hiện tượng "ko return lại được nữa".

Em cảm ơn anh, code chạy rất good ạ, cám ơn mọi người rất nhiều :)
 
Upvote 0
Web KT

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

Back
Top Bottom