此代码提取名称为“M *”的文件,我希望它还可以提取名称为“X *”的文件。我可以简单地在filename参数上设置OR条件吗?谢谢你的建议!
Worksheets("PMNs").Range("A2:A500").Clear
Application.Calculation = xlManual
Dim Filename, Pathname As String
Dim wb As Workbook
Dim ws As Worksheet
Pathname = ActiveWorkbook.Path & "\"
Filename = Dir(Pathname & "M*.xlsm")
x = 1
Range("A1").Select
Do While Filename <> ""
x = x + 1
' MsgBox (Filename)
Windows("Resource Load Data_fy18.xlsm").Activate 'open data file
Sheets("PMNs").Select
ActiveCell(A & x).Value = Filename 'copy filename to next cell Ax
Filename = Dir()
Loop
Application.Calculation = xlAutomatic
Dim lastrow As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:A" & lastrow).Sort key1:=Range("A2:A" & lastrow), _
order1:=xlAscending, Header:=xlNo
End Sub
答案 0 :(得分:0)
稍微更改您打开文件的方式:
Dim Filename as String, Pathname As String
Dim wb As Workbook
Dim ws As Worksheet
Pathname = ActiveWorkbook.Path & "\"
x = 1
Range("A1").Select
Dim StrFile As String
StrFile = Dir(Pathname)
Do While Len(StrFile) > 0
If (Left(StrFile, 1) = "M" Or Left(StrFile, 1) = "X") and right(StrFile,4) = "xlsm" Then
Debug.print(StrFile) ' prints the file name and extension
' Do stuff here!
End If
Filename = Dir
Loop