You are on page 1of 1

2/6/2015

ExcelAmacrotoinsertpictures

ExcelAmacrotoinsertpictures
January2015
MacrosinExcelcanperformfunctionssuchasinsertingpicturesatlocations,copyingdatafromonecelltoanotheretc.Someknowledgeofprogramming
conceptssuchaslooping,ifelseconditionetc.mayberequiredtowriteeffectivemacrosthatcanaccomplishvarioustasks.Addingloopsinmacrostoinsert
pictureswillensurethatpicturesareinsertedforalltherowsoftheExcelworksheet.Withoutaddingloopsinmacros,itisdifficulttoperformrepetitiveactions.If
thenumberofiterationsrequiredisnotfinite,thedowhileloopcanbeusedinamacroforinsertingpicturesinExcelOfficeSoftware.
Issue
Solution
Note
Seealso:Vbaexcelimageinsert

Issue
CouldsomeonepleasehelpmeinwritingaloopinthismacroinExcel:
IhaveanExcelsheetwhichcontainsthenameofthepicturesincolumnB.IhavewrittenamacrotopickupthenameofthepictureincolumnBand
inserttheactualpicturefromthedirectoryintocolumnA.IneedtocreatealooptodothisdependingonthenumberofpicturesintheBcolumn.The
numberofrowsvariesfrom10to1000.ThemacrothatIhavewrittenworksperfectlyforthefirstrowbutdoesnotgotothenextrowsinceIamnotsure
howtodoit.PleaseseethemacrothatI'vewritten:
SubPicture()Range("A6").Select'ThisiswherepicturewillbeinsertedDimpicnameAsStringpicname=Range("B6")'ThisisthepicturenameActiveSheet.Pictures.Insert("C:\Users\vbayat\MyDocuments\vidabayat\remarket\"&picname&".jpg").Select'Pathtowherepicturesarestored''''''''''''''''''''''''''''''''''''''''''''''''''''''''Thisresizesthepicture'''''''''''''''''''''''''''''''''''''''''''''''''''''''WithSelection.Left=Range("A6").Left.Top=Range("A6").Top.ShapeRange.LockAspectRatio=msoFalse.ShapeRange.Height=100#.ShapeRange.Width=80#.ShapeRange.Rotation=0#EndWithRange("A10").SelectApplication.ScreenUpdating=TrueExitSubErrNoPhoto:MsgBox"UnabletoFindPhoto"'ShowsmessageboxifpicturenotfoundExitSubRange("B20").SelectEndSub

Solution
Assumptions
1.PicturenamesarefoundincolBstartingatB2
2.Youwanttopasteeachpictureatdifferentlocation
3.WheretopastethepictureidentifiedincolB,isfoundincolCstartingatC2
SubPicture()
DimpicnameAsStringDimpasteAtAsIntegerDimlThisRowAsLonglThisRow=2DoWhile(Cells(lThisRow,2)<>"")'Range("A6").Select'ThisiswherepicturewillbeinsertedpasteAt=Cells(lThisRow,3)Cells(pasteAt,1).Select'Thisiswherepicturewillbeinserted'DimpicnameAsString'picname=Range("B6")'Thisisthepicturenamepicname=Cells(lThisRow,2)'ThisisthepicturenameActiveSheet.Pictures.Insert("C:\Users\vbayat\MyDocuments\vidabayat\remarket\"&picname&".jpg").Select'Pathtowherepicturesarestored''''''''''''''''''''''''''''''''''''''''''''''''''''''''Thisresizesthepicture'''''''''''''''''''''''''''''''''''''''''''''''''''''''WithSelection'.Left=Range("A6").Left'.Top=Range("A6").Top.Left=Cells(pasteAt,1).Left.Top=Cells(pasteAt,1).Top.ShapeRange.LockAspectRatio=msoFalse.ShapeRange.Height=100#.ShapeRange.Width=80#.ShapeRange.Rotation=0#EndWithlThisRow=lThisRow+1LoopRange("A10").SelectApplication.ScreenUpdating=TrueExitSubErrNoPhoto:MsgBox"UnabletoFindPhoto"'ShowsmessageboxifpicturenotfoundExitSubRange("B20").SelectEndSub

Note
Thankstorizvisa1forthistipontheforum.
ThisdocumententitledExcelAmacrotoinsertpicturesfromKioskea(en.kioskea.net)ismadeavailableundertheCreativeCommonslicense.Youcancopy,modifycopiesofthispage,underthe
conditionsstipulatedbythelicense,asthisnoteappearsclearly.

http://en.kioskea.net/faq/6485excelamacrotoinsertpictures

1/1

You might also like