Code chạy bị lỗi Runtime Error '424'

Liên hệ QC

Bảy Dzõ

Ngu Excel nhất GPE
Tham gia
18/11/09
Bài viết
565
Được thích
1,361
Nghề nghiệp
Just for daily's rice
Nhờ mọi người xem giúp mình bị lỗi chỗ nào mà nó cứ báo lỗi hoài... Mò hoài mà không biết nó bị ở đâu. Cám ơn

Mã:
Sub CountColorsST()    Dim sh As Shape
    Dim i As Integer
    Dim ws As Worksheet
    Dim SS As Worksheet
    
    Set ws = ThisWorkbook.Sheets("Sun-Thu")
    Set SS = ThisWorkbook.Sheets("Settings")
    ssLastRow = SS.Cells(SS.Rows.Count, "A").End(xlUp).Row - 1
    
    For i = 2 To ssLastRow
        SS.Cells(i, 2).Value = 0
    Next i
    
    For Each sh In Application.ActiveSheet.Shapes
        If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
            For i = 2 To ssLastRow
                If (sh.Fill.ForeColor = SS.Cells(i, 1).Interior.Color) Then
                    SS.Cells(i, 2).Value = SS.Cells(i, 2).Value + 1
                End If
            Next i
        End If
    Next
End Sub

Nó báo lỗi ở hàng: If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
 
Nhờ mọi người xem giúp mình bị lỗi chỗ nào mà nó cứ báo lỗi hoài... Mò hoài mà không biết nó bị ở đâu. Cám ơn

Mã:
Sub CountColorsST()    Dim sh As Shape
    Dim i As Integer
    Dim ws As Worksheet
    Dim SS As Worksheet
    
    Set ws = ThisWorkbook.Sheets("Sun-Thu")
    Set SS = ThisWorkbook.Sheets("Settings")
    ssLastRow = SS.Cells(SS.Rows.Count, "A").End(xlUp).Row - 1
    
    For i = 2 To ssLastRow
        SS.Cells(i, 2).Value = 0
    Next i
    
    For Each sh In Application.ActiveSheet.Shapes
        If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
            For i = 2 To ssLastRow
                If (sh.Fill.ForeColor = SS.Cells(i, 1).Interior.Color) Then
                    SS.Cells(i, 2).Value = SS.Cells(i, 2).Value + 1
                End If
            Next i
        End If
    Next
End Sub

Nó báo lỗi ở hàng: If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
Mã:
If (shp.Name Like "*" & "Rectangle" & "*" Or shp.Name Like "*" & "Flowchart" & "*" Or shp.Name Like "*" & "Freeform" & "*") Then
Bạn thử xem có thế này được ko?
 
Upvote 0
Đọc qua thấy cái này:

For Each sh In Application.ActiveSheet.Shapes
If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
For i = 2 To ssLastRow
If (sh.Fill.ForeColor = SS.Cells(i, 1).Interior.Color) Then
SS.Cells(i, 2).Value = SS.Cells(i, 2).Value + 1
End If
Next i
End If
Next

Màu xanh và đỏ khác nhau hả anh? Và cũng chả cần ngoặc sau If đâu...
 
Upvote 0
Đọc qua thấy cái này:

For Each sh In Application.ActiveSheet.Shapes
If (shp.Name Like "*Rectangle*" Or shp.Name Like "*Flowchart*" Or shp.Name Like "*Freeform*") Then
For i = 2 To ssLastRow
If (sh.Fill.ForeColor = SS.Cells(i, 1).Interior.Color) Then
SS.Cells(i, 2).Value = SS.Cells(i, 2).Value + 1
End If
Next i
End If
Next

Màu xanh và đỏ khác nhau hả anh? Và cũng chả cần ngoặc sau If đâu...

Cảm ơn bác Hướng nhiều. Em mò từ sáng đến giờ nhìn đi nhìn lại vẫn không thấy được cái lỗi đó. Phải là "sh". Code chạy được rồi.
 
Upvote 0
Cảm ơn bác Hướng nhiều. Em mò từ sáng đến giờ nhìn đi nhìn lại vẫn không thấy được cái lỗi đó. Phải là "sh". Code chạy được rồi.

Nên dùng "Option Explicit" để buộc phải khai báo biến và kiểm soát lỗi tên biến anh nhé.
 
Upvote 0
Web KT
Back
Top Bottom