Thắc mắc: Code VBA không tự động cập nhật (1 người xem)

  • Thread starter Thread starter lminhv
  • Ngày gửi Ngày gửi
Liên hệ QC

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

lminhv

Thành viên mới
Tham gia
8/1/11
Bài viết
7
Được thích
4
Xin chào cả nhà!
hiện tại em đang tập tành làm 1 đoạn code về ẩn hiện các shapes trong excel, nhưng em bị 1 vấn đề là các shapes không tự ẩn hiện khi em thay đổi nội dung trong ô, phải vào VBA bấm F5 lại thì nó mới cập nhật.

đây là đoạn code em dùng,
PHP:
Sub AnHien()
Sheets("Sheet1").Select
Range("b1").Select
If ActiveCell = 0 Then
Sheet1.Shapes("AutoShape 1").Visible = False
Sheet1.Shapes("AutoShape 2").Visible = False
ElseIf ActiveCell = 1 Then
Sheet1.Shapes("AutoShape 1").Visible = False
Sheet1.Shapes("AutoShape 2").Visible = True
ElseIf ActiveCell = 2 Then
Sheet1.Shapes("AutoShape 1").Visible = True
Sheet1.Shapes("AutoShape 2").Visible = True
End If

End Sub

em có tìm tòi trên diễn dàn về auto rerfresh, và thử thêm vào
PHP:
Private Sub Workbook_Sheet1(ByVal Sh As Object)
Sheet1.RefreshAll
End Sub

nhưng vẫn không ăn thua, nhờ các cao thủ giúp em hoàn chỉnh file này với.
em gửi theo file đính kèm,

cảm ơn các bác trước
 

File đính kèm

Cái này dùng thủ tục worksheet_Change() đi bạn. Câu lệnh Sheet1.Select bỏ đi. Câu lệnh range("B1").Select bạn thay bằng if Target.Address="$B$1" then, Câu lệnh if Activecell.Value=0 bạn thay bằng If Target.value=0.
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
Xin chào cả nhà!
hiện tại em đang tập tành làm 1 đoạn code về ẩn hiện các shapes trong excel, nhưng em bị 1 vấn đề là các shapes không tự ẩn hiện khi em thay đổi nội dung trong ô, phải vào VBA bấm F5 lại thì nó mới cập nhật.

đây là đoạn code em dùng,
PHP:
Sub AnHien()
Sheets("Sheet1").Select
Range("b1").Select
If ActiveCell = 0 Then
Sheet1.Shapes("AutoShape 1").Visible = False
Sheet1.Shapes("AutoShape 2").Visible = False
ElseIf ActiveCell = 1 Then
Sheet1.Shapes("AutoShape 1").Visible = False
Sheet1.Shapes("AutoShape 2").Visible = True
ElseIf ActiveCell = 2 Then
Sheet1.Shapes("AutoShape 1").Visible = True
Sheet1.Shapes("AutoShape 2").Visible = True
End If

End Sub

em có tìm tòi trên diễn dàn về auto rerfresh, và thử thêm vào
PHP:
Private Sub Workbook_Sheet1(ByVal Sh As Object)
Sheet1.RefreshAll
End Sub

nhưng vẫn không ăn thua, nhờ các cao thủ giúp em hoàn chỉnh file này với.
em gửi theo file đính kèm,

cảm ơn các bác trước

Viết vầy nha
PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$1" Then
   Sheet1.Shapes("AutoShape 1").Visible = 0
   Sheet1.Shapes("AutoShape 2").Visible = 0
   If [B1] <> 0 Then Sheet1.Shapes("AutoShape " & [B1]).Visible = True
End If
End Sub
 
Upvote 0
Cảm ơn anh vu_tuan_manh_linh và anh quanghai1969 nhiều lắm, tương tự như trên, nhưng có phần phức tạp hơn, nhờ các bác giúp em ẩn hiện Shape như file đính kèm với ạ,
hiện tại em làm thế này, không biết có cách nào hay và gọn hơn không nhỉ
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$d$14" Then

Shapes("Straight Connector 11017").Visible = 0
Shapes("Straight Connector 47").Visible = 0
Shapes("Straight Connector 11025").Visible = 0
Shapes("Straight Connector 11023").Visible = 0
Shapes("Straight Connector 11019").Visible = 0
Shapes("Straight Connector 11015").Visible = 0
Shapes("Straight Connector 11013").Visible = 0
Shapes("Freeform 7").Visible = 0
Shapes("Straight Connector 118").Visible = 0
Shapes("Straight Connector 119").Visible = 0
Shapes("Straight Connector 120").Visible = 0
Shapes("Straight Connector 121").Visible = 0
Shapes("Straight Connector 122").Visible = 0
Shapes("Straight Connector 123").Visible = 0
Shapes("Straight Connector 124").Visible = 0
Shapes("Freeform 117").Visible = 0

ElseIf [d14] = 0 Then
Shapes("Straight Connector 11017").Visible = False
Shapes("Straight Connector 47").Visible = False
Shapes("Straight Connector 11025").Visible = False
Shapes("Straight Connector 11023").Visible = False
Shapes("Straight Connector 11019").Visible = False
Shapes("Straight Connector 11015").Visible = False
Shapes("Straight Connector 11013").Visible = False
Shapes("Freeform 7").Visible = False
Shapes("Straight Connector 118").Visible = False
Shapes("Straight Connector 119").Visible = False
Shapes("Straight Connector 120").Visible = False
Shapes("Straight Connector 121").Visible = False
Shapes("Straight Connector 122").Visible = False
Shapes("Straight Connector 123").Visible = False
Shapes("Straight Connector 124").Visible = False
Shapes("Freeform 117").Visible = False

