简体   繁体   English

搜索excel列中的所有列,并对照文件夹中的文件名检查值是否存在?

[英]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.

相关问题 如何针对 Excel 中多列中的所有值搜索一列中的一个值 - How to search for one value in a column against all values in multiple columns in Excel 检查其他多个工作表上是否存在excel单元格值-如果是,则返回存在于另一列中的工作表名称 - Check if an excel cell value exists on multiple other sheets - and if so return the name of the sheet it exists on in another column VBA搜索文件名的一部分,看看文件是否存在? - vba search part of a file name to see if file exists? Excel宏以搜索文件夹中是否存在列(文件名)的内容 - Excel Macro to search if content of a column (filename) exists in a folder Excel - VBA 代码检查列 A(人名)与列 F(6) 到 K(11),如果有包含“是”,则在空白行中添加“是” - Excel - VBA code to check column A (person's name) against Columns F(6) to K(11), if any contain "Yes", then add "Yes" to the blank rows Excel:检查列中是否存在单元格字符串值,并获取对该字符串的所有单元格引用 - Excel: Check if cell string value exists in column, and get all cell references to that string 在Excel中,搜索一列以获取存在于另一列的最大值 - In Excel, search one column for highest value that exists that is linked to another column Excel-检查值是否存在于另一个工作表的列中并返回相邻列 - Excel - Check if value exists in a column in another worksheet and return adjacent column Excel:检查所有列的值是否不同 - Excel: check all columns have a different value 检查 Excel 文件中的标题/列名称 - Check for Header/Column name in Excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM