Macro VBA for inserting images automatically based on a column of codes

Asked

Viewed 6,622 times

2

Good afternoon,

I have a spreadsheet, in which there is a column with the code of the photos and in the other column beside with spaces to insert the images. I wonder if there is any way to elaborate a macro, in which it recognizes the code filled next to and search in a certain folder the photo related to this code.

Ex:

inserir a descrição da imagem aqui

In order to complement only, is it possible to add the correction of two restrictions to the formula? Next, when there is the code of the photo in the spreadsheet (ex: 1532) but in the folder there is no photo with this code, can it skip the line in which this code is described and continue inserting photos to the next ones? And the other restriction would be for him to put picture only where it is empty and skip those that already have picture.

1 answer

0


If I understood correctly, you have this table in a spreadsheet (in VBA I considered it to be in the range A2:B6) and what you call code refers to the image file name without its localized extension and folder.

With these deductions I suggest the following code:

Sub Teste()

'Definir intervalo onde estão os códigos das imagens
    Dim TodosCod, Cod As Range
        Set TodosCod = ActiveSheet.Range("A2:A9")

'Definir variáveis para o procedimento de inserção de fotos
    Dim Pasta, Ext, TxtCod As String
    Dim Fig As Shape
    Dim FigJaExist As Boolean
        Pasta = "C:\Users\TashRiser\Desktop\"
        Ext = ".jpg"

'Inserir a imagem baseado no código da imagem
    For Each Cod In TodosCod
        TxtCod = Cod.Value
        FigJaExist = False

'Checar a existência do arquivo
        If Not Dir(Pasta & TxtCod & Ext) = "" Then

'Checar se há alguma foto na célula de destino
            For Each Fig In ActiveSheet.Shapes
                If Fig.TopLeftCell.Address = Cod.Offset(0, 1).Address Then FigJaExist = True
                Next Fig

'Se não houver foto na célula, inserir o arquivo
            If FigJaExist = False Then
                With ActiveSheet.Pictures.Insert(Pasta & TxtCod & Ext)
                    .Left = Cod.Offset(0, 1).Left
                    .Top = Cod.Offset(0, 1).Top
                '...caso queira determinar a largura e altura da imagem
                    .ShapeRange.LockAspectRatio = msoFalse
                    .ShapeRange.Width = 100
                    .ShapeRange.Height = 100
                    End With
                End If
            End If
        Next Cod

End Sub
  • Thanks for the feedback friend. A question, I need to generate a button, or drop the code and update Ctrl+alt+Shift+F9?

  • Sorry, I don’t understand!!! Do you want to know how to generate a button by code or how to put a button to run this code? And q Ctrl+Alt+F9 has to be?

  • 1

    I got it!!!! Man, without words, gave right, until the function of dimensioning in the cell. Thank you so much for the help, man, I really admire the commitment of the people who help here.

  • Glad it all worked out! Don’t forget to give that check in green for the right answer, and if possible give votes, so vc encourages respondents and contributes to the growth of the community!

  • 1

    You can leave, friend! Now, in order to just complement, there is the possibility to add 2 corrections of restrictions? Next, when there is the code of the photo in the spreadsheet (ex: 1532) but in the folder there is no photo with this code, can it skip the line in which this code is described and continue inserting photos to the next ones? And the other restriction would be for him to put photo only where it is empty and skip those that already have photo. I thank from now.

  • I adapted the code to meet these two restrictions. I suggest you edit your original question and insert this question, to share with the community. If only in the comment, the visibility is lower!

  • It worked perfectly!!! Thank you very much. I’ve edited the question as suggested.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.