简体   繁体   English

Excel VBA代码打开文件

[英]Excel VBA code to open a file

I created a macro button to open my daily files from a excel production sheet where I have all the my macro button for specific files. 我创建了一个宏按钮,以从excel生产表中打开我的日常文件,其中我拥有所有用于特定文件的宏按钮。

The format for all my files are conventionally the same: 我所有文件的格式通常都相同:

  1. Businese Unit Name: YMCA 业务单位名称:YMCA
  2. Year:2012 年份:2012
  3. Month: April 月:四月
  4. Week: Week 2 周:第2周
  5. Day: 12 一天:12
  6. File Name: YMC Template 041212.xlsm 文件名:YMC模板041212.xlsm

I am having issue with the last excel file name extension. 我最近的excel文件扩展名有问题。 how do I add the MyDaily Template and MyDateProd along with the .xlsm. 如何添加MyDaily模板和MyDateProd以及.xlsm。 I have this -J:.....\\& myDailyTemplate & myDateProd.xlsm") see below for entire file path names. 我有这个-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

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

since that is what is included in the quotes, but from your code, you appear to have a number of variables that are part of this string, you need to take them out of the quotes and concatenate them together. 由于这是引号中包含的内容,但是在您的代码中,您似乎有很多变量属于该字符串,因此您需要将它们从引号中删除并将它们连接在一起。 Try something like this: 尝试这样的事情:

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

I added the continuation _ to make it more readable onthe screen here, but it is not necessary, you can put everything on one line together if you prefer. 我添加了连续号_,以使其在屏幕上更易读,但不是必须的,您可以根据需要将所有内容放在一行上。

Unless you need all of the myBusinessUnit , myFolderYear , etc variables elsewhere, I would think about doing it in some sort of array and then doing a Join function to concatenate everything. 除非在其他地方需要所有myBusinessUnitmyFolderYear等变量,否则我会考虑以某种数组的形式进行处理,然后执行Join函数来连接所有内容。 I, personally, find this easier to maintain going forward and easier to see the hierarchy in the folder structure rather than looking at a very long string and trying to find what part of the path is wrong. 我个人认为,这比往后看一个很长的字符串并尝试查找路径的哪部分错误更容易维护,也更容易查看文件夹结构中的层次结构。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM