如何检查“ afile.xlsm:2”是否已打开VBA

时间:2019-01-11 20:02:32

标签: excel vba function if-statement

我已经在Excel工作表中建立了一个搜索栏,用户可以在其中单击[搜索]。它将根据一些FormControl按钮对指定的表列进行排序,然后从当前工作簿打开一个NewWindow,选择Sheet2并在Sheet2上搜索指定的列。我无法阻止下次有人搜索手动关闭第二个窗口时再次发生这种情况。我目前只需要一次打开两个活动窗口。因此,我不希望Excel打开第三个ActiveWindow,依此类推。

我对使用功能不太熟悉。我已经粘贴了宏Sub和Function。我尝试过不同的配置/变化,以为我缺少一些简单的东西(希望如此)。

在我看来,我的AlreadyOpen函数不正确。当afile.xlsm:2已经打开时,我似乎无法使我的第一个IF...True语句正常工作。

Function AlreadyOpen(sFname As String) As Boolean
    Dim wkb As Workbook
    'Dim sFname As String
    sFname = "afile.xlsm:2"
    On Error Resume Next
    Set wkb = Workbooks(sFname)
    AlreadyOpen = Not wkb Is Nothing
    Set wkb = Nothing
End Function


Private Sub Search_cmd1_Click()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value

Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant

'Load Sheet into A Variable
  Set sht = ActiveSheet

'Unfilter Data (if necessary)
  On Error Resume Next
    sht.ShowAllData
  On Error GoTo 0

'Filtered Data Range (include column heading cells)
  Set DataRange = sht.ListObjects("Table1").Range 'Table

'Retrieve User's Search Input
  mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*"     'ActiveX Control    ''must include "*" for partials

'Determine if user is searching for number or text
  If IsNumeric(mySearch) = True Then
    SearchString = "=" & mySearch
  Else
    SearchString = "=*" & mySearch & "*"
  End If

'Loop Through Option Buttons
  For Each myButton In sht.OptionButtons
    If myButton.Value = 1 Then
      ButtonName = myButton.Text
      Exit For
    End If
  Next myButton

'Determine Filter Field
  On Error GoTo HeadingNotFound
    myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
  On Error GoTo 0

'Filter Data
      DataRange.AutoFilter _
        Field:=myField, _
        Criteria1:=SearchString, _
        Operator:=xlAnd

    Dim sFilename As String

    sFilename = "afile.xlsm:2"

    If AlreadyOpen(sFilename) Then
        Sheets("Sheet2").ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
    Else
        If myButton.Text = "SITE" Then
        Sheets("Sheet1").Select
        ActiveWindow.NewWindow
        Windows("afile.xlsm:1").Activate
        Windows("afile.xlsm:2").Activate
        Windows.Arrange ArrangeStyle:=xlVertical
        Sheets("Sheet2").Select
        ActiveWindow.Zoom = 55

        ActiveSheet.ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
        End If
    End If
Exit Sub

End Sub

我希望Excel打开NewWindow(“ afile.xlsm:2”),选择Sheet2和Sort Table1。但是,如果第二个窗口已经打开,则只需对Table24排序即可。

1 个答案:

答案 0 :(得分:1)

WorkbookWindow不同,这肯定是您的If语句失败的地方。您需要修改功能以反映这一点。

Function AlreadyOpen(sFname As String) As Boolean
    Dim wkb As Window
    On Error Resume Next
    Set wkb = Windows(sFname)
    wkb.Activate
    AlreadyOpen = Not wkb Is Nothing
    Set wkb = Nothing
End Function