1

我创建了一个宏按钮来从 excel 生产表中打开我的日常文件,其中我有特定文件的所有宏按钮。

我所有文件的格式通常是相同的:

  1. 业务单位名称:基督教青年会
  2. 年份:2012
  3. 月份:四月
  4. 周:第 2 周
  5. 天数:12
  6. 文件名称:YMC模板041212.xlsm

我遇到了最后一个 excel 文件扩展名的问题。如何添加 MyDaily 模板和 MyDateProd 以及 .xlsm。我有这个 -J:.....\& myDailyTemplate & myDateProd.xlsm") 请参阅下面的完整文件路径名。

Sub Open_DailyProd()

    Dim myFolderYear As String
    Dim myFolderMonth As String
    Dim myFolderWeek As String
    Dim myFolderDaily As String
    Dim myDateProd As String
    Dim myBusinessUnit As String
    Dim myDailyTemplate As String

    myBusinessUnit = Sheet1.Cells(32, 2)
    myFolderYear = Sheet1.Cells(11, 2)
    myFolderMonth = Sheet1.Cells(12, 2)
    myFolderWeek = Sheet1.Cells(13, 2)
    myFolderDaily = Sheet1.Cells(14, 2)
    myDateProd = Sheet1.Cells(15, 2)
    myDailyTemplate = Sheet1.Cells(6, 5)

    Application.Workbooks.Open ("J:\IAS\3CMC05HA01\IAC Clients\myBusinessUnit\myFolderYear\myFolderMonth\myFolderWeek\myFolderDaily\& myDailyTemplate & myDateProd.xlsm")

End Sub
4

1 回答 1

5

Excel 正在寻找一个名为: "J:\IAS\3CMC05HA01\IAC Clients\myBusinessUnit\myFolderYear\myFolderMonth\myFolderWeek\myFolderDaily\& myDailyTemplate & myDateProd.xlsm"

因为这是引号中包含的内容,但是从您的代码中,您似乎有许多变量是该字符串的一部分,您需要将它们从引号中取出并将它们连接在一起。尝试这样的事情:

"J:\IAS\3CMC05HA01\IAC Clients\" & myBusinessUnit & "\" & myFolderYear _
& "\" & myFolderMonth & "\" & myFolderWeek & "\" & myFolderDaily & _
"\" & myDailyTemplate & myDateProd & ".xlsm"

我在此处添加了延续 _ 以使其在屏幕上更具可读性,但这不是必需的,如果您愿意,可以将所有内容放在一行中。

除非您在其他地方需要所有myBusinessUnit, myFolderYear, etc 变量,否则我会考虑在某种数组中执行它,然后执行一个Join函数来连接所有内容。我个人觉得这更容易维护,更容易看到文件夹结构中的层次结构,而不是查看一个很长的字符串并试图找出路径的哪一部分是错误的。

Sub Open_DailyProd()

    Dim pathParts(1 To 10) As String
    Dim path As String

    pathParts(1) = "J:"
    pathParts(2) = "IAS"
    pathParts(3) = "3CMC05HA01"
    pathParts(4) = "IAC Clients"
    pathParts(5) = Sheet1.Cells(32, 2)
    pathParts(6) = Sheet1.Cells(11, 2)
    pathParts(7) = Sheet1.Cells(12, 2)
    pathParts(8) = Sheet1.Cells(13, 2)
    pathParts(9) = Sheet1.Cells(14, 2)
    pathParts(10) = Sheet1.Cells(6, 5) & Sheet1.Cells(15, 2) & ".xlsm"

    path = Join(pathParts, "\")

    Application.Workbooks.Open (path)

End Sub
于 2012-04-13T18:16:17.410 回答