I'm trying to help out a coworker with her VBA in Excel 2013. It looks like the macro is successfully pulling in the images from the designated path, but it dumps every single photo into cell A1.
Any thoughts?
Sub DeleteAllPictures()
Dim S As Shape
For Each S In ActiveSheet.Shapes
Select Case S.Type
Case msoLinkedPicture, msoPicture
S.Delete
End Select
Next
End Sub
Sub UpdatePictures()
Dim R As Range
Dim S As Shape
Dim Path As String, FName As String
'Setup the path
Path = "G:\In Transit\Carlos\BC Website images"
'You can read this value also from a cell, e.g.:
'Path = Worksheets("Setup").Range("B1")
'Be sure the path has a trailing backslash
If Right(Path, 1) <> "\" Then Path = Path & "\"
'Visit each used cell in column A
For Each R In Range("B1", Range("B" & Rows.Count).End(xlUp))
'Try to get the shape
Set S = GetShapeByName(R)
'Found?
If S Is Nothing Then
'Find the picture e.g. "C:\temp\F500.*"
FName = Dir(Path & R & ".*")
'Found?
If FName <> "" Then
Set S = InsertPicturePrim(Path & FName, R)
End If
End If
If Not S Is Nothing Then
'Show the error if the name did not match the cell
If S.Name <> R Then R.Interior.Color = vbRed
With R.Offset(0, 1)
'Move the picture to the cell on the right side
S.Top = .Top
S.Left = .Left
'Resize it
S.Width = .Width
'Remove the aspect ratio by default if necessary
'S.LockAspectRatio = False
If S.LockAspectRatio Then
'Make it smaller to fit the cell if necessary
If S.Height > .Height Then S.Height = .Height
Else
'Stretch the picture
S.Height = .Height
End If
End With
'Move it behind anything else
S.ZOrder msoSendToBack
Else
R.Offset(0, 1) = "No picture available"
End If
Next
End Sub
Private Function GetShapeByName(ByVal SName As String) As Shape
'Return the shape with SName, Nothing if not exists
On Error Resume Next
Set GetShapeByName = ActiveSheet.Shapes(SName)
End Function
Private Function InsertPicturePrim(ByVal FName As String, ByVal SName As String) As Shape
'Inserts the picture, return the shape, Nothing if failed
Dim P As Picture
On Error Resume Next
'Insert the picture
Set P = ActiveSheet.Pictures.Insert(FName)
'code to resize
With P
.ShapeRange.LockAspectRatio = msoFalse
.Height = ActiveCell.Height
.Width = ActiveCell.Width
.Top = ActiveCell.Top
.Left = ActiveCell.Left
.Placement = xlMoveAndSize
End With
Set P = Nothing
'code to resize
'Success?
If Not P Is Nothing Then
'Return the shape
Set InsertPicturePrim = P.ShapeRange(1)
'Rename it, so we can easily find it later
P.Name = SName
End If
End Function
The short answer is: your macro is inserting the picture at the selected cell. Change the selection before the insert line, and you should get it inserted at each row.
Here in this example, I am selecting the cell to the left of the cell you are pulling the name value from.
If FName <> "" Then
'select the cell 1 to the left of the cell containing the image name
R.Offset(0,-1).select
Set S = InsertPicturePrim(Path & FName, R)
End If
Related
I was trying to insert some pictures that are saved on my desktop to an excel file.
I found that some online codes worked well. But it seemed that those inserted pictures were not saved with the documents - the inserted pictures won't be displayed when I opened the file on another computer. I am wondering how I should tweak the codes so it can save the inserted pictures within the excel? If possible with VBA, how to adjust the inserted pictures to their 50% dimensions? I am completely new to VBA. Sorry for this basic question.
Sub add_pictures_R2()
Dim i%, ppath$
For i = 2 To 145
' file name at column A
ppath = "C:\Users\myname\output\" & CStr(Cells(i, 1).Value) & ".png"
If Len(Dir(ppath)) Then
With ActiveSheet.Pictures.Insert(ppath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 300
End With
.Left = ActiveSheet.Cells(i, 10).Left
.Top = ActiveSheet.Cells(i, 10).Top
.Placement = 1
.PrintObject = True
End With
End If
Next
End Sub
You can do either, edit the path of the file to go along with your excel file or you could embed it. For embedding I would look at this.
https://danny.fyi/embedding-and-accessing-a-file-in-excel-with-vba-and-ole-objects-4d4e7863cfff
Its a bit messy but you would achieve what you want to do with at least the file being in the document and not trying to transfer everything with it.
Try this (using Shapes.AddPicture)
Sub add_pictures_R2()
'Note - type identifiers such as `S`, `%` are very outdated...
Dim i As Long, ppath As String, ws As Worksheet, c As Range
Set ws = ActiveSheet 'use a specific/explicit sheet reference
For i = 2 To 145
ppath = "C:\Users\myname\output\" & CStr(ws.Cells(i, 1).Value) & ".png"
Set c = ws.Cells(i, 10) 'insertion point
'passing -1 to Width/Height preserves original size
With ws.Shapes.AddPicture(Filename:=ppath, linktofile:=msoFalse, _
savewithdocument:=msoTrue, _
Left:=c.Left, Top:=c.Top, Width:=-1, Height:=-1)
.LockAspectRatio = msoTrue
.Placement = xlMove
.Height = .Height / 2 'size to 50%
End With
Next i
End Sub
I got the answer from Jimmypop at mrexcel. It worked.
Sub add_pictures_R2()
Const folderPath As String = "C:\Users\YANG\output\"
Dim r As Long
Application.ScreenUpdating = False
With ActiveSheet
For r = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
If Dir(folderPath & .Cells(r, "A").Value & ".png") <> vbNullString Then
.Shapes.AddPicture Filename:=folderPath & .Cells(r, "A").Value & ".png", _
LinkToFile:=False, SaveWithDocument:=True, _
Left:=ActiveSheet.Cells(r, 10).Left, Top:=ActiveSheet.Cells(r, 10).Top, Width:=.Cells(r, "C").Width, Height:=.Cells(r, "C").Height
Else
.Cells(r, "B").Value = "Not found"
End If
DoEvents
Next
End With
Set myDocument = Worksheets(1)
For Each s In myDocument.Shapes
Select Case s.Type
Case msoLinkedPicture, msoPicture
s.ScaleHeight 0.5, msoTrue
s.ScaleWidth 0.5, msoTrue
Case Else
' Do Nothing
End Select
Next
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
I developed code to search images from a folder (chosen by the user) and put it in a column in Excel thru some loops.
Now I'm trying to work with multi-dimensional arrays, so it can also insert images into multiple columns. Instead of putting the images inside column A, I'm trying to make a loop so it also inserts the images inside column B.
What I achieved so far:
Sub ReadFolder()
'
'ReadFolder
'
Dim File As Variant
Dim Counter As Long
Dim DirectoryList() As String
Dim varResp As Variant
Dim shape as Excel.shape
ReDim DirectoryList(1000)
' check if the user inserted a valid path or if he canceled the operation and offer him a chance to abort the operation or retry
lblTryAgain:
varResp = InputBox("Type down the files path)", "Path")
If Trim(varResp) = "" Then
If MsgBox("Do you wish to abort?", vbYesNo + vbQuestion, "Abort?") = vbYes Then
GoTo lblExit
Else
GoTo lblTryAgain
End If
Else
File = Dir$(varResp & "\*.*")
If File = "" Then
MsgBox "The path doesnt exist , Please retry", vbExclamation, "Fail"
GoTo lblTryAgain
End If
End If
On Error GoTo Erro
' fill the array with elements that are inside the file(gotta put then into a 2d array with the dimension (n,2)
Do While File <> ""
DirectoryList(Counter) = File
File = Dir$
Counter = Counter + 1
Loop
' resize the array accordingly to the number of elements filled inside it
ReDim Preserve DirectoryList(Counter - 1)
' delete the images inside the sheet before inserting new ones
For Each shape In Worksheets("Sheet1").Shapes
shape.Delete
Next
' loop thru the array and put images into columns A and resize the column, images and rows
For i = 0 To UBound(DirectoryList)
for j = 0 to 1
Debug.Print DirectoryList(i)
With Worksheets("Sheet1").Cells(i+1, j+1)
Set File = Worksheets("Sheet1").Pictures.Insert(DirectoryList(i))
File.Top = .Top
File.Left = .Left
File.ShapeRange.LockAspectRatio = msoFalse
File.Placement = xlMoveAndSize
.ColumnWidth = 30
.RowHeight = 100
File.ShapeRange.Width = 170
File.ShapeRange.Height = 100
End With
next j
Next i
lblExit:
Exit Sub
Erro:
MsgBox "OOpssie, Fail!", vbCritical, "Error"
End Sub
I expect this (considering the folder has only 4 images):
I get this:
Points to consider:
The last image has changed because I accidentally deleted it (that doesn't interfere with the problem)
There is no way to tell how many images the folder is going to have
Try something like this:
Sub ReadFolder()
Const ROW_START As Long = 3 'start row
Const COL_START As Long = 3 'start column
Const PER_ROW As Long = 4 'how many pics per row
Dim File As Variant
Dim Counter As Long
Dim DirectoryList() As String
Dim folder As Variant
Dim shape As Excel.shape, ws As Worksheet, i As Long, c As Range
'get folder or cancel
Do
folder = GetFolderPath("Select the source folder")
If Len(folder) = 0 Then
If MsgBox("Abort?", vbYesNo) = vbYes Then Exit Sub
End If
Loop While Len(folder) = 0
'any files?
File = Dir(folder & "*.png", vbNormal)
If Len(File) = 0 Then
MsgBox "No files in this folder"
Exit Sub
End If
Set ws = Worksheets("Sheet1")
For i = ws.Shapes.Count To 1 Step -1
ws.Shapes(i).Delete
Next i
Set c = ws.Cells(ROW_START, COL_START) 'start cell for pics
Do While Len(File) > 0
Debug.Print "Inserting at: " & c.Address
If c.Row = ROW_START Then c.ColumnWidth = 30 'only need to set once per row/column
If c.Column = COL_START Then c.RowHeight = 100
With ws.Pictures.Insert(folder & File)
.Placement = xlMoveAndSize
.Top = c.Top
.Left = c.Left
.ShapeRange.LockAspectRatio = msoFalse
.Width = 170
.Height = 100
End With
'where is the next picture going?
If (c.Column - COL_START) >= PER_ROW - 1 Then 'already at max column?
Set c = c.Offset(1).EntireRow.Cells(COL_START) 'first cell on next row
Else
Set c = c.Offset(0, 1) 'move one cell over
End If
File = Dir()
Loop
End Sub
'get a folder from the user - returns empty string if no selection
Function GetFolderPath(msg As String) As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = msg
If .Show = -1 Then GetFolderPath = .SelectedItems.Item(1) & "\"
End With
End Function
I have been using this code which works for some URL but not for all I really do not why. Then I have tried with different available codes online but no success.
Your help will be really appreciated in this regards.
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("A1:A3000") ' <---- ADJUST THIS
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then ' <--- USES JPG ONLY
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 = 20
.Height = 20
.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
URL's
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/fc310885-cd82-49cb-bc7a-aabd08531517.jpg
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/c6c7a645-8273-40ee-87e5-1dd385111a28.jpg
https://s3-eu-west-1.amazonaws.com/images.linnlive.com/a93f20bbb3640a1a7dc9b9a05bee8540/cf9f971b-6af6-4894-a2d5-c58681adb466.jpg
Try this code below, it will Debug.Print the URL that fails to insert. Adapt to your need (if any)
Sub URLPictureInsert()
Dim rng As Range
Dim cell As Range
Application.ScreenUpdating = False
With ActiveSheet
Set rng = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row) ' <---- ADJUST THIS
End With
For Each cell In rng
If InStr(UCase(cell), "JPG") > 0 Then '<--- ONLY USES JPG'S
With cell.Offset(0, 1)
On Error Resume Next
ActiveSheet.Shapes.AddPicture cell, msoFalse, msoTrue, .Left + (.Width - 10) / 2, .Top + (.Height - 10) / 2, 20, 20
If Err.Number = 1004 Then Debug.Print "File not found: " & cell
On Error GoTo 0
End With
End If
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub
You will need to use On Error Resume Next, but only for the single statement that inserts the picture. And you should get rid of the Select. The Pictures.Insert-method returns the reference to the inserted image, assign this to a variable and work with that.
Additionally, I would suggest to split your code and create a routine that insert one image into a cell. Call this routine from the loop. I have implemented it as a function that returns True if it was successfull, it's up to you to decide if you want to do something if it returns False.
Function TryInsertImg(filename As String, cell As Range) As Boolean
Dim p As Picture
On Error Resume Next
Set p = cell.Parent.Pictures.Insert(filename)
If Err.Number > 0 Then Debug.Print "Couldn't insert image " & Err.Number & "-" & Err.Description
On Error GoTo 0
If p Is Nothing Then
Exit Function
End If
Dim theShape As Shape
Set theShape = p.ShapeRange.Item(1)
With theShape
.LockAspectRatio = msoFalse
.Width = 20
.Height = 20
.Top = cell.Top + (cell.Height - .Height) / 2
.Left = cell.Left + (cell.Width - .Width) / 2
End With
TryInsertImg = True
End Function
Your calling routine could look like this:
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then '<--- ONLY USES JPG'S
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
If Not TryInsertImg(filename, xRg) then
xRg = "(error loading image)"
End If
End If
Next cell
I'd like to show a pic loaded from network location next to each non empty cell in range A1:A10 and name the picture after the value in that range.
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
On Error GoTo ErrNoPhoto
pictureloc = "location here" & Cell & ".jpg"
With Cell.Offset(0, 1)
Set mypict = ActiveSheet.Pictures.Insert(pictureloc)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell
mypict.OnAction = "enlarge"
End With
Exit Sub
ErrNoPhoto:
MsgBox "Unable to Find Photo for " & Cell
End If
Next Cell
End Sub
So far I came up with the code above, but it only shows the pic behind A1 if I run it.
What am I doing wrong/missing in my code?
The main issue is that you exit the loop after the first iteration. However, I refactored your code below to make it more stable, readable and better actual error trapping.
Option Explicit
Sub testpics()
Dim Cell As Range
For Each Cell In Range("A1:A10").Cells
If Not IsEmpty(Cell) Then
Dim pictureLoc As String
pictureLoc = "location here" & Cell & ".jpg"
Dim mypict As Object
On Error Resume Next
Set mypict = ActiveSheet.Pictures.Insert(pictureLoc)
On Error GoTo 0
If Not mypict Is Nothing Then
With Cell.Offset(0, 1)
mypict.Height = .RowHeight
mypict.Left = .Left
mypict.Top = .Top
mypict.Placement = xlMoveAndSize
mypict.Name = Cell.Value
mypict.OnAction = "enlarge"
End With
Else
Cell.Offset(0, 1).Value = "Could Not Find Pic"
End If
End If
Next
End Sub
I am looking for a way to insert text into the background of a cell, so that I can still enter numbers on top of that text - similar to a watermark except for an individual cell. Any ways to do this, preferably without using a macro (but open to these solutions as well)?
Similar to Andrews post, this is the VBA version which formats the shape correctly and also allows direct selecting of cells.
Code MODULE:
Sub watermarkShape()
Const watermark As String = "watermark"
Dim cll As Range
Dim rng As Range
Dim ws As Worksheet
Dim shp As Shape
Set ws = Sheet1
Set rng = ws.Range("A1:F10") 'Set range to fill with watermark
Application.ScreenUpdating = False
For Each shp In ws.Shapes
shp.Delete
Next shp
For Each cll In rng
Set shp = ws.Shapes.AddShape(msoShapeRectangle, 5, 5, 5, 5)
With shp
.Left = cll.Left
.Top = cll.Top
.Height = cll.Height
.Width = cll.Width
.Name = cll.address
.TextFrame2.TextRange.Characters.Text = watermark
.TextFrame2.TextRange.Font.Name = "Tahoma"
.TextFrame2.TextRange.Font.Size = 8
.TextFrame2.VerticalAnchor = msoAnchorMiddle
.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
.TextFrame2.WordWrap = msoFalse
.TextFrame.Characters.Font.ColorIndex = 15
.TextFrame2.TextRange.Font.Fill.Transparency = 0.35
.Line.Visible = msoFalse
' Debug.Print "'SelectCell (""" & ws.Name & """,""" & cll.address & """)'"
.OnAction = "'SelectCell """ & ws.Name & """,""" & cll.address & """'"
With .Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.Transparency = 1
.Solid
End With
End With
Next cll
Application.ScreenUpdating = True
End Sub
Sub SelectCell(ws, address)
Worksheets(ws).Range(address).Select
End Sub
UPDATE:
the example below assigns a watermark of the cell address to odd rows and leaves the even rows as the constant watermark. This is an exaple based on my comment that any cell can be assigned any watermark text based on whatever conditons you want.
Option Explicit
Sub watermarkShape()
Const watermark As String = "watermark"
Dim cll As Range
Dim rng As Range
Dim ws As Worksheet
Dim shp As Shape
Set ws = Sheet1
Set rng = ws.Range("A1:F10") 'Set range to fill with watermark
Application.ScreenUpdating = False
For Each shp In ws.Shapes
shp.Delete
Next shp
For Each cll In rng
Set shp = ws.Shapes.AddShape(msoShapeRectangle, 5, 5, 5, 5)
With shp
.Left = cll.Left
.Top = cll.Top
.Height = cll.Height
.Width = cll.Width
.Name = cll.address
If cll.Row Mod 2 = 1 Then
.TextFrame2.TextRange.Characters.Text = cll.address
Else
.TextFrame2.TextRange.Characters.Text = watermark
End If
.TextFrame2.TextRange.Font.Name = "Tahoma"
.TextFrame2.TextRange.Font.Size = 8
.TextFrame2.VerticalAnchor = msoAnchorMiddle
.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
.TextFrame2.WordWrap = msoFalse
.TextFrame.Characters.Font.ColorIndex = 15
.TextFrame2.TextRange.Font.Fill.Transparency = 0.35
.Line.Visible = msoFalse
' Debug.Print "'SelectCell (""" & ws.Name & """,""" & cll.address & """)'"
.OnAction = "'SelectCell """ & ws.Name & """,""" & cll.address & """'"
With .Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.Transparency = 1
.Solid
End With
End With
Next cll
Application.ScreenUpdating = True
End Sub
Sub SelectCell(ws, address)
Worksheets(ws).Range(address).Select
End Sub
You can use a custom number format (select the cell(s), hit Ctrl+1, number formats, custom) to specify a light-grey text to display when the cell value is 0 - Color15 makes a nice watermark color:
[Black]000000;;[Color15]"(order number)";#
No messy shapes, no VBA, and the watermark disappears when the value is actually filled up.
And if you absolutely need to do it in VBA, then you can easily write a function that builds the format string based on some parameters:
Public Function BuildWatermarkFormat(ByVal watermarkText As String, Optional ByVal positiveFormat As String = "General", Optional ByVal negativeFormat As String = "General", Optional ByVal textFormat As String = "General") As String
BuildWatermarkFormat = positiveFormat & ";" & negativeFormat & ";[Color15]" & Chr(34) & watermarkText & Chr(34) & ";" & textFormat
End Function
And then you can do:
myCell.NumberFormat = BuildWatermarkFormat("Please enter a value")
myCell.Value = 0
And you can still supply custom formats for positive/negative values as per your needs; the only thing is that 0 is reserved for "no value" and triggers the watermark.
myCell.NumberFormat = BuildWatermarkFormat("Please enter a value", "[Blue]#,##0.00_)", "[Red](#,##0.00)")
myCell.Value = -25
Select the Cell where you want to make the Background.
Click "Insert" and insert a rectangular Shape in that location.
Right click on the shape - select "Format Shape"
Goto "Fill" and select "Picture or texture fill"
Goto “Insert from File” option
Select the picture you want to make water-mark
Picture will appear at the place of rectangular shape
Now click on the picture “right click” and select Format Picture
Goto “Fill” and increase the transparency as required to look it like a “Water Mark” or light beckground
This will get printed also.
taken from here
Type your text in a cell anywhere.
Copy it and it will be saved on the clipboard.
Insert a rectangular shape anywhere.
Right click and choose "Send to back".
This will make sure it will be at the background.
Right click and "Format Shape".
Do to tab "Fill" and click on "picture or texture fill".
At the "insert from" choose "clipboard".
Now whatever text you have copied onto your clipboard will be in the rectangular shape.
Resize the shape to fit the cell(s) you desired.
Adjust however you like for example remove the rectangular lines, add shadow, change font, remove background etc.