给定范围后的命名表

时间:2013-12-03 10:15:24

标签: excel vba excel-vba

我有一些代码,我坚持一个解决方案,并想知道你是否可以帮助我:

Private Sub CommandButton2_Click()
Dim wsn As Range
Set wsn = Worksheets("Formula Sheet").Range("C1:C26")
Worksheets("Sheet1").Select
ActiveSheet.Next.Select
Do Until ActiveSheet.Name = "Summary Sheet"
ActiveSheet.UnprotectPassword:="admin"
ActiveSheet.Name = (wsn + x)
x = x + 1
With Worksheets(ActiveSheet.Name)
.Protect Password:="admin", userinterfaceonly:=True
.EnableOutlining = True
.EnableAutoFilter = True
End With
ActiveSheet.Next.Select
Loop
Worksheets("Sheet1").Select
End Sub

我试图实现的是一种在工作表1之后但在汇总到我的公式表中列出的值范围(命名表中的C1:C26)之前命名我的所有工作表的方法,我已将x作为变量但是我希望它命名第二张C1表3 C2等等,如何实现一系列值的选择下一个功能(C1:C26),如果选择了这个值,则不再使用。

希望这很清楚:)

1 个答案:

答案 0 :(得分:0)

试试这个:

x = 1    
Do Until ActiveSheet.Name = "Summary Sheet"
ActiveSheet.UnprotectPassword:="admin"
ActiveSheet.Name = wsn.Cells(x, 1)
x = x + 1

'etc.

编辑以回应以下评论:

试试这个:

Private Sub CommandButton2_Click()
Dim wsn As Range
Set wsn = Worksheets("Formula Sheet").Range("C1:C26")
set ws = Worksheets(Worksheets("Sheet1").Index + 1)
Do Until ws.Name = "Summary Sheet"
ws.UnprotectPassword:="admin"
ws.Name = wsn.Cells(x, 1)
x = x + 1
With ws
.Protect Password:="admin", userinterfaceonly:=True
.EnableOutlining = True
.EnableAutoFilter = True
End With
set ws = Worksheets(ws.Index + 1)
Loop
Worksheets("Sheet1").Select
End Sub