我的代碼完美的工作,除了它創建一個額外的表格(無論是在序列中的下一個...即「Sheet2」..「Sheet3」...每次我運行它時都可以幫助弄清楚爲什麼會發生這種情況? ...應該只有兩片創建爲我在下面的代碼表明:爲什麼我的VBA代碼在運行時會創建額外的表單?
Sub CopySheet()
Dim NewSheet As String
Dim PrevSheet As String
Dim CashWS As Worksheet
Dim MonthVal As String
NewSheet = InputBox("Which month is this Commissions statement for?")
PrevSheet = InputBox("What was the previous month?")
Worksheets(PrevSheet).Copy After:=Worksheets("Summary")
ActiveSheet.Name = NewSheet
Range("D2").Select
ActiveCell.FormulaR1C1 = "=EOMONTH(DATE(2017,MONTH(DATEVALUE(MID(CELL(""filename"", RC[-5]), FIND(""]"", CELL(""filename"", RC[-5])) + 1, 255)&""1"")+1),1),0)"
Selection.NumberFormat = "m/d/yyyy"
Range("D3").Select
ActiveCell.FormulaR1C1 = "=MONTH(R[-1]C)"
Range("D3").Select
Selection.NumberFormat = "General"
MonthVal = ActiveCell.Value
Set CashWS = Sheets.Add
Sheets.Add.Name = "2017_0" & MonthVal & " Cash"
End Sub
除了下面的答案,值得一提的是最好[避免使用'.Select' /'.Activate'](https://stackoverflow.com/questions/10714251/how-to-avoid-using - 選擇功能於Excel的VBA的宏)。這將有助於縮短/「收緊」你的代碼。 – BruceWayne