Notifications
Clear all

VBA inserir imagem na coluna A

2 Posts
2 Usuários
0 Reactions
1,416 Visualizações
(@palmanhani)
Posts: 0
New Member
Topic starter
 

Pessoal,

Estou usando esse codigo para inserir imagens em uma planilha, mas tentei alterar o codigo para que a imagem fique na coluna A e se ajuste ao tamanho da celula.

Alguem pode me ajudar..... Obrigado

Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long

lastrow = Worksheets("sheet1").Range("B1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select 'This is where picture will be inserted

pictname = Cells(x, 2) 'This is the picture name
ActiveSheet.Pictures.Insert("C:UsersRodrigoPictures2016-11" & pictname & ".jpg").Select 'Path to where pictures are stored

With Selection

.Left = Cells(pasterow, 1).Left
.Top = Cells(pasterow, 1).Top

.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 80#
.ShapeRange.Width = 80#
.ShapeRange.Rotation = 0#
End With

Next

End Sub
 
Postado : 24/10/2019 9:19 pm
(@laerteb)
Posts: 0
New Member
 

Boa noite, Palmanhani (Boa madrugada, :lol: )

Amigo obrigado por esta dúvida, aprendi algo novo ajudando a ti, pois ainda não tinha feito este tipo de
ajuste de imagem (automaticamente) em relação as dimensões da célula ;) ....

Então vamos lá... terá que deletar o seu código (mensagem anterior) e colocar esse que se encontra
abaixo:

Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long

lastrow = Worksheets("Planilha1").Range("B1").CurrentRegion.Rows.Count

x = 2

For x = 2 To lastrow

Set pastehere = Cells(x, 1)

pasterow = pastehere.Row

Cells(pasterow, 1).Select 'This is where picture will be inserted

pictname = Cells(x, 2) 'This is the picture name
ActiveSheet.Pictures.Insert("C:UsersRodrigoPictures2016-11" & pictname & ".jpg").Select 'Path to where pictures are stored

' ****************************
 Dim iColumnWidth  As Long
 Dim iRowHeight  As Long

  iColumnWidth = Columns("a").ColumnWidth * 5.3
  iRowHeight = Rows(pasterow).RowHeight
' ****************************

With Selection

.Left = Cells(pasterow, 1).Left
.Top = Cells(pasterow, 1).Top

.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = iRowHeight 
.ShapeRange.Width = iColumnWidth 
.ShapeRange.Rotation = 0#

End With

Next

End Sub

Como pode observar, fiz pequenas alterações no Height e Width e acrescentei um trecho de código
que fazem o ajuste que pediu :) ...

Agora com isso qualquer imagem (de qualquer tamanho) ficará perfeitamente ajustada na célula da Coluna "A" :D ...

Espero que seja isto que queria :) ...

Qualquer coisa estamos aqui para ajudar ;) ...

Aguardando sua resposta e seu Feed Back(é muito importante) ... se foi útil, não esqueça de clicar na "mãozinha" :D

LaerteB :D

 
Postado : 25/10/2019 10:35 pm