编译错误:参数不是可选的vba excel

编译错误:参数不是可选的vba excel,excel,vba,compiler-errors,Excel,Vba,Compiler Errors,我正在尝试编写一个代码,在检查工作簿每一页中的信息后,在其中输入一个图像。由于我将for-each添加到代码中,它停止工作并开始向我提供此编译错误消息,因此代码可以在没有for-each的情况下工作,但我希望它是自动的。你能帮忙吗 Sub ForEachWs() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets Call Worksheet_SelectionChange Next ws End

我正在尝试编写一个代码,在检查工作簿每一页中的信息后,在其中输入一个图像。由于我将for-each添加到代码中,它停止工作并开始向我提供此编译错误消息,因此代码可以在没有for-each的情况下工作,但我希望它是自动的。你能帮忙吗

Sub ForEachWs()
Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
    Call Worksheet_SelectionChange
    Next ws

End Sub

您正在调用事件例程子工作表\u SelectionChange。这是一个例程,当用户更改移动光标的选定单元格时,将从Excel自动调用该例程。允许手动调用事件例程,但必须将范围作为表示所选范围的参数传递,例如:

For Each ws In ActiveWorkbook.Worksheets
    Call Worksheet_SelectionChange(ws.cells(1,2))
Next ws
这将满足编译器的要求,但是,为什么不直接调用实际例程:

For Each ws In ActiveWorkbook.Worksheets
    Call BuscarImagemTavares (ws.cells(1,2).value)
Next ws

由于要为每个工作表运行子BuscarImagemTavares,因此必须同时更改子ForEachWs和BuscarImagemTavares

ForEachWs:

Sub ForEachWs()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        'Here you can directly call the sub without the sub Worksheet_SelectionChange
        Call BuscarImagemTavares(ws, ws.Cells(1,2).Value)
        'in BuscarImagemTavares you´ll need the ws reference to actually work on the right worksheet (otherwise youll always work on the selected one)
    Next ws

End Sub
BuscarImagemTavares:

Sub BuscarImagemTavares(ByVal ws as Worrksheet, Produto As String)
    'Mind the additional parameter 'ws'
    On Error Resume Next
    'Autor: Tavares

    'If Range("B2") = "ok" Then 'Verifica se celula B2 tem ok se sim não insere a imagem novamente
    If ws.Range("B2") = "ok" Then 'Here you actually have to use a reference to the Worksheet you want to use, otherwise alwys the same will be used
        Exit Sub
    End If

    ...

    'You need the reference here as well so you won#t use the same worksheet over and over again
    With ws.Pictures.Insert(CaminhoImagem) 'Mostra Imagem

    ...

    If CaminhoImagem <> "" Then 'Após inserir imagem informa "ok" na B2 para não inserir de novo
        'Range("B2").Select
        'ActiveCell.FormulaR1C1 = "OK"
        'If you don´t actually need the cell in excel to be selected after the programm finished you should´nt use the '.select' and '.selection' instead use this:
        ws.Range("B2").Value= "OK" 'Since you aren´t adding a formula you should address the '.value' property
    End If

    ...

End Sub

希望我能帮你一点忙。

你到底想用子ForEachWs实现什么?我希望它是自动的,我只需启动宏,它就会遍历每一张表,否则我必须访问每一张表才能输入图像是的,它工作了,太酷了,现在的问题是,它将所有图片输入到一张工作表中,而不是每个工作表中的一张图片。这是因为您使用的是activeSheet对象。在调用bad之前插入一个ws.activate,或者将整个工作表作为参数传递并处理它。我刚开始修改代码,但FatTony更快了。。。
Sub ForEachWs()
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        'Here you can directly call the sub without the sub Worksheet_SelectionChange
        Call BuscarImagemTavares(ws, ws.Cells(1,2).Value)
        'in BuscarImagemTavares you´ll need the ws reference to actually work on the right worksheet (otherwise youll always work on the selected one)
    Next ws

End Sub
Sub BuscarImagemTavares(ByVal ws as Worrksheet, Produto As String)
    'Mind the additional parameter 'ws'
    On Error Resume Next
    'Autor: Tavares

    'If Range("B2") = "ok" Then 'Verifica se celula B2 tem ok se sim não insere a imagem novamente
    If ws.Range("B2") = "ok" Then 'Here you actually have to use a reference to the Worksheet you want to use, otherwise alwys the same will be used
        Exit Sub
    End If

    ...

    'You need the reference here as well so you won#t use the same worksheet over and over again
    With ws.Pictures.Insert(CaminhoImagem) 'Mostra Imagem

    ...

    If CaminhoImagem <> "" Then 'Após inserir imagem informa "ok" na B2 para não inserir de novo
        'Range("B2").Select
        'ActiveCell.FormulaR1C1 = "OK"
        'If you don´t actually need the cell in excel to be selected after the programm finished you should´nt use the '.select' and '.selection' instead use this:
        ws.Range("B2").Value= "OK" 'Since you aren´t adding a formula you should address the '.value' property
    End If

    ...

End Sub