VBA Code For Excel ..........................
Solved/Closed
Related:
- VBA Code For Excel ..........................
- Battery reset code - Guide
- Samsung volume increase code - Guide
- Usa country code for whatsapp - Guide
- Number to words in excel formula without vba - Guide
- Cs 1.6 code - Guide
2 responses
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Apr 20, 2010 at 07:22 AM
Apr 20, 2010 at 07:22 AM
Santosh, all you need is an else statement before the last ENDIF. Here is you whole modified code. New lines are highlighted
Sub ProcessFiles()
Dim sPath As String, s As String, r As Range
Dim shp As ShapeRange
Dim c As Range, cell As Range, sname As String
Dim p As Picture, diffwidth As Double, diffHeight As Double
sPath = "D:\2010I\Excel Pic\Complete Range _2010I"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
Set r = Range("B2", Cells(Rows.Count, 2).End(xlUp))
For Each cell In r
cell.Offset(0, 1).Select
Set c = cell.Offset(0, -1)
s = sPath & cell.Value & ".jpg" 'remove the .jpg if the cell contains the extension
sname = Dir(s)
If sname <> "" Then
Set p = ActiveSheet.Pictures.Insert(s)
Set shp = p.ShapeRange
diffwidth = c.Width - p.Width
If diffwidth > 0 Then
p.Left = c.Left + 0.5 * diffwidth
Else
p.Left = c.Left
End If
diffHeight = c.Height - p.Height
If diffHeight > 0 Then
p.Top = c.Top + 0.5 * diffHeight
Else
p.Top = c.Top
End If
Else
c.Value = "Picture is not available"
End If
Next
End Sub