【发布时间】:2015-09-05 15:17:21
【问题描述】:
我有一个包含 .txt 文件的文件夹:
\\uksh000-file06\SharedAreas\1.0 Hewden Public\NS\Approval
每个文本文件都有一个随机名称,如下所示:
NS123SHS.txt
NSg234eH.txt
NSds3461.txt
大多数文件的文件名(减去扩展名 .txt)都在我的 Excel 工作表的 c 列中。
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
【问题讨论】:
-
看看
Dir函数。它将允许您在文件夹中搜索具有给定名称的文件。如果要遍历整个列,则需要在Range上使用For Each循环而不是Find。
标签: vba excel search directory