Alguém pode me explicar como transformar um link da web (URL) em uma imagem.
Imagem de exemplo (URL é http://cache.lego.com/media/bricks/5/1/4667591.jpg
)
O que estou tentando fazer é fazer com que uma lista de peças que baixei exiba a imagem em vez do link da web acima.
O que tenho em J2 a J1903 é:
http://cache.lego.com/media/bricks/5/1/4667591.jpg
http://cache.lego.com/media/bricks/5/1/4667521.jpg
...
O que eu gostaria de fazer é que o Excel transforme tudo isso (10.903 deles) em imagens (tamanho de célula 81x81).
Alguém pode explicar passo a passo como posso fazer isso?
Responder1
Se você tiver um conjunto de links na colunaJ.como:
e você executa esta pequena macro VBA:
Sub InstallPictures()
Dim i As Long, v As String
For i = 2 To 1903
v = Cells(i, "J").Value
If v = "" Then Exit Sub
With ActiveSheet.Pictures
.Insert (v)
End With
Next i
End Sub
cada um dos links será aberto e a imagem associada será colocada na planilha.
As fotos deverão estar devidamente dimensionadas e posicionadas.
EDITAR#1:
As macros são muito fáceis de instalar e usar:
- ALT-F11 abre a janela VBE
- ALT-I ALT-M abre um novo módulo
- cole o material e feche a janela do VBE
Se você salvar a pasta de trabalho, a macro será salva com ela. Se você estiver usando uma versão do Excel posterior a 2003, deverá salvar o arquivo como .xlsm em vez de .xlsx
Para remover a macro:
- abra a janela VBE como acima
- limpar o código
- feche a janela do VBE
Para usar a macro do Excel:
- ALT-F8
- Selecione a macro
- Toque em EXECUTAR
Para saber mais sobre macros em geral, consulte:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
e
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
As macros devem estar habilitadas para que isso funcione!
EDITAR#2:
Para evitar parar em erros de recuperação, use esta versão:
Sub InstallPictures()
Dim i As Long, v As String
On Error Resume Next
For i = 2 To 1903
v = Cells(i, "J").Value
If v = "" Then Exit Sub
With ActiveSheet.Pictures
.Insert (v)
End With
Next i
On Error GoTo 0
End Sub
Responder2
Esta é a minha modificação:
- Substitua a célula pelo link com imagem (não uma nova coluna)
- Faça com que as fotos sejam salvas com o documento (em vez de links que podem ser frágeis)
- Torne as imagens um pouco menores para que elas tenham a chance de serem classificadas com suas células.
Código abaixo:
Option Explicit
Dim rng As Range
Dim cell As Range
Dim Filename As String
Sub URLPictureInsert()
Dim theShape As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
' Set to the range of cells you want to change to pictures
Set rng = ActiveSheet.Range("A2:A600")
For Each cell In rng
Filename = cell
' Use Shapes instead so that we can force it to save with the document
Set theShape = ActiveSheet.Shapes.AddPicture( _
Filename:=Filename, linktofile:=msoFalse, _
savewithdocument:=msoCTrue, _
Left:=cell.Left, Top:=cell.Top, Width:=60, Height:=60)
If theShape Is Nothing Then GoTo isnill
With theShape
.LockAspectRatio = msoTrue
' Shape position and sizes stuck to cell shape
.Top = cell.Top + 1
.Left = cell.Left + 1
.Height = cell.Height - 2
.Width = cell.Width - 2
' Move with the cell (and size, though that is likely buggy)
.Placement = xlMoveAndSize
End With
' Get rid of the
cell.ClearContents
isnill:
Set theShape = Nothing
Range("A2").Select
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub
Responder3
Este funciona muito melhor porque a imagem fica ao lado da célula a que pertence.
Option Explicit
Dim rng As Range
Dim cell As Range
Dim Filename As String
Sub URLPictureInsert()
Dim theShape As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set rng = ActiveSheet.Range("C1:C3000") ' <---- ADJUST THIS
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then '<--- ONLY USES JPG'S
ActiveSheet.Pictures.Insert(Filename).Select
Set theShape = Selection.ShapeRange.Item(1)
If theShape Is Nothing Then GoTo isnill
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
With theShape
.LockAspectRatio = msoFalse
.Width = 100
.Height = 100
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
isnill:
Set theShape = Nothing
Range("A2").Select
End If
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub