Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 获取已打开工作簿的工作表名称_Vba_Excel - Fatal编程技术网

Vba 获取已打开工作簿的工作表名称

Vba 获取已打开工作簿的工作表名称,vba,excel,Vba,Excel,我有下面的代码,提示用户选择工作簿,我希望确保用户选择的是特定文件,为此,我希望在打开工作簿时验证工作表名称是否与我期望的一致: Private Sub CommandButton1_Click() Dim wb1 As Workbook, wb2 As Workbook Dim Ret1 Set wb1 = ActiveWorkbook Ret1 = Application.GetOpenFilename("Excel

我有下面的代码,提示用户选择工作簿,我希望确保用户选择的是特定文件,为此,我希望在打开工作簿时验证工作表名称是否与我期望的一致:

    Private Sub CommandButton1_Click()

        Dim wb1 As Workbook, wb2 As Workbook
        Dim Ret1
        Set wb1 = ActiveWorkbook

        Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
        , "Please a file to load from")
        If Ret1 = False Then Exit Sub

        Set wb2 = Workbooks.Open(Ret1)

    If wb2.Sheet1.Name = "Sum" And wb2.Sheet2.Name = "Names" And wb2.Sheet3.Name = "Things" Then
    MsgBox "Fine"
'Code Here
    Else
    MsgBox "Issue"
'Code Here
    End If

        wb2.Close SaveChanges:=False

        Set wb2 = Nothing
        Set wb1 = Nothing

    End Sub
不幸的是,当我运行上述代码时,如果wb2.Sheet1.Name=“Sum”和wb2.Sheet2.Name=“Names”和wb2.Sheet3.Name=“Things”


救命啊

您可以使用此功能检查工作表是否存在:

Function IsSheetExist(wb As Workbook, shName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = wb.Worksheets(shName)
    On Error GoTo 0
    IsSheetExist = Not ws Is Nothing
End Function
然后像这样使用它:

If IsSheetExist(wb2, "Sum") And IsSheetExist(wb2, "Names") And IsSheetExist(wb2, "Things") Then
    MsgBox "Fine"
    'Code Here
Else
    MsgBox "Issue"
    'Code Here
End If
    If wb2.Sheets(1).Name = "Sum" And wb2.Sheets(2).Name = "Names" And wb2.Sheets(3).Name = "Things" Then

如果要检查工作簿中是否按特定顺序存在千张工作表,可以使用以下方法:

Function IsContainsSheetsInOrder(wb As Workbook) As Boolean
    IsContainsSheetsInOrder = False

    If wb.Sheets.Count < 3 Then Exit Function
    If wb.Sheets(1).Name <> "Sum" Then Exit Function
    If wb.Sheets(2).Name <> "Names" Then Exit Function
    If wb.Sheets(3).Name <> "Things" Then Exit Function

    IsContainsSheetsInOrder = True
End Function

您可以使用此功能检查图纸是否存在:

Function IsSheetExist(wb As Workbook, shName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = wb.Worksheets(shName)
    On Error GoTo 0
    IsSheetExist = Not ws Is Nothing
End Function
然后像这样使用它:

If IsSheetExist(wb2, "Sum") And IsSheetExist(wb2, "Names") And IsSheetExist(wb2, "Things") Then
    MsgBox "Fine"
    'Code Here
Else
    MsgBox "Issue"
    'Code Here
End If
    If wb2.Sheets(1).Name = "Sum" And wb2.Sheets(2).Name = "Names" And wb2.Sheets(3).Name = "Things" Then

如果要检查工作簿中是否按特定顺序存在千张工作表,可以使用以下方法:

Function IsContainsSheetsInOrder(wb As Workbook) As Boolean
    IsContainsSheetsInOrder = False

    If wb.Sheets.Count < 3 Then Exit Function
    If wb.Sheets(1).Name <> "Sum" Then Exit Function
    If wb.Sheets(2).Name <> "Names" Then Exit Function
    If wb.Sheets(3).Name <> "Things" Then Exit Function

    IsContainsSheetsInOrder = True
End Function

或者,更靠近他的原始脚本,将
wb1.sheet#.Name
更改为
wb1.sheets(#)).Name
,如下所示:

If IsSheetExist(wb2, "Sum") And IsSheetExist(wb2, "Names") And IsSheetExist(wb2, "Things") Then
    MsgBox "Fine"
    'Code Here
Else
    MsgBox "Issue"
    'Code Here
End If
    If wb2.Sheets(1).Name = "Sum" And wb2.Sheets(2).Name = "Names" And wb2.Sheets(3).Name = "Things" Then

或者,更靠近他的原始脚本,将
wb1.sheet#.Name
更改为
wb1.sheets(#)).Name
,如下所示:

If IsSheetExist(wb2, "Sum") And IsSheetExist(wb2, "Names") And IsSheetExist(wb2, "Things") Then
    MsgBox "Fine"
    'Code Here
Else
    MsgBox "Issue"
    'Code Here
End If
    If wb2.Sheets(1).Name = "Sum" And wb2.Sheets(2).Name = "Names" And wb2.Sheets(3).Name = "Things" Then

谢谢,但似乎代码太多了。我正在寻找一个修复给我错误的线路。答案@TheMadTechnician就是这样做的。正如我看到您的主要意图是检查用户是否打开了正确的工作簿,但请注意,在他的回答中,如果用户打开工作簿时使用了两个或一个工作表,那么您的代码将因运行时错误而失败。感谢您强调这一点,我在
If
语句之前添加了
On Error GoTo Err
,在
Else
语句之后添加了
Err:
。但这是一个敏锐的观察,再次感谢。非常好的观点@simoco…@CaptainABC,如果wb1.sheets.count<3,您将想添加他的
,然后
位在那里,只是为了安全。
函数是sheetexist()
+1正确的方法:)谢谢,但似乎代码太多了。我正在寻找一个修复给我错误的线路。答案@TheMadTechnician就是这样做的。正如我看到您的主要意图是检查用户是否打开了正确的工作簿,但请注意,在他的回答中,如果用户打开工作簿时使用了两个或一个工作表,那么您的代码将因运行时错误而失败。感谢您强调这一点,我在
If
语句之前添加了
On Error GoTo Err
,在
Else
语句之后添加了
Err:
。但这是一个敏锐的观察,再次感谢。非常好的观点@simoco…@CaptainABC,如果wb1.sheets.count<3,那么您需要添加他的
位,以确保安全。
函数是sheetexist()
+1正确的方法:)