ElseIf [d14] = 1 Then
Shapes("Straight Connector 11017").Visible = False
Shapes("Straight Connector 47").Visible = False
Shapes("Straight Connector 11025").Visible = False
Shapes("Straight Connector 11023").Visible = False
Shapes("Straight Connector 11019").Visible = False
Shapes("Straight Connector 11015").Visible = False
Shapes("Straight Connector 11013").Visible = False
Shapes("Freeform 7").Visible = False
Shapes("Straight Connector 118").Visible = True
Shapes("Straight Connector 119").Visible = True
Shapes("Straight Connector 120").Visible = True
Shapes("Straight Connector 121").Visible = True
Shapes("Straight Connector 122").Visible = True
Shapes("Straight Connector 123").Visible = True
Shapes("Straight Connector 124").Visible = True
Shapes("Freeform 117").Visible = True

ElseIf [d14] = 2 Then
Shapes("Straight Connector 11017").Visible = True
Shapes("Straight Connector 47").Visible = True
Shapes("Straight Connector 11025").Visible = True
Shapes("Straight Connector 11023").Visible = True
Shapes("Straight Connector 11019").Visible = True
Shapes("Straight Connector 11015").Visible = True
Shapes("Straight Connector 11013").Visible = True
Shapes("Freeform 7").Visible = True
Shapes("Straight Connector 118").Visible = True
Shapes("Straight Connector 119").Visible = True
Shapes("Straight Connector 120").Visible = True
Shapes("Straight Connector 121").Visible = True
Shapes("Straight Connector 122").Visible = True
Shapes("Straight Connector 123").Visible = True
Shapes("Straight Connector 124").Visible = True
Shapes("Freeform 117").Visible = True
End If

End Sub

do em mới làm quen VBA nên còn kém lắm, mong các bác giúp đỡ
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
Cảm ơn anh vu_tuan_manh_linh và anh quanghai1969 nhiều lắm, tương tự như trên, nhưng có phần phức tạp hơn, nhờ các bác giúp em ẩn hiện Shape như file đính kèm với ạ,
hiện tại em làm thế này, không biết có cách nào hay và gọn hơn không nhỉ
...
do em mới làm quen VBA nên còn kém lắm, mong các bác giúp đỡ

bạn tải file đính kèm xem có đúng ko nhé ! ("code đã được rút gọn hết mức ..." ,,,,,,,)

Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
    If Target <> "" Then
        If Target = 0 Then
            Call hide_show_Obj(False, False)
        ElseIf Target = 1 Then
            Call hide_show_Obj(True, False)
        ElseIf Target = 2 Then
            Call hide_show_Obj(True, True)
        End If
    End If
    End If
End Sub

hoặc

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
    If Target <> "" Then
        Select Case Target.Value
            Case 0: Call hide_show_Obj(False, False)
            Case 1: Call hide_show_Obj(True, False)
            Case 2: Call hide_show_Obj(True, True)
        End Select
    End If
    End If
End Sub

Private Sub hide_show_Obj(dkien1 As Boolean, dkien2 As Boolean)
    Shapes("Group_obj_1").Visible = IIf(dkien1, True, False)
    Shapes("Group_obj_2").Visible = IIf(dkien2, True, False)
End Sub
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
bạn tải file đính kèm xem có đúng ko nhé ! ("code đã được rút gọn hết mức ..." ,,,,,,,)

Mã:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
    If Target <> "" Then
        If Target = 0 Then
            Call hide_show_Obj(False, False)
        ElseIf Target = 1 Then
            Call hide_show_Obj(True, False)
        ElseIf Target = 2 Then
            Call hide_show_Obj(True, True)
        End If
    End If
    End If
End Sub
Private Sub hide_show_Obj(dkien1 As Boolean, dkien2 As Boolean)
    'If dkien1 = True Then
        'Shapes("Group_obj_1").Visible = True
    'Else
        'Shapes("Group_obj_1").Visible = False
    'End If
    'rut gon.
    Shapes("Group_obj_1").Visible = IIf(dkien1, True, False)
    Shapes("Group_obj_2").Visible = IIf(dkien2, True, False)
End Sub

Còn 1 cách để rút gọn nữa và rất gọn đấy chứ.
Đặt lại toàn bộ tên của shapes từ 1 cho đến 16, chạy 2 vòng lặp For Next là xong, nhưng hiện tại tên shape như thế thì thua.
 
Upvote 0
cảm ơn anh quanghai1969, phucbugis, rất nhiều ạ, do em để mặc định cho excel đặt tên các shapes nên khá mắc công, theo như anh quanghai1969 góp ý về đặt tên lại, em thấy rất hay, cảm ơn anh nhiều lắm
 
Upvote 0
Các anh cho em hỏi chút. Nếu như "$D$1" trong If Target.Address = "$D$1" Then ô chứa công thức thì code sửa lại thế nào a
Chẳng hạn D1 = len(A1)
 
Upvote 0
Ý em là ô A1 chứa các ký tự có thể thay đổi và D1 là ô chứa công thức đếm ký tự của ô A1. Em đặt hàm ở trên ô D1 thì có lúc code VBA bên trên kia hoạt động được, có lúc lại thấy không chạy.
A có thể nói rõ giúp em được không.
 
Upvote 0
Ý em là ô A1 chứa các ký tự có thể thay đổi và D1 là ô chứa công thức đếm ký tự của ô A1. Em đặt hàm ở trên ô D1 thì có lúc code VBA bên trên kia hoạt động được, có lúc lại thấy không chạy.
A có thể nói rõ giúp em được không.
Tức là thay đổi chổ này nè bạn: If Target.Address = "$A$1" Then
 
Upvote 0
Web KT

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

Back
Top Bottom