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

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

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循环遍历文件并检查它们的名称:

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

Use FSO to loop through files and check their names:

  1. Option Explicit
  2. Sub Example()
  3. Dim WB As Workbook
  4. Dim FSO As New FileSystemObject
  5. Dim oFolder As Object
  6. Dim oFile As Object
  7. Set FSO = CreateObject("Scripting.filesystemobject")
  8. Set oFolder = FSO.GetFolder("C:\Users\cameron\Documents")
  9. For Each oFile In oFolder.Files
  10. If oFile.Name Like "*Your Partial File Name*" Then
  11. Set WB = Workbooks.Open(oFile.Path)
  12. 'Do whatever you want with your workbook.
  13. WB.Close '(Optional True/False for save changes)
  14. End If
  15. Next oFile
  16. End Sub

答案2

得分: 1

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

  1. Public Sub LoopFilesNamesInFolder()
  2. Dim myPath As String
  3. Dim myFile As String
  4. Dim myExtension As String
  5. Dim MyFileName As String
  6. myPath = ThisWorkbook.Path & "\"
  7. '你要查找的文件名
  8. MyFileName = "*Health*"
  9. myExtension = "*.xls*"
  10. '循环中的当前文件
  11. myFile = Dir(myPath & myExtension)
  12. Do While myFile <> vbNullString
  13. If myFile Like MyFileName Then
  14. '你也可以使用类似于下面这样的语句
  15. 'If LCase(myFile) Like LCase(MyFileName) Then
  16. '如果你想要将其设为不区分大小写
  17. '你的代码
  18. Else
  19. '你的代码
  20. End If
  21. '获取下一个文件名
  22. myFile = Dir
  23. Loop
  24. End Sub
  25. Public Sub LoopAndOpenFilesInFolder()
  26. Dim wb As Workbook
  27. Dim myPath As String
  28. Dim myFile As String
  29. Dim myExtension As String
  30. Dim MyFileName As String
  31. '如果你想要优化代码,请保留以下三条指令
  32. Application.ScreenUpdating = False
  33. Application.EnableEvents = False
  34. Application.Calculation = xlCalculationManual
  35. myPath = ThisWorkbook.Path & "\"
  36. '你要查找的文件名
  37. MyFileName = "*Health*"
  38. myExtension = "*.xls*"
  39. '循环中的当前文件
  40. myFile = Dir(myPath & myExtension)
  41. Do While myFile <> vbNullString
  42. Set wb = Workbooks.Open(Filename:=myPath & myFile)
  43. '你可以在这里添加一个 DoEvents,以便给Excel打开工作簿的时间,然后再继续执行下面的代码
  44. DoEvents
  45. '做你想做的事情
  46. If wb.Name Like MyFileName Then
  47. '你也可以使用类似于下面这样的语句
  48. 'If LCase(wb.Name) Like LCase(MyFileName) Then
  49. '如果你想要将其设为不区分大小写
  50. '你的代码
  51. Else
  52. '你的代码
  53. End If
  54. '如果你想保存你的更改,请将 False 替换为 True 以保存并关闭工作簿
  55. wb.Close SaveChanges:=False
  56. '你也可以在这里添加一个 DoEvents,以便给你的Excel关闭的时间,然后再继续下一个
  57. DoEvents
  58. '获取下一个文件名
  59. myFile = Dir
  60. Loop
  61. '重置设置
  62. Application.ScreenUpdating = True
  63. Application.EnableEvents = True
  64. Application.Calculation = xlCalculationAutomatic
  65. 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

  1. Public Sub LoopFilesNamesInFolder()
  2. Dim myPath As String
  3. Dim myFile As String
  4. Dim myExtension As String
  5. Dim MyFileName As String
  6. myPath = ThisWorkbook.Path &amp; &quot;\&quot;
  7. &#39;The file name that you are looking for
  8. MyFileName = &quot;*Health*&quot;
  9. myExtension = &quot;*.xls*&quot;
  10. &#39;Current File in loop
  11. myFile = Dir(myPath &amp; myExtension)
  12. Do While myFile &lt;&gt; vbNullString
  13. If myFile Like MyFileName Then
  14. &#39;You can also use something like
  15. &#39;If LCase(myFile) Like LCase(MyFileName) Then
  16. &#39;If you want to make it not case sensitive
  17. &#39;Your code
  18. Else
  19. &#39;Your code
  20. End If
  21. &#39;Get next file name
  22. myFile = Dir
  23. Loop
  24. End Sub
  25. Public Sub LoopAndOpenFilesInFolder()
  26. Dim wb As Workbook
  27. Dim myPath As String
  28. Dim myFile As String
  29. Dim myExtension As String
  30. Dim MyFileName As String
  31. &#39;if you want to Optimize code keep the following 3 instructions
  32. Application.ScreenUpdating = False
  33. Application.EnableEvents = False
  34. Application.Calculation = xlCalculationManual
  35. myPath = ThisWorkbook.Path &amp; &quot;\&quot;
  36. &#39;The file name that you are looking for
  37. MyFileName = &quot;*Health*&quot;
  38. myExtension = &quot;*.xls*&quot;
  39. &#39;Current File in loop
  40. myFile = Dir(myPath &amp; myExtension)
  41. Do While myFile &lt;&gt; vbNullString
  42. Set wb = Workbooks.Open(Filename:=myPath &amp; myFile)
  43. &#39;You can add a DoEvents here to give time to Excel to open the workbook before moving fwd
  44. DoEvents
  45. &#39;Do Whatever you want to do
  46. If wb.Name Like MyFileName Then
  47. &#39;You can also use something like
  48. &#39;If LCase(wb.Name) Like LCase(MyFileName) Then
  49. &#39;If you want to make it not case sensitive
  50. &#39;Your Code
  51. Else
  52. &#39;Your Code
  53. End If
  54. &#39;If you want to save your changes, replace the False by True to Save and Close Workbook
  55. wb.Close SaveChanges:=False
  56. &#39;You can add a DoEvents here as well to give time to your Excel to close before moving to next one
  57. DoEvents
  58. &#39;Get next file name
  59. myFile = Dir
  60. Loop
  61. &#39;Reset Settings
  62. Application.ScreenUpdating = True
  63. Application.EnableEvents = True
  64. Application.Calculation = xlCalculationAutomatic
  65. 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:

确定