Excel VBA商店图纸索引编号供以后使用

时间:2017-01-13 22:07:04

标签: excel excel-vba vba

我使用此代码删除活动工作表,然后复制另一个工作表,如何存储活动工作表索引编号,以便我可以使用它将复制的工作表移动到存储的索引编号?

Master_Work_Order.Visible = True


Dim oldSheetName As String
Dim Ws As Worksheet
Dim oldTabColor
Dim oldSheetIndexNumber

'Store
oldSheetName = ActiveSheet.NAme
oldTabColor = ActiveSheet.Tab.Color
' oldSheetIndexNumber = Store active sheet index number

ActiveSheet.Delete

Master_Work_Order.Copy After:=Sheets(ActiveSheet.Index)
Set Ws = ActiveSheet

'set new copied sheet name to previous ActiveSheet's name

Ws.NAme = oldSheetName
Ws.Tab.Color = oldTabColor
' Move active sheet to stored index number


Master_Work_Order.Visible = 2 'Makes sheet very hidden

1 个答案:

答案 0 :(得分:2)

如果我了解你的情况,这应该有效。我只是以有限的方式测试它。 .Copy方法应该将新工作表放在正确的位置。

Dim Ws As Worksheet
Dim oldTabColor
Dim oldSheetIndexNumber As Integer

'Store
oldSheetName = ActiveSheet.Name
oldTabColor = ActiveSheet.Tab.Color
oldSheetIndexNumber = ActiveSheet.Index

ActiveSheet.Delete

If ActiveWorkbook.Sheets.Count >= oldSheetIndexNumber Then
    ActiveWorkbook.Sheets("Sheet2").Copy before:=Sheets(oldSheetIndexNumber)
Else
    ActiveWorkbook.Sheets("Sheet2").Copy after:=Sheets(oldSheetIndexNumber - 1)
End If
Set Ws = ActiveSheet

'set new copied sheet name to previous ActiveSheet's name

Ws.Name = oldSheetName
Ws.Tab.Color = oldTabColor