我从未在 VBA 中编码,但我正在尝试将 obj-c 中的一些知识转换为这个。
-我想打开一个文件(一个文件夹大约200个文件)
- 浏览每个文件中的单元格范围
- 然后找到每个单元格中第一个逗号之前的所有内容(单词)(此范围内的单元格有三个逗号)
- 将每个单元格的值添加到数组中
(扫描其余文件并执行相同操作)
-获取结果数组并将它们全部粘贴到另一个名为 master list 的文件中
我想我已经涵盖了大部分内容(第一次使用 VBA,虽然不确定),但我还没有弄清楚如何阅读每个单元格中的第一个逗号之前的所有内容
另外,如果我有任何明显的错误或逻辑问题,请告诉我
先感谢您
感谢您的帮助!
Sub CopyWordsToMainFileRow()
Dim Cell As Range
Dim counter As Integer
Dim word As String
Dim arrayOfIngredients() As Variant 'array of words from search
Dim fileName As String
Dim arrayOfFileNames As Variant
Dim MainCounter As Integer
Dim p As String, x As Variant
MainCounter = 0
counter = 0
' Make array of file names
p = "/Users/waf04/Desktop/*.xls"
arrayOfFileNames = GetFileList(p)
Select Case IsArray(arrayOfFileNames)
Case True 'files found
MsgBox UBound(arrayOfFileNames)
Sheets("Sheet1").Range("A:A").Clear
For i = LBound(arrayOfFileNames) To UBound(arrayOfFileNames)
Sheets("Sheet1").Cells(i, 1).Value = arrayOfFileNames(i)
Next i
Case False 'no files found
MsgBox "No matching files"
End Select
'end make array of file names
'Create array from cells in each file
For fileNameCounter = 0 To UBound(arrayOfFileNames)
fileName = arrayOfFileNames(MainCounter)
Workbooks.Open fileName:="fileName"
arrayOfIngredients = Range("AT2:EP200").Value 'add value of cells to array
'make array of results for each file
For Each Cell In Range("AT2:EP200")
word = Cell.Value ' make this string equal to the value of everything before the first comma in that cell
arrayOfIngredients(counter) = word 'add string to array
counter = counter + 1
Next Cell
Workbooks.Close fileName:="fileName"
Next fileNameCounter
'==============================
'Output unsorted array
Workbooks.Open fileName:="/Users/waf04/Desktop/ingredients_collection.xlsx"
Range("A1:A" & UBound(arrayOfIngredients) + 1) = _
WorksheetFunction.Transpose(arrayOfIngredients)
End Sub