我可以使用什么函数来完善复制粘贴公式forms的其他工作簿表

我从离线工作表工作表复制粘贴公式

Private Sub CommandButton9_Click() Workbooks.Open Filename:= _ "C:\Report\2017\february\data february\STOCK_REPORT_16_02_" & Year(Date) & ".xls" Range("A1:H1000").Select Selection.Copy Windows("MGM FEBRUARI.xlsm").Activate Sheets("STOCK16").Activate Range("A2.H2").Select ActiveSheet.Paste Windows("STOCK_REPORT_16_02_" & Year(Date) & ".xls").Activate Application.CutCopyMode = False Application.DisplayAlerts = False ActiveWindow.Close UserForm3.Hide Range("A2").Select Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

通过应用@Shai Rado提供的“&Year(Date)&”,我可以在不改变年份的情况下进行配方

但月份呢? 如何使用一个适当的function月份,把它放在我的公式?不改变date只有月份和年份

  1. 为每个放置函数; 月和年
  2. 有没有单一的function呢

您可以使用Format和工作簿variables:

 Private Sub CommandButton9_Click() Dim wB As Workbook Set wB = Workbooks.Open(filename:= _ "C:\Report\2017\february\data february\STOCK_REPORT_16_" & Format(Date, "mm_yyyy") & ".xls") wB.Sheets(1).Range("A1:H1000").Copy Workbooks("MGM FEBRUARI.xlsm").Sheets("STOCK16").Range("A2.H2").Paste Application.CutCopyMode = False Application.DisplayAlerts = False wB.Close Application.DisplayAlerts = True UserForm3.Hide Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

或者用可变path:

 Private Sub CommandButton9_Click() Dim wB As Workbook Dim DatDate As Date Dim wBPath As String DatDate = Date wBPath = "C:\Report\" & Format(DatDate, "yyyy\MMMM") & "\data " & Format(DatDate, "MMMM") & "\STOCK_REPORT_16_" & Format(DatDate, "mm_yyyy") & ".xls" Set wB = Workbooks.Open(filename:=wBPath) wB.Sheets(1).Range("A1:H1000").Copy Workbooks("MGM FEBRUARI.xlsm").Sheets("STOCK16").Range("A2.H2").Paste Application.CutCopyMode = False Application.DisplayAlerts = False wB.Close Application.DisplayAlerts = True UserForm3.Hide Application.ScreenUpdating = True Application.EnableEvents = True End Sub