VBA很有趣,我是新手。
我有一个Excel文件,其中包含我必须在文件夹中找到的编号列表,我想将该路径作为链接返回到我的Excel文件中,以便我可以打开并验证文档是否正确然后打印
INVOICE DATE INVOICE NO DN ORDER NO COMPANY NAME
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
5/9/2017 XXXXX XXXXX XXXXX XXXXX
说我的搜索文件夹路径是C:\Users\XXXXX\XXXXX\XXXXX
例如
INVOICE DATE INVOICE NO DN ORDER NO COMPANY NAME
5/9/2017 XXXXX XXXXX XXXXX XXXXX
我想搜索DN下的编号和Order No列的文件数,并检索列中找到的文件的计数,最重要的是检索在同一行中另一列中找到的文件的文件路径并搜索整个列表直到最后
DN - 287484135
和order No 580449925
- 按两个数字搜索
文件夹中文件的命名约定如下
Delivery_0650_LF_XXXXX_Delivery_note.PDF
- 这是针对DN的
Order_9250_TA_055699732_File Name..TIF
- 这是订单号
Order_9250_TA_052835641_Copy_of_customer_PO.TIF
- 这是订单号
或者至少请建议验证是否找到了关键字,以便有效地组织工作。
答案 0 :(得分:1)
你可能正在寻找这些线路周围的东西。此代码扫描Path
中文件夹中的每个文件,并将其与Ord No. Name
进行比较。然后,函数以counter, path, path, ...
格式返回文件数和路径。
Sub Test()
Dim Path1 As String, Data() As String
Dim lRow As Long
Dim i As Integer
Dim sht As Worksheet
Set sht = Worksheets("Tabelle1")
lRow = sht.Cells(Rows.Count, 3).End(xlUp).Row
Path1 = "C:\Users\User\Desktop\Files\"
Col = 3
For i = 2 To lRow
If sht.Cells(i, Col).Value <> "" Then
sht.Cells(i, Col + 5).Value = Scan(Path1, sht.Cells(i, Col).Value)
End If
If i = lRow Then
If Col = 4 Then
Exit For
End If
Col = 4
i = 0
End If
Next i
End Sub
Function Scan(ByVal MyPath As String, Name As String) As String
Dim File As String, temp As String
Dim i As Integer: i = 0
File = Dir(MyPath & "*.*")
Do While File <> ""
If File Like "*" & Name & "*" Then
i = i + 1
temp = temp & MyPath & File & ","
End If
File = Dir()
Loop
temp = i & "," & temp
Scan = Left(temp, Len(temp) - 1)
End Function