简体   繁体   English

在工作簿之间切换

[英]Switch between workbooks

I have a code that searches for a certain information in another workbook, but that workbook changes names and formats. 我有一个代码可以在另一个工作簿中搜索某些信息,但是该工作簿会更改名称和格式。 I am currently using workbook.open to open it, and when I specify the file location it has a dynamic part (you will understand on the sample code). 我当前正在使用workbook.open打开它,当我指定文件位置时,它具有动态部分(您将在示例代码上理解)。 The problem is that in the middle of the code I need to go back to the first workbook (the one running the code), do some stuff, then go back again to the workbook it searched the info on. 问题是在代码中间,我需要回到第一个工作簿(运行代码的那个),做一些事情,然后再回到它搜索信息的工作簿。 I tried setting it as a variable using dim secondfile as string/workbook (tried both) and then tried to set it as the active workbook when it was "active", so I could use "secondfile.activate" later on when I needed to go back to it, but had no luck. 我尝试使用dim secondfile作为字符串/工作簿(同时尝试了两者)将其设置为变量,然后尝试将其设置为“活动”时将其设置为活动工作簿,因此以后可以在需要时使用“ secondfile.activate”回到它,但没有运气。 Here is the code: 这是代码:

dim originalworkbook as workbook
set originalworkbook as this workbook
dim wb as workbook
set wb as Workbooks.Open("C:\Users\abc\Documents\bla bla bla " + Range("D6").Offset(iLoop).Value)
'code here...
originalworkbook.activate
'code here...
'[insert code here to go back to the wb workbook]

I cannot tell it to open the wb workbook again because it would not make sense. 我不能告诉它再次打开wb工作簿,因为这没有意义。 Right now I have list with 1k+ data that is searched in another more than 30 files. 现在我有一个包含1k +数据的列表,在另外30多个文件中进行搜索。 The name of the file is on the first workbook, at least part of it, as you can see in the code. 文件的名称在第一个工作簿上,至少在其中一部分,如代码中所示。 I use the "Range("D6").offset" part to get it. 我使用“ Range(“ D6”)。offset“部分来获取它。 The thing is, it would take too long to run the code if I closed and opened the second workbook everytime, so what I am trying to do is to search for all things that are in a specific file and after searching it all, saving and closing it. 事实是,如果我每次关闭并打开第二个工作簿,都将花费很长时间来运行代码,所以我想做的是搜索特定文件中的所有内容,并在全部搜索之后保存并保存关闭它。 That is why I need to get back to this file... 这就是为什么我需要回到这个文件...

PS: not all files are in the same format, thats why I didn't use "workbooks.activate" or something like it and then use the same "Range("D6").offset" thing to set the file to activate, because some are in .xls and others are in xlsx... PS:并非所有文件的格式都相同,这就是为什么我不使用“ workbooks.activate”或类似的东西,然后使用相同的“ Range(” D6“)。offset”来设置要激活的文件的原因,因为有些在.xls中,而另一些在xlsx中...

Public w as workbook    
For Each w In Workbooks
        If w.Name = originalworkbook Then
            Workbooks(w.Name).Activate
            'Do Stuff
        Exit For
        End If
Next w

The beauty of the above is that you can use this for infinite number of excel files open at the same time. 上面的好处是您可以使用它同时打开无限多个excel文件。 just change If w.Name = original workbook then to if w.Name like "*charcters from a file name*" then 只需更改If w.Name = original workbook then更改为if w.Name like "*charcters from a file name*" then

The wild cards allow you to search efficiently 通配符使您可以高效搜索

Hope this helps! 希望这可以帮助!

Since your code is really not very descriptive I'm giving you a piece of mine and I'll try to explain what's going on. 由于您的代码实际上不是很具描述性,因此我将向您介绍我的情况,我将尝试解释发生了什么。 I am writing in another workbook with this code, putting information from the first. 我正在用此代码编写另一本工作簿,从第一本开始放置信息。 It's a long sub and I will remove unnecessary parts. 这是一个很长的子程序,我将删除不必要的部分。

I tried to cover all situations you would encounter. 我试图涵盖您遇到的所有情况。

sub TemplateFiller(AHNSnumber as String)
    Dim wbk As Workbook
    Dim wbpath As String
    Dim lastline as long
    Dim wbName as string

    wbpath = "O:\08_Lean_Eng\10_On_going\David\Soldier's Pond\MDR\Templates\TemplateCustom.xls"

    'This is to check if it's open first. Only open it if it is closed!
    'IsWorkBookOpen is one of my own functions, not included here
    If IsWorkBookOpen(wbpath) = False Then
        Set wbk = Workbooks.Open(wbpath) 'Notice: set wbk =, not Set wbk as
    ElseIf IsWorkBookOpen(wbpath) = True Then
        wbName = "somethingworkbook.xlsb"
        Set wbk = Workbooks(wbName)
    End If

    'Finding the last line in the other wbk (I'm using info from the other wbk for variables here)
    lastline = wbk.Sheets("DL001").Range("BG65000").End(xlUp).row + 1

    wbk.Sheets("Dl001").Range("F" & lastline) = AHNSnumber 'Using a passed variable
    wbk.Sheets("Dl001").Range("G" & lastline) = "OK" 'Writing some stuff
    wbk.Sheets("Dl001").Range("H" & lastline) = ThisWorkbook.Sheets("a").Range("A1") 'Value from one to the other and vice versa
    Thisworkbook.sheets("b").Range("A2") = wbk.Sheets("Dl001").Range("A" & lastline)


    'Changing a variable in this workbook
    ThisWorkbook.Sheets("Data").Range("Revision") = ThisWorkbook.Sheets("Data").Range("Revision") + 1

End sub

'I am not the original writer of this function.
Function IsWorkBookOpen(filename As String) As Boolean
    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open filename For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM