1

I'm very new to VBA and hope this is not a too-nooby-question.

I want to add PNG graphic to an Excel sheet by using a macro. Recording the macro and using it then is easy. But what I want: When I execute the macro, it should read a specific cell's content and import a graphic that has that cell's content (or a part of it) as it's name from a defined folder.

Do you understand? I want to do one click and it adds the correct graphics to my worksheets by checking the content of a cell in the worksheet.

EDIT: I managed to Import the Graphic, but it has the wrong size. How can I change that? My Code right now:

Sub GraphicfileInZelleEinfuegen_Reaktionszeit()
Dim strPfad As String
Dim strDatei As String
Dim lngZeile As Long
Dim lngSpalte As Long
Dim wksTabelle As Worksheet
Dim shpNeu As Object
Dim Modulnummer As Integer


lngSpalte = 3 ' = C
lngZeile = 22
Set wksTabelle = ActiveWorkbook.Worksheets("max_force_auslesen_Modul1")
 strPfad = wksTabelle.Cells(18, 10).Text                      
 If Right(strPfad, 1) <> "\" Then strPfad = strPfad & "\"
 strDatei = wksTabelle.Cells(19, 10).Text                        
 Set shpNeu = wksTabelle.Pictures.Insert(strPfad & strDatei)
 shpNeu.Top = wksTabelle.Rows(lngZeile).Top 'Einzufügende Zeile
 shpNeu.Left = wksTabelle.Columns(lngSpalte).Left
 shpNeu.Height = wksTabelle.Rows(600).Height
 shpNeu.Width = 111


End Sub
dalleaux
  • 527
  • 5
  • 11
  • Please show the code you have already tried. This is not a free code writing service. People can only show you what is missing/wrong in your code if you [edit] your question and add it formatted as code block. – Pᴇʜ Aug 29 '17 at 08:49
  • So what @Peh is saying is that this is indeed possible, and not too complicated if you ask the right questions (here or on google) – a-burge Aug 29 '17 at 10:04
  • I managed to do the most of it, but still have a little problem (see edit) – dalleaux Aug 29 '17 at 12:18

1 Answers1

1

Okay, I got it...:

shpNeu.Width = 1000

Easy-Peasy

dalleaux
  • 527
  • 5
  • 11
  • 1
    Nice that you figured it out! Just some notes: (1) There is a big difference between `ActiveWorkbook` and `ThisWorkbook` figure it out! Most times when people write `ActiveWorkbook` they actually mean `ThisWorkbook` make sure which is the right one for you. (2) [Always use `Long` instead of `Integer`](https://stackoverflow.com/a/26409520/3219613) there is no advantage in `Integer` and you can avoid the issues you might get by using it. Apart from that well done. – Pᴇʜ Aug 30 '17 at 12:56