【问题标题】:Compile error: Argument not optional vba excel编译错误:参数不是可选的 vba excel
【发布时间】:2023-03-30 06:55:02
【问题描述】:

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

Sub ForEachWs()
Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
    Call Worksheet_SelectionChange
    Next ws

End Sub

Sub Worksheet_SelectionChange(ByVal Target As Range)

   On Error Resume Next

    If Target.Column = 2 And Target.Row = 1 Then ' onde clicar para buscar imagem

        BuscarImagemTavares (Target.Value)

    End If

End Sub

Sub BuscarImagemTavares(Produto As String)
    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
    Exit Sub
    End If

    Dim Imagem, CaminhoImagem As String

    If Len(Produto) = 3 Then 'acrescenta 00 antes do cod do produto
        Produto = "00" & Produto
    End If
    If Len(Produto) = 4 Then 'acrescenta 0 antes do cod do produto
        Produto = "0" & Produto
    End If

    Imagem = Dir("\\Clfssrvfar\ENGENHARIA\GESTAO_DE_PROJETOS\04. FOLLOWUP\09. ARQUIVOS PARA FERRAMENTAS\09.1 IMAGENS\09.1.2 IMAGENS PRODUTOS\" & Produto & "*", vbDirectory)

    CaminhoImagem = "\\Clfssrvfar\ENGENHARIA\GESTAO_DE_PROJETOS\04. FOLLOWUP\09. ARQUIVOS PARA FERRAMENTAS\09.1 IMAGENS\09.1.2 IMAGENS PRODUTOS\" & Imagem



    With ActiveSheet.Pictures.Insert(CaminhoImagem) 'Mostra Imagem
        'Define tamanho e posição da imagem

    With .ShapeRange
        .Width = 75
        .Height = 115
        .Top = 7
        .Left = 715
        '*above it's me trying to make white background transparent*
            'With .PictureFormat
            '.TransparentBackground = True
            '.TransparencyColor = RGB(255, 0, 0)
            'End With
        '.Fill.Visible = True
        'End With
        'ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        'Application.CommandBars("Format Object").Visible = False
    End With
    End With
    If CaminhoImagem <> "" Then 'Após inserir imagem informa "ok" na B2 para não inserir de novo
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "OK"
    End If

End Sub

【问题讨论】:

  • 您到底想用子ForEachWs() 实现什么?
  • 我希望它是自动的,我只是启动宏,它会遍历每张纸,否则我必须访问每张纸才能输入图像

标签: excel vba compiler-errors


【解决方案1】:

由于您想为您拥有的每个工作表运行子 BuscarImagemTavares,您必须同时更改子 ForEachWsBuscarImagemTavares

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

希望能帮到你。

【讨论】:

    【解决方案2】:

    您正在调用事件例程Sub Worksheet_SelectionChange。这是当用户更改所选单元格(移动光标)时从 Excel 自动调用的例程。允许手动调用事件例程,但您必须传递 range 作为参数(表示选择的范围),例如:

    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
    

    【讨论】:

    • 是的,它成功了,太酷了,现在的问题是它在一张纸中输入所有图片,而不是在每张纸中输入一张图片。
    • 这是因为您在使用 activeSheet 对象。要么在调用前插入 ws.activate (错误),要么将整个工作表作为参数传递并处理它。我刚开始改代码,但是 FatTony 更快...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多