Excel 如何在文件路径字符串中使用分配给变量的单元格值?

Excel 如何在文件路径字符串中使用分配给变量的单元格值?,excel,vba,Excel,Vba,我试图在一个不断变化的位置打开Excel电子表格。变量是日期和文件名: C:\Reports\2019\09。2019年9月\2019年9月10日\1。客户报告\Report\u 20190911.csv 我尝试过使用变量,引用工作簿中的特定单元格来添加每天、每月和每年都在更改的日期 例如: 今天-C:\Reports\2019\09。2019年9月\2019年9月10日\1。客户报告\Report\u 20190911.csv 明天-C:\Reports\2019\09。2019年9月\201

我试图在一个不断变化的位置打开Excel电子表格。变量是日期和文件名:

C:\Reports\2019\09。2019年9月\2019年9月10日\1。客户报告\Report\u 20190911.csv

我尝试过使用变量,引用工作簿中的特定单元格来添加每天、每月和每年都在更改的日期

例如:

今天-C:\Reports\2019\09。2019年9月\2019年9月10日\1。客户报告\Report\u 20190911.csv

明天-C:\Reports\2019\09。2019年9月\2019年9月11日\1。客户报告\Report\u 20190912.csv

我似乎无法添加引用的excel单元格的图像,因此我将对其进行描述(所有单元格都是日期公式驱动的):

下面是我的代码:

Sub Client_Reports()

Dim year_ As Integer
year_ = Cells(6, 2)

Dim month_ As String
month_ = Cells(7, 2)

Dim date_ As Date
date_ = Cells(9, 2)

Dim FilePath As String
Dim FileName As String

FileName = "Report_" & Format(Range("B4") + 1, "yyyymmdd") & ".csv"

FilePath = "C:\Reports\year_\month_\date_\1. Client reports\FileName"

Workbooks.Open (FilePath)

End Sub
它搜索C:\Reports\year\uuu\month\uu\date\u1。客户端报告\Report_20190911.csv

我就是这样做的:

Option Explicit
Sub Client_Reports()

    Dim MyDate As Date
    MyDate = ThisWorkbook.Sheets("MySheet").Range("B4") 'change MySheet to the sheet name holding the date

    Dim MonthName As String
    MonthName = Format(MyDate, "mmmm") 'this takes the month name

    Dim MonthNumber As String
    MonthNumber = Format(MyDate, "mm") 'this takes the month number with 2 digits

    Dim DayNumber As String
    DayNumber = Format(MyDate, "dd") 'this takes the day number with 2 digits, switch to "d" for 1 digit.

    Dim FilePath  As String
    FilePath = "C:\Reports\" & Year(MyDate) & "\" & MonthNumber & ". " & MonthName & "\" & DayNumber & " " & MonthName & _
        " " & Year(MyDate) & "\1. Client reports\"

    Dim FileName
    FileName = "Report_" & Format(MyDate, "yyyymmdd") & ".csv"

    Dim wb As Workbook

    Set wb = Workbooks.Open(FilePath & FileName)

End Sub

这样,您只需使用单元格B4即可完成所有其他操作。

如评论中所述,您应编写以下内容:

FilePath=“C:\Reports\”&year\u&“\”&month\u&“\”&date\u&“\ 1.客户端报告\”&FileName


当您想要构建字符串表达式时,千万不要将变量名放在双引号内。这就像以字符串形式键入其名称,您没有引用实际变量。

将变量移到引号之外,并用符号连接:
FilePath=“C:\Reports\”&year&“\”&month\…
问题的答案在
FileName=…
行中找到。您需要从引号中删除vba变量,并将它们重新连接到引号中,就像您在那一行中所做的那样。我现在已经成功地用日期变量获取了文件的源代码。但是,我通过VBA打开的文件的格式似乎有问题。例如,如果我正常打开该文件,它有一个日期为dd/mm/yyyy的列,但当使用宏打开时,它将更改为mm/dd/yyyy。这可能是因为我试图打开的文件是csv吗?如果不看到它,很难判断,但这似乎与您的系统中日期的格式和vba(美国系统)中日期的格式不同有关。
Option Explicit
Sub Client_Reports()

    Dim MyDate As Date
    MyDate = ThisWorkbook.Sheets("MySheet").Range("B4") 'change MySheet to the sheet name holding the date

    Dim MonthName As String
    MonthName = Format(MyDate, "mmmm") 'this takes the month name

    Dim MonthNumber As String
    MonthNumber = Format(MyDate, "mm") 'this takes the month number with 2 digits

    Dim DayNumber As String
    DayNumber = Format(MyDate, "dd") 'this takes the day number with 2 digits, switch to "d" for 1 digit.

    Dim FilePath  As String
    FilePath = "C:\Reports\" & Year(MyDate) & "\" & MonthNumber & ". " & MonthName & "\" & DayNumber & " " & MonthName & _
        " " & Year(MyDate) & "\1. Client reports\"

    Dim FileName
    FileName = "Report_" & Format(MyDate, "yyyymmdd") & ".csv"

    Dim wb As Workbook

    Set wb = Workbooks.Open(FilePath & FileName)

End Sub