I'm trying to write a code where I input an image after checking the info in each sheet of my workbook. Since I added for each to the code it stopped working and started giving me this compile error message, the code works without the for each but i want it to be automatic. Can you help?
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
You are calling the Event-Routine Sub Worksheet_SelectionChange
. This is a routine that is called from Excel automatically when the user is changing the selected cell (moving cursor). It is allowed to call the event-routine by hand, but you have to pass a range
as parameter (representing the range that was selected), for example:
For Each ws In ActiveWorkbook.Worksheets
Call Worksheet_SelectionChange(ws.cells(1,2))
Next ws
This will satisfy the compiler, however, why not calling the real routine directly:
For Each ws In ActiveWorkbook.Worksheets
Call BuscarImagemTavares (ws.cells(1,2).value)
Next ws
Since you want to run the sub BuscarImagemTavares
for every worksheet you have, you have to alter both the subs ForEachWs
and 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
Hope I could help you a bit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.