2017-07-25 1466 views
0

我试图写一个代码,其中检查我的工作簿中的每个表的信息后输入图像我。自从我加入每个给它采空工作,并开始给我这个编译错误消息的代码,该代码作品,未经对于每一个,但我希望它是自动的。 u人能帮忙吗?编译错误:参数不可选的Excel VBA

EN:我想放在一起,它检查所有表和插入图像作为一个给定的单元格中数据的代码。代码工作不循环,但希望它是完全自动化,并提供了包括对于每个出现此编译错误。如果你能帮助我,这是感激。

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 
+1

你应该尝试写在英格尔斯的问题。如果你不知道如何使用翻译,有人将编辑可能的错误。 –

+0

Неставатакатука。 – Vityata

+0

有ü去,现在英格尔。 –

回答

0

既然你要运行的子BuscarImagemTavares你有充分的工作,你必须改变潜艇BOTH 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 

希望我可以帮助你有点。

+0

Duuuuuude,太感谢你了,它现在workig,我希望我能买焦炭ü! –

+0

不客气:) – FatTony

0

您所呼叫的事件例行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 
+0

是的,它的工作,酒精所以,问题是,现在是输入查询所有的图片在一张纸上,而不是每个表中的一个画面。 –

+0

这是因为你的ActiveSheet对象上工作。无论是插入ws.activate在调用(坏)的前传或整个工作表的参数,并在有效的。我刚开始更改代码,但FatTony是快... – FunThomas

+0

老兄你srsly帮了不少忙,谢谢!它现在是workig。 –