How to use file path from a cell in VBA?

橙三吉。 提交于 2019-12-03 21:02:27

This line strFolder = Dir(Range("C7").Value) finds firts file in directory (from C7) and then writes path of this file into variable strFolder (say, C:\temp\somefile.txt).

Next line of your code: strFile = Dir(strFolder & "*.xlsx") takes this path and adds *.xlsx. In result you would get strFile = Dir("C:\temp\somefile.txt*.xlsx") and that's wrong.

So, change this code:

strFolder = Dir(Range("C7").Value)
strFile = Dir(strFolder & "*.xlsx")

to next one:

strFolder = Range("C7").Value
strFile = Dir(strFolder & "*.xlsx")

Btw, I'd recommend you to specify sheet for Range("C7") like this: wsDest.Range("C7")

Pedrumj

Try this

dim strPath as string
strPath = CurDir + "NameofFile.xls"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!