使用VBAsearch多个CSV文件中的值

首先,我真的是一个罕见的Excel用户。 而现在,我需要用它来“发展”。

所以,基本上,我有一个工作簿中的这种forms(作为第一张)。 此表单具有文件夹目标(CSV文件所在的位置),要search的值(如单元名称)和button。 文件夹目的地和要search的值可以根据用户在表单中input的内容而改变。

当用户按下button时,macros将运行,CSV中匹配单元的一些特征将显示在工作簿的下一页中。

我有一个想法,使用列来search和检索CSV的价值。 例如,CSV中B列所有要查找的值,而需要检索的值分别为C和F。

那么,有谁能帮助我呢? 我目前正在做我的研究和研究,因为我对这种语言很陌生。

这是我迄今为止所做的。 当我在单个工作簿上进行search时,可以使用searchfunction(search,复制和粘贴)。 但是我无法打开打开的文件夹,并在文件中循环。 谁可以帮我这个事? 任何forms的帮助将不胜感激。

Sub Button1_Click() Dim LSearchRow As Integer Dim LCopyToRow As Integer Dim strPath As String Dim strFile As String Dim wbSource As Workbook Dim wsSource As Worksheet Dim wsMain As Worksheet Dim wsDest As Worksheet Dim filePath Dim numOfData Dim unitName On Error GoTo Err_Execute Set wsMain = ThisWorkbook.Worksheets(1) Set wsDest = ThisWorkbook.Worksheets(2) 'Set destination worksheet to paste the matching data wsDest.Cells.Clear 'filePath = wsMain.Range("B4").Value 'Get the file path unitName = wsMain.Range("B5").Value 'Get the unit name 'Open the files from the path provided strPath = "C:\Users\Lenovo\Desktop\DataLog" strFile = Dir(strPath & "\*.csv") Do While strFile <> vbNullString Set wbSource = Workbooks.Open(strPath & "\" & strFile) Set wsSource = wbSource.Worksheets(1) noOfData = wsSource.Range("A2:A12").Find("*", , , , xlByRows, xlPrevious).Row - 1 'Set row to start to search and copy data to LSearchRow = 2 LCopyToRow = 2 While Len(wsSource.Range("B" & CStr(LSearchRow)).Value) > 0 'If value in column B = "unitName", copy entire row to Sheet2 If wsSource.Range("B" & CStr(LSearchRow)).Value = unitName Then 'Copy and paste the matching values into the next sheet For Count = 2 To noOfData + 1 wsDest.Cells(1, 1).Value = "Unit No" wsDest.Cells(1, 2).Value = unitName wsDest.Cells(LCopyToRow, 1).Value = _ wsSource.Cells(LSearchRow, 3).Value wsDest.Cells(LCopyToRow, 2).Value = _ wsSource.Cells(LSearchRow, 6).Value Next Count 'Move counter to next row LCopyToRow = LCopyToRow + 1 'Go back to Sheet1 to continue searching wsSource.Select End If LSearchRow = LSearchRow + 1 Wend wbSource.Close True strFile = Dir() Loop MsgBox "All matching data has been copied." Exit Sub Err_Execute: MsgBox "An error has occurred." End Sub