0
我在excel中記錄了一個宏。我已經用excel做這個每週產生報告。列的位置和數量保持不變。但是,行數每週都在變化。文件名稱和圖紙名稱更改方式相同。如何在excel 2013下面記錄的宏中設置活動工作表爲變量?
此錄製的宏記錄名爲「2017_08_13」的紙張。我必須每次都在此宏中手動更改工作表名稱。同樣的方式單元格選擇轉到行號101.每次我必須手動輸入最後一個行號才能使其工作。任何人都可以幫助我使這兩件事情自動適用於每張紙。 1)它將在activesheet上運行,而不是表格名稱,我們可以使其與活動表一起使用。 2)它應該上到最後一個單元格來選擇。在第17行(我猜)它指向第101行。
Sub Macro1()
'
' Macro1 Macro
'
'
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Select
ActiveCell.FormulaR1C1 = "Decimal"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*24"
Range("E2").Select
Selection.Copy
Range("D2").Select
Selection.End(xlDown).Select
Range("E101").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-105
Application.CutCopyMode = False
Selection.NumberFormat = "General"
ActiveWindow.SmallScroll Down:=-126
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("A1:F1").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Columns("F:F").EntireColumn.AutoFit
Range("H2").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"2017_08_13!R1C1:R101C6", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="2017_08_13!R2C8", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion15
Sheets("2017_08_13").Select
Cells(2, 8).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("activity")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_
"PivotTable1").PivotFields("Decimal"), "Sum of Decimal", xlSum
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWindow.SmallScroll Down:=-105
End Sub
我不是一個編程人員。 謝謝
我** **強烈推薦通過閱讀[如何避免'.Select' /'。 Activate'](https://stackoverflow.com/questions/10714251/)。至少,你想要做'作爲工作表//設置ws =表格(「mySheetName」)// ws.Range(「A1」)Value = ...'Dim ws以確保你正在訪問「mySheetName」的A1單元格。 – BruceWayne