使用”Like”运算符打开Excel工作簿 | VBA

huangapple go评论64阅读模式
英文:

Using Like operator to open Excel workbooks | VBA

问题

感谢您抽出时间阅读这个内容。

我正在尝试使用LIKE运算符打开工作簿。之前我使用了下面的代码,效果非常好:

Report_Name = "\XYZ.xls"
Workbooks.open Filename:= ThisWorkbook.Path & Report_Name

我的主要目标是打开一个报告,有时报告的名称会有所不同,比如healthdoc或healthassess。

我尝试使用LIKE运算符来捕获工作簿的名称,但我找不到编写代码的方法。

任何指导或帮助将不胜感激。谢谢!

我尝试使用以下语法:

Dim Report_Name as Workbook
if Report_Name LIKE "*Health*" then

xyz

else

xyz

然而我只能让LIKE运算符在字符串中起作用。

英文:

Thank you for taking the time to read this.

I am trying to use the LIKE operator to open workbooks. Previously I went with this code which works really well

Report_Name = "\XYZ.xls"
Workbooks.open Filename:= ThisWorkbook.Path & Report_Name

My main goal is to essentially open a report and sometimes the names differ i.e healthdoc or healthassess

I tried utilizing the LIKE operator to pick up on the name of the workbook however I cannot find a way to code it.

Any direction or help is appreciated. Thank you!

I was trying to use this syntax

Dim Report_Name as Workbook
if Report_Name LIKE "*Health*" then

xyz

else

xyz

However I could only get LIKE operator working only with strings

答案1

得分: 1

使用FSO循环遍历文件并检查它们的名称:

Option Explicit
Sub Example()
    
    Dim WB As Workbook
    Dim FSO As New FileSystemObject
    Dim oFolder As Object
    Dim oFile As Object
    
    Set FSO = CreateObject("Scripting.filesystemobject")
    Set oFolder = FSO.GetFolder("C:\Users\cameron\Documents")
    
    For Each oFile In oFolder.Files
        If oFile.Name Like "*Your Partial File Name*" Then
            
            Set WB = Workbooks.Open(oFile.Path)
            
            '在工作簿上执行你想要的操作。
            
            WB.Close '(可选的True/False用于保存更改)
            
        End If
    Next oFile
    
End Sub
英文:

Use FSO to loop through files and check their names:

Option Explicit
Sub Example()
    
    Dim WB As Workbook
    Dim FSO As New FileSystemObject
    Dim oFolder As Object
    Dim oFile As Object
    
    Set FSO = CreateObject("Scripting.filesystemobject")
    Set oFolder = FSO.GetFolder("C:\Users\cameron\Documents")
    
    For Each oFile In oFolder.Files
        If oFile.Name Like "*Your Partial File Name*" Then
            
            Set WB = Workbooks.Open(oFile.Path)
            
            'Do whatever you want with your workbook.
            
            WB.Close '(Optional True/False for save changes)
            
        End If
    Next oFile
    
End Sub

答案2

得分: 1

以下是我可以提供的两个子程序。由于我无法从你的问题中理解你是想循环、打开并修改文件夹中的文件,还是只想检查文件夹中的文件名,所以我创建了两个子程序。第一个子程序 "LoopFilesNamesInFolder" 将循环检查文件名而不打开文件,第二个子程序 "LoopAndOpenFilesInFolder" 允许你打开并在需要时对文件进行更改。你可以根据需要使用其中任何一个。如果对你有所帮助,请让我知道。

Public Sub LoopFilesNamesInFolder()

Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim MyFileName As String

myPath = ThisWorkbook.Path & "\"
'你要查找的文件名
MyFileName = "*Health*"

myExtension = "*.xls*"
'循环中的当前文件
myFile = Dir(myPath & myExtension)

 Do While myFile <> vbNullString

   If myFile Like MyFileName Then
   '你也可以使用类似于下面这样的语句
   'If LCase(myFile) Like LCase(MyFileName) Then
   '如果你想要将其设为不区分大小写
   '你的代码
   Else
   '你的代码
   End If

   '获取下一个文件名
   myFile = Dir
  Loop
  
