首页 > 解决方案 > VBA - 代码上的参数不是可选错误

问题描述

我想编写一个 VBA 代码来检查单元格是否有图像,以及它是否在与图像相同的行中打印一些东西(可能是任何东西,在这种情况下我选择了数字 1),在第 6 列。

我在子的第一行不断收到“Argument not Optional”错误。好奇是否有人可以帮助我!谢谢!!

Function CellImageCheck(CellToCheck As Range) As Integer
' Return 1 if image exists in cell, 0 if not

    Dim wShape As Shape

    For Each wShape In ActiveSheet.Shapes
        If wShape.TopLeftCell = CellToCheck Then
            CellImageCheck = 1
        Else
            CellImageCheck = 0
        End If
    Next wShape

End Function

Sub testFunction()
Dim i As Integer
Application.ScreenUpdating = False
For i = 3 To 10 Step 1
    If CellImageCheck(Cells(i, 1)) Then
        Cells(i, 6) = CellImageCheck
    Else
    End If
Next i
End Sub

标签: excelvba

解决方案


根据我的评论:

Function CellImageCheck(CellToCheck As Range) As Boolean
' Return True if image exists in cell, False if not
    CellImageCheck = False
    
    Dim wShape As Shape
    For Each wShape In ActiveSheet.Shapes
        If wShape.TopLeftCell.Address = CellToCheck.Address Then
            CellImageCheck = True
            Exit For
        End If
    Next wShape

End Function

Sub testFunction()
    Application.ScreenUpdating = False
    With Worksheets("Sheet1") 'Change this to the sheet being tested.
        Dim i As Long
        For i = 3 To 10 Step 1
            Dim bl As Boolean
            bl = False
            bl = CellImageCheck(.Cells(i, 1))
            
            If bl Then .Cells(i, 6) = bl
        Next i
    End With
End Sub

推荐阅读