Dim s() As String, F8 As String Open "c:\A_工单用料明细.csv" For Input As #1 s = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf) Close #1 F8 = Split(s(UBound(s) - 1), ",")(8) MsgBox F8
可以使用VB的FileSystemObject对象来读取CSV文件,并使用Split函数将每一行分割成数组。然后可以通过数组的长度来获取最后一行,并使用Split函数将最后一行分割成单元格。最后可以使用Excel的Application对象来计算公式并获取结果。
以下是一份示例代码:
Sub ReadLastCellInCSV()
Dim filePath As String
filePath = "C:\test.csv" 'CSV文件路径
' 创建FileSystemObject对象
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' 打开CSV文件
Dim file As Object
Set file = fso.OpenTextFile(filePath, 1)
' 读取CSV文件内容
Dim content As String
content = file.ReadAll
' 关闭CSV文件
file.Close
' 将CSV文件内容分割成行数组
Dim lines() As String
lines = Split(content, vbCrLf)
' 获取最后一行
Dim lastLine As String
lastLine = lines(UBound(lines))
' 将最后一行分割成单元格数组
Dim cells() As String
cells = Split(lastLine, ",")
' 获取第8列的内容
Dim cellValue As String
cellValue = cells(7) '数组下标从0开始,第8列的下标为7
' 创建Excel的Application对象
Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
' 计算公式并获取结果
Dim result As Variant
result = excelApp.Evaluate(cellValue)