End Sub

Public Sub LoopAndOpenFilesInFolder()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim MyFileName As String

'如果你想要优化代码,请保留以下三条指令
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

myPath = ThisWorkbook.Path & "\"
'你要查找的文件名
MyFileName = "*Health*"

myExtension = "*.xls*"
'循环中的当前文件
myFile = Dir(myPath & myExtension)
 
 Do While myFile <> vbNullString

      Set wb = Workbooks.Open(Filename:=myPath & myFile)
    
    '你可以在这里添加一个 DoEvents,以便给Excel打开工作簿的时间,然后再继续执行下面的代码
      DoEvents
    
    '做你想做的事情
   If wb.Name Like MyFileName Then
   '你也可以使用类似于下面这样的语句
   'If LCase(wb.Name) Like LCase(MyFileName) Then
   '如果你想要将其设为不区分大小写
   '你的代码
   Else
   '你的代码
   End If

    '如果你想保存你的更改,请将 False 替换为 True 以保存并关闭工作簿
     wb.Close SaveChanges:=False
      
    '你也可以在这里添加一个 DoEvents,以便给你的Excel关闭的时间,然后再继续下一个
      DoEvents

    '获取下一个文件名
      myFile = Dir
  Loop
  
'重置设置
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
英文:

Here are 2 sub that I can suggest. Since I don't understand from your question if you want to loop, open and modify the files in a folder, or you want simply to check the files names in a folder, I made 2 sub. One "LoopFilesNamesInFolder" will loop to check the file names without opening the files, and second one "LoopAndOpenFilesInFolder" allows you to open and make changes if you want to the files. You can use any of them based on your needs. Let me know if it helps you

Public Sub LoopFilesNamesInFolder()

Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim MyFileName As String

myPath = ThisWorkbook.Path &amp; &quot;\&quot;
&#39;The file name that you are looking for
MyFileName = &quot;*Health*&quot;

myExtension = &quot;*.xls*&quot;
&#39;Current File in loop
myFile = Dir(myPath &amp; myExtension)

 Do While myFile &lt;&gt; vbNullString

   If myFile Like MyFileName Then
   &#39;You can also use something like
   &#39;If LCase(myFile) Like LCase(MyFileName) Then
   &#39;If you want to make it not case sensitive
   &#39;Your code
   Else
   &#39;Your code
   End If

   &#39;Get next file name
   myFile = Dir
  Loop
  
End Sub


Public Sub LoopAndOpenFilesInFolder()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim MyFileName As String

&#39;if you want to Optimize code keep the following 3 instructions
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
  
myPath = ThisWorkbook.Path &amp; &quot;\&quot;
&#39;The file name that you are looking for
MyFileName = &quot;*Health*&quot;

myExtension = &quot;*.xls*&quot;
&#39;Current File in loop
myFile = Dir(myPath &amp; myExtension)
 
 Do While myFile &lt;&gt; vbNullString

      Set wb = Workbooks.Open(Filename:=myPath &amp; myFile)
    
    &#39;You can add a DoEvents here to give time to Excel to open the workbook before moving fwd
      DoEvents
    
    &#39;Do Whatever you want to do
   If wb.Name Like MyFileName Then
   &#39;You can also use something like
   &#39;If LCase(wb.Name) Like LCase(MyFileName) Then
   &#39;If you want to make it not case sensitive
   &#39;Your Code
   Else
   &#39;Your Code
   End If

    &#39;If you want to save your changes, replace the False by True to Save and Close Workbook
     wb.Close SaveChanges:=False
      
    &#39;You can add a DoEvents here as well to give time to your Excel to close before moving to next one
      DoEvents

    &#39;Get next file name
      myFile = Dir
  Loop
  
&#39;Reset Settings
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub

huangapple
  • 本文由 发表于 2023年2月8日 08:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定