如何使用With选择最后一个工作表

时间:2017-10-12 07:44:09

标签: excel vba excel-vba

我的代码看起来像这样:

malloc

由于工作表名称总是不同,我想删除特定名称,即" 10月6日"并指向此处的最后一个工作表。任何人都可以帮我解决这个问题吗?

感谢您的提前帮助

2 个答案:

答案 0 :(得分:1)

您可以使用

轻松选择工作簿中的最后一个工作表
Workbooks("Name of your Workbook").Worksheets(Worksheets.Count)

例如

With Workbooks("Book1.xlsm").Worksheets(Worksheets.Count)
    'Do whatever you want
End With

如何在代码中使用它

我相信您在代码中尝试做的事情就像这样做

With ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    'Do whatever you want
End With

感谢@ YowE3K和@ChrisNeilsen在评论中的贡献

答案 1 :(得分:0)

要访问工作簿的最后一张,请使用以下命令:

Worksheets(Worksheets.Count).Select

试试这个:

Dim Current As Worksheet

' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
    If Current.Name = "6th Oct" Then Current.Delete
Next

With Worksheets(Worksheets.Count)
    With .Range(.Cells(3, "Q"), .Cells(.Rows.Count, "P").End(xlUp).Offset(0, 1))
        .Formula = "=IF(OR(J3=""Pending"",J3=""Assigned""),""Pending"",IF(AND(J3=""Closed"",E3=M3),""Passed"",""Failed""))"
    End With
End With
Range("Q2").Value = "Test"