2014-12-04 44 views
1

我需要我的代码解决方案,我需要一个“MyDir”从Sheet1.Range(“A1”)读取路径。这是从“MyDir”+图像名称加载图像的代码,但它仅在您为MyDir代码放入代码时才有效,例如:* C:\ Users \ Me \ Desktop *。但我不希望出现这种情况,我想* C:\用户\我\桌面*从Sheet1.Range("A1")读,有人可以帮助我这个代码:如何将范围声明为Path?

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 
+0

您是否尝试过没有'MyDir'周围的引号? – tospig 2014-12-04 02:24:26

+0

是的,如果你想把C:\ Users \ Me \ Desktop \ – 2014-12-04 02:31:14

+0

放到引号去,你不需要引号,因为你已经为变量MyDir指定了路径 – tospig 2014-12-04 02:34:52

回答

1

您不需要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 
0

变量MYDIR是在引号。

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工作,但代码的莱恩(...)部分做工作,所以剩下的只是读取图片。

+0

你需要指定你想要的图片'Image1'的属性,即'Image1.Picture' – tospig 2014-12-04 02:40:28