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

重新说明我的问题:

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") 

path是单元格中的date。 当这个单元格更改时,我希望目录根据path进行更改。

两件事情

  1. 声明path为string,而不是范围。
  2. 在path中使用之前,请在date中replace“\”

这是你正在尝试?

 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")