Excel工作表索引通过VBA宏中的唯一编号标识吗?

时间:2018-11-12 14:11:45

标签: excel vba excel-vba

下面两个用于检查清单和模板的工作宏

工作簿=史蒂夫 工作簿中的工作表已分配15286

我需要向宏中添加工作表,并且需要知道如何定位我认为唯一的_number,以便可以在Same工作簿中的新数据表上执行相同的例程

工作簿包含相同的宏,唯一的不同是数倍

例如:清单=

Sub PublishChecklists()
'
' Saves htm version of Sheet on Server
'
Range("M8:M2000").Select
Selection.Font.ColorIndex = 0
Selection.autofilter Field:=7, Criteria1:=Array("<>Exc")
Columns("A:J").Select
Selection.EntireColumn.Hidden = True
With ActiveWorkbook.PublishObjects("Steve_**15826**")
.Title = "CHECKLIST LIST"
.Filename = "\\f:\data\Work\Checklists.htm"
.Publish (True)
.AutoRepublish = False
End With
ChDir "F:\data\Work\Today's Work"
Columns("A:J").Select
Selection.EntireColumn.Hidden = False
Selection.autofilter Field:=7
Columns("H:H").Select
Selection.EntireColumn.Hidden = True
Range("L8").Select

End Sub

模板=

Sub PublishTemplates()
'
' Saves htm version of Sheet on Server
'
Range("M8:M2000").Select
Selection.Font.ColorIndex = 0
Selection.autofilter Field:=7, Criteria1:=Array("<>Exc")
Columns("A:J").Select
Selection.EntireColumn.Hidden = True
With ActiveWorkbook.PublishObjects("Steve_**3496**")
.Title = "TEMPLATES LIST"
.Filename = "\\f:\data\Work\Templates.htm"
.Publish (True)
.AutoRepublish = False
End With
ChDir "F:\data\Work\Today's Work"
Columns("A:J").Select
Selection.EntireColumn.Hidden = False
Selection.autofilter Field:=7
Columns("H:H").Select
Selection.EntireColumn.Hidden = True
Range("L8").Select

End Sub

-

两项工作都唯一不同的是“ 15286”和“ 3486”,因为它们是不同的纸张? 如果我要添加新工作表,我需要知道如何定位“ 3486”数字位,因为对于新工作表它将是一个不同的数字

我继承了代码,不知道如何找到它们以便将它们放置在上面的代码中

非常感谢您的光临

史蒂夫

1 个答案:

答案 0 :(得分:0)

只需调用相同的过程并将数字作为参数传递给它即可。

您的过程将类似于:

Sub PublishLists(RefNumber As Long, Title As String)

'...
'...
'...
        With ActiveWorkbook.PublishObjects("Steve_**" & RefNumber & "**")
            .Title = Title & " LIST"

'...
'...
'...

End Sub

然后您将调用该过程并将其编号和标题传递给它:

Sub Test()

    PublishLists 15826, "CHECKLIST"
    PublishLists 3496, "TEMPLATES"

End Sub  

修改:
我尚未测试您的任何代码-刚刚进行了调整,可以接受传递的参数并删除任何显示SelectSelection的内容。
这里的Test过程将每次使用不同的参数执行PublishLists两次。

Public Sub Test()

    'The three values are passed to the "PublishLists" procedure.
    'First line would PublishObjects to "Steve_**15826**" and second to "Steve_**3496**"

    PublishLists 15826, "CHECKLIST ", "Checklists.htm"
    PublishLists 3496, "TEMPLATES", "Templates.htm"

End Sub

Sub PublishLists(RefNumber As Long, Title As String, SaveFile As String)

    With ThisWorkbook.Worksheets("Sheet1") 'Update sheet name as required.
        With .Range("M8:M2000")
            .Font.ColorIndex = 0
            .AutoFilter Field:=7, Criteria1:=Array("<>Exc")
        End With
        .Columns("A:J").Hidden = True
    End With

    With ThisWorkbook.PublishObjects("Steve_**" & RefNumber & "**") 'RefNumber variable passed to name.
        .Title = Title & " LIST" 'Title variable passed to title.
        .Filename = "\\f:\data\Work\" & SaveFile 'SaveFile variable passed to FileName
        .Publish (True)
        .AutoRepublish = False
    End With

    With ThisWorkbook.Worksheets("Sheet1") 'Update sheet name as required.
        With .Columns("A:J")
            .Hidden = False
            .AutoFilter Field:=7
        End With
        .Columns("H:H").Hidden = True
        .Range("L8").Select 'The only cell that is selected.
    End With

End Sub