如何激活excel工作簿而不使用其名称或至少部分名称?提取的工作表具有动态名称

时间:2017-08-04 06:27:54

标签: excel vba excel-vba

我几乎完成了我的代码,从我们公司的服务器中提取数据并将其粘贴到我已经创建的已经托管我的代码的工作表中...问题是我想复制来自我从服务器打开的这个excel工作簿,但问题是它有一个动态名称,每次从服务器检索它时都会更改。但是,工作簿名称的初始部分没有改变,所以有没有办法只使用未更改的名称部分激活工作簿?提取的工作簿的常量部分通常以BFUK开头,然后数字,空格和下划线以.txt.xls" BFUK *****。txt结尾。的.xls"

Sub test()

    Set objIE = CreateObject("InternetExplorer.Application")
    WebSite = "company website"
    logSite = "/logoff.do"
    With objIE
        .Visible = True
        .Navigate WebSite
        Do While .Busy Or .readyState <> 4
            DoEvents
        Loop

        Set unElement = .Document.getElementsByName("username")
        unElement.Item(0).Value = "username"
        Set pwElement = .Document.getElementsByName("password")
        pwElement.Item(0).Value = "password"
        .Document.forms(0).submit
        '.quit

        Do While .Busy Or .readyState <> 4
            DoEvents
        Loop


        Set expElement = .Document.getElementsByClassName("nav__action dropdown-trigger js--tooltip")
        expElement(0).Click

        Do While .Busy Or .readyState <> 4
            DoEvents
        Loop

        .Document.getElementById("obb_EXPORT_EXCEL").Click

    End With

     application.Wait (Now + TimeValue("0:00:04")) SendKeys "%O", True


    With objIE


      .Navigate logSite


>    End With

    Windows("sheet with the dynamic name.xls").Activate ActiveSheet.Range("A1:BC600").Copy

Windows("macro testing.xlsm").Activate
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

        [L:L].Select With Selection
    .NumberFormat = "General"
    .Value = .Value End With

     End Sub

我真的非常需要这些人,所以我将非常感谢你的帮助,如果能够完成工作,我会欢迎新的想法。

2 个答案:

答案 0 :(得分:0)

这里有一个页面How to list all files in directory。获得文件名后,可以这样比较:

If fileName Like "BFUK*.txt.xls" Then
    Windows(fileName) ....
   'rest of your code...
End If

它将匹配以&#34开头的文件名称; BFUK&#34;并以&#34; .txt.xls&#34;结尾。之间可以是任意数量的任何字符。

这里的示例宏:

Sub ListFilesMatchingPattern()

Dim objFSO, objFolder, objFile As Object
Dim i As Integer
Dim path As String
'choose folder with your Excel files
Application.FileDialog(msoFileDialogFolderPicker).Show
path = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(path)
i = 1
'loops through each file in the directory and prints their names
For Each objFile In objFolder.Files
    'open the Excel file if it matches pattern
    If objFile.Name Like "BFUK*txt.xls" Then
        'in your macro, you should do work on the file here
        Workbooks.Open (path & "\" & objFile.Name)
    End If
Next objFile

End Sub

答案 1 :(得分:0)

@Michal我找到了它并最终工作

> > Sub Tester()
> 
>    Const File_PATH As String = "C:\Users\Ali\Desktop\Ali\"
>    
> 
> Dim file As String

> 
> file = Dir$(File_PATH & "BFUK*" & ".xlsx")
> 
> If (Len(file) > 0) Then   Workbooks.Open(File_PATH & file).Activate
>       End If
> 
> End Sub

我唯一的办法就是使用SendKeys "%S", True将文件保存到特定文件夹中,然后上面的代码将其调用