2017-03-22 21 views
0

我试图在当前月份的文件夹中的文件上运行以下宏。我有一个宏工作,它将当前月份的文件夹定位到共享驱动器位置,并成功调用此宏。然而,下面的宏指定3月,但我正在寻找一种方法来指定“当前月”,所以我不必每个月都改变宏。 (此宏只是转换的.csv原来的.xlsx,但仅用于测试目的了。)在文件路径中指定当前月份,防止每月更改宏

Sub Reject_Review() 



ActiveWorkbook.Worksheets.Add 
With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;G:\Rejects\2017\March\Test_3.csv" _ 
    , Destination:=Range("$A$1")) 
    .Name = "Test" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 437 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = False 
    .TextFileOtherDelimiter = "|" 
    .TextFileColumnDataTypes = Array(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ 
    , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
    1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 


End With 

End Sub  
+0

Month(Now)。不过,请注意格式。 – Cyril

回答

0

此功能在VBA工作对我来说:

Function GetMonth() As String 

    GetMonth = Application.WorksheetFunction.Text(Date, "mmmm") 
End Function 
1

使用VBA Format关键字这

根据包含在格式表达式中的指令返回包含格式为 的表达式的变体(字符串)..

格式(表达式[,格式[,Firstdayofweek可[,firstweekofyear]]])
https://msdn.microsoft.com/en-us/library/office/gg251755.aspx

不要忘记你一年需要更新也是如此。

"TEXT;G:\Rejects\" & Year(Date) & "\" & Format(Date, "mmmm") & "\Test_3.csv" 
+0

谢谢绅士! – Eric

相关问题