[英]Search all columns in an excel column and check value against file name in folder to see if exists?
I have a folder with .txt files: 我有一个包含.txt文件的文件夹:
\\uksh000-file06\SharedAreas\1.0 Hewden Public\NS\Approval
Each text file has a random name like so: 每个文本文件都有一个随机名称,如下所示:
NS123SHS.txt
NSg234eH.txt
NSds3461.txt
Most of the files file names (minus the extenstion .txt) are in my excel sheet in column c. 大多数文件的文件名(减去扩展名.txt)都在我的Excel工作表的c列中。
NS123SHS
NSds3461
I am trying to scan column c to check if the filename is found in my folder directory and if yes show a message saying found, else show a message saying not found. 我正在尝试扫描c列以检查是否在我的文件夹目录中找到了文件名,如果是,则显示一条消息,指出已找到,否则显示一条消息,指出未找到。
So far all I've been able to figure out is how to scan my column for specific value, which i define, however I want to be able to scan the entire column, and compare each value to see if it exists in my folder? 到目前为止,我所能找到的就是如何扫描我定义的特定值的列,但是我希望能够扫描整个列,并比较每个值以查看它是否存在于我的文件夹中?
Can someone please show me how I might do this please? 有人可以告诉我如何做吗? Thanks
谢谢
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
In the code bellow you'll have to update Worksheets( 1 ) to the sheet index where you have the file names, and FOLDER_PATH with your info: 在下面的代码中,您将必须将Worksheets( 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
. 。
Result: 结果:
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.