Home » excel » excel – VBA notation – backslash in quotations

excel – VBA notation – backslash in quotations

Posted by: admin May 14, 2020 Leave a comment

Questions:

A colleague at work made a macro and I need to edit it, but first I need to figure out how it works.

    Sheet2.Pictures.Insert(importPath & "\" & partName & "\" & picName & ".png").Select

This line is (I think) where images are inserted into the excel document. I’m trying to figure out exactly how it works. I know importpath, partName and picName are variables he defined earlier. importPath is self explanatory, I don’t know why part name is in there but I know what it is and picName is there because you enter the picture name in a certain cell and it searches for that name in the importPath to insert into the sheet.

Also what confuses me is the & and “\” I have no idea what these do.

Any help out there?

How to&Answers:

This is compiling a group of variables into a String that represents a path. For example, if:

importPath = "myPics"
partName = "2014"
picName = "flower"

Then the line

importPath & "\" & partName & "\" & picName & ".png"

will create the String

"myPics14\flower.png"

which is the full filename being passed to Sheet2.Pictures.Insert

Answer:

You’re constructing a full file path to an image in that line so the & concatenates all the string variables and the "\" is just manually adding the backslash characters you’d expect to see in any windows explorer or command line window.