我有一个包含.txt文件的文件夹:
\\uksh000-file06\SharedAreas\1.0 Hewden Public\NS\Approval
每个文本文件都有一个随机名称,如下所示:
NS123SHS.txt
NSg234eH.txt
NSds3461.txt
大多数文件文件名(减去扩展名.txt)都在c栏的Excel表格中。
NS123SHS
NSds3461
我正在尝试扫描列c以检查文件夹目录中是否找到文件名,如果是,则显示已找到的消息,否则显示一条消息称未找到。
到目前为止,我能够弄清楚的是如何扫描我的列以获取特定值,我定义了,但是我希望能够扫描整个列,并比较每个值以查看它是否存在于我的文件夹中?
有人可以告诉我怎么可能这样做吗?感谢
Private Sub Workbook_Open()
Dim FindString As String
Dim Rng As Range
FindString = "NSds3461"
If Trim(FindString) <> "" Then
With Sheets("Home").Range("C:C") 'searches all of column A
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MsgBox "found" 'value not found
Else
MsgBox "Reference Not Found" 'value not found
End If
End With
End If
答案 0 :(得分:0)
在下面的代码中,您必须将工作表( 1 )更新为您拥有文件名的工作表索引,并将FOLDER_PATH更新为您的信息:
\\uksh000-file06\SharedAreas\1.0 Hewden Public\NS\Approval
Option Explicit
Private Sub Workbook_Open()
Const FOLDER_PATH As String = "C:\temp\"
Const FILE_NAME_COL As Long = 3
Dim fileNames As Variant, ws As Worksheet, i As Long
Dim fName As String, result As String
Set ws = Worksheets(1)
fileNames = ws.UsedRange.Columns(FILE_NAME_COL)
For i = 2 To UBound(fileNames)
fName = Trim(fileNames(i, 1))
If Len(Dir(FOLDER_PATH & fName)) > 0 Then
fName = fName & vbTab & ": Found" & vbCrLf
Else
fName = fName & vbTab & ": Not Found" & vbCrLf
End If
result = result & i - 1 & ". " & vbTab & fName
Next
MsgBox result, , "Search Result"
End Sub
结果:
希望有所帮助