使用VBA代码中的单元格数据打开目录

时间:2012-10-25 19:51:56

标签: excel vba excel-vba excel-2007

重写我的问题:

Sub Path()
Dim path As Range
Dim shPivot As Worksheet
Set shPivot = ActiveWorkbook.Sheets("Pivot")
Set path = shPivot.Range("E12").Value
Set wkbFrom = Workbooks.Open("S:\_Supply Chain\Weekly Rpts Supplier and Buyer\" & path & "\SUPPLIER_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls")

路径是单元格中的日期。当此单元格更改时,我希望目录根据路径更改。

1 个答案:

答案 0 :(得分:3)

两件事

  1. 将路径声明为字符串而不是范围。
  2. 在路径
  3. 中使用之前的日期替换“\”

    这是你在尝试的吗?

    Dim path As String
    path = shPivot.Range("E12").Value
    Set wkbFrom = Workbooks.Open("S:\_Supply Chain\Weekly Rpts Supplier and Buyer\" & _
                  format(path,"DD-MM-YYYY") & _
                  "\SUPPLIER_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls")
    

    <强>后续

    在这种情况下使用此

    Dim path As String
    path = shPivot.Range("E12").Value
    Set wkbFrom = Workbooks.Open("S:\_Supply Chain\Weekly Rpts Supplier and Buyer\" & _
                  path & _
                  "\SUPPLIER_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls")
    

    如果有任何不需要的空格,则必须使用TRIM

    Dim path As String
    path = shPivot.Range("E12").Value
    Set wkbFrom = Workbooks.Open("S:\_Supply Chain\Weekly Rpts Supplier and Buyer\" & _
                  Trim(path) & _
                  "\SUPPLIER_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls")