选择工作表为函数中的字符串或整数

时间:2018-07-10 21:45:49

标签: vba excel-vba

当我致电Function opensheet(“mysheet”)时,以下代码不起作用。我可以这样称呼工作表的名称或工作表编号吗?

Function opensheet(tab As String)

    ActiveWorkbook.Worksheets(tab).Select

End Function

3 个答案:

答案 0 :(得分:1)

您不能将tab用作变量名,我修改了代码并进行了测试,它可以正常工作:

Function opensheet(str_Tab As String)
    ActiveWorkbook.Worksheets(str_Tab).Select
End Function

Sub hello()
    opensheet "TestName"
End Sub

请尽量避免使用Tab,Sheet,book之类的保留字,我通常将其称为MyTab或类似名称,或者将其前缀为str_tab(因为它是字符串)或lng_tab(如果它很长),当您的代码开始变大时,它会很有帮助。

希望有帮助

编辑:以下是几种简化的方法,可从您作为最终例行程序发布的内容中获得相同的结果:

此代码将轮询第三行中馈入数组的工作表列表,这是灵活的,因为可以随意调用工作表。

Function SumCounts(sht As String, dest As String, addr As String, divi As String)
Dim SheetArray As Variant, TempValue As Long, X As Long
SheetArray = Array("BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ", "KKK", "LLL", "MMM", "NNN", "OOO", "PPP")
TempValue = 0
For X = LBound(SheetArray) To UBound(SheetArray)
    TempValue = TempValue + Application.CountIf(Workbooks(SheetArray(X)).Worksheets(sht).Range(addr), ">0")
Next
Workbooks("MASTER").Sheets("XXX").Range(dest).Value = TempValue / Workbooks("MASTER").Sheets("XXX").Range(divi).Value
End Function

此代码使用ASCII码,并假设您的工作表是一个接一个的命名,它不如上面的代码灵活,但更短

Function SumCounts(sht As String, dest As String, addr As String, divi As String)
Dim TempValue As Long, X As Long
TempValue = 0
For X = 66 To 80
    TempValue = TempValue + Application.CountIf(Workbooks(Chr(X) & Chr(X) & Chr(X)).Worksheets(sht).Range(addr), ">0")
Next
Workbooks("MASTER").Sheets("XXX").Range(dest).Value = TempValue / Workbooks("MASTER").Sheets("XXX").Range(divi).Value
End Function

另外,我可能不会在函数中进行工作表操作,而是在函数中进行计算,然后在工作表中进行操作,只是我的想法,但它使维护起来非常容易:)。

希望能以某种方式帮助您。

答案 1 :(得分:0)

谢谢大家。为了将来的搜索/信息参考,这里是完整的功能:

bin/Release/netcoreapp*

答案 2 :(得分:-1)

玩这些功能..

    'To check if a worksheet exists
    Public Function worksheetExists(ByVal wb As Workbook, ByVal sheetNameStr As String) As Boolean

    On Error Resume Next
    worksheetExists = (wb.Worksheets(sheetNameStr).Name <> "")
    Err.Clear: On Error GoTo 0

    End Function

    'To get the name of the first visible worksheet in the workbook (the one at the far left)
    Public Function getFirstSheet(ByVal wb As Workbook) As String
    Dim x As Integer, sheetCount As Long

    sheetCount = wb.Worksheets.Count
    For x = 1 To sheetCount
        If wb.Worksheets(x).Visible = xlSheetVisible Then Exit For
    Next x
    getFirstSheet = wb.Worksheets(x).Name

    End Function

    'To activate a specific worksheet and move cursor to a specific position
    Public Sub activateSheet(ByRef wb As Workbook, ByVal sheetNameStr As String, Optional ByVal lngCol As Long = 1, Optional ByVal moveToEndCol As Boolean = False)

    If worksheetExists(wb, sheetNameStr) Then
        With wb.Worksheets(sheetNameStr)
            If .Visible = xlSheetHidden Or .Visible = xlSheetVeryHidden Then .Visible = xlSheetVisible
            .Activate
            If moveToEndCol Then
                Application.GoTo Reference:=.Range(columnLetter(lngCol) & LastRowInOneColumn(wb.Worksheets(sheetNameStr), 2) + 1), Scroll:=True
            Else
                Application.GoTo Reference:=.Range(columnLetter(lngCol) & 1), Scroll:=True
            End If
            .Activate
        End With
    End If

    End Sub

'To return the column letter for a given column number
Function columnLetter(ByVal lngCol As Long) As String
Dim splitArr As Variant

splitArr = Split(Cells(1, lngCol).Address(True, False), "$")
columnLetter = splitArr(0)
Erase splitArr

End Function