如何将范围声明为Path?

我需要为我的代码解决scheme,我需要一个“MyDir”从Sheet1.Range(“A1”)读取path。 这是从“MyDir”+图像名称加载图像的代码,但只有当您为MyDir代码放入代码时才有效:* C:\ Users \ Me \ Desktop *。 但我不想这样,我想* C:\ Users \ Me \ Desktop *从Sheet1.Range("A1")读取,有人可以帮助我这个代码:

 Dim MyDir As Range Set MyDir = Sheet1.Range("A1") If Len(Dir("MyDir" & TextBox1.Text & ".jpg")) > 0 Then Image1.Picture = LoadPicture("MyDir" & TextBox1.Text & ".jpg") Else Image1.Picture = LoadPicture("") End If 

你不需要MyDir的引号。

我也使用Dir() <> ""来检测文件的存在

这里我的图像被称为“code_pic.jpg”。

 Sub image() Dim MyDir As Range Set MyDir = Sheet1.Range("A1") If Dir(MyDir) <> "" Then Set Sheet1.Image1.Picture = LoadPicture(MyDir & "\code_pic.jpg") Else Set Sheet1.Image1.Picture = LoadPicture("") End If End Sub 

variablesMyDir在引号中。

 Sub test() Dim fullpath_and_name As String Dim MyDir As Range Set MyDir = Sheet1.Range("A1") fullpath_and_name = MyDir & "\" & "test.bmp" Debug.Print Dir(fullpath_and_name) If Len(Dir(MyDir & "\" & "test.bmp")) > 0 Then Image1 = LoadPicture(MyDir & "\" & "test.bmp") Else Image1 = LoadPicture("") End If End Sub 

我无法让Image1.Picture工作,但Len(…)部分的代码确实有效,所以剩下的只是读取图片。