从CSV中提取值并将其放入Excel工作表中

从CSV中提取值并将其放入Excel工作表中,excel,vba,csv,Excel,Vba,Csv,我有一个名为HardwareMonitoring.CSV的CSV文件,位于路径D:\GPU Info 我需要在Excel文件中运行宏,以从CSV文件中提取一些信息,并按以下方式将其放入Excel工作表1: LAST value of column B of CSV file should be placed in the cell G2 of the Excel file LAST value of column C of CSV file should be placed in the ce

我有一个名为HardwareMonitoring.CSV的CSV文件,位于路径D:\GPU Info

我需要在Excel文件中运行宏,以从CSV文件中提取一些信息,并按以下方式将其放入Excel工作表1:

LAST value of column B of CSV file should be placed in the cell G2 of the Excel file 
LAST value of column C of CSV file should be placed in the cell A2 of the Excel file 
LAST value of column D of CSV file should be placed in the cell B2 of the Excel file 
LAST value of column E of CSV file should be placed in the cell C2 of the Excel file 
LAST value of column F of CSV file should be placed in the cell D2 of the Excel file 
LAST value of column G of CSV file should be placed in the cell E2 of the Excel file 
LAST value of column H of CSV file should be placed in the cell F2 of the Excel file 
注意:最后一个值的行并不总是同一行,它会有所不同

CSV文件最后一行的图像

将运行宏的Excel文件的图像

运行宏后Excel的外观图像


如果宏遇到excel文件中的值,则可以使用新值覆盖这些值。请尝试下一个代码。它不会在Excel中打开csv文件。它假定csv分隔符是逗号(“,”),行分隔符是
vbCrLf
。如果没有,则必须在代码发送特定消息时更改它们。它将处理结果放在活动表上,但很容易调整代码以选择所需的代码:

Private Sub ExtractFromCSV()
    Dim ws As Worksheet, strFile As String, sep As String, arrCSV, arrLast, i As Long, strProbl As String
                           
    Set ws = ActiveSheet 'you can use here the sheet you need (maybe "Sheet1")

    strFile = "D:\GPU Info\HardwareMonitoring.csv"
    
    'Put the content of the csv file in an array (split by the line ending separator). If not vbCrLf, use the appropriate one:
    arrCSV = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(strFile, 1).ReadAll, vbCrLf)
      If UBound(arrCSV) = 0 Then MsgBox "The end line delimiter is not the chosen one (vbCrLf)...": Exit Sub
      
    sep = ","   'the csv file separator. Use here the correct one if not comma
    arrLast = Split(arrCSV(UBound(arrCSV)), sep) 'split the last array row, by csv separator (comma, Tab etc.)
      If UBound(arrLast) = 0 Then MsgBox "The csv delimiter is not the chosen one...": Exit Sub
      
      'Put the extracted values in their place on the sheet:
     ws.Range("A2:G2").value = Array(arrLast(2), arrLast(3), arrLast(4), arrLast(5), arrLast(1))
End Sub
从CSV检索
选项显式
子更新事件()
“常数
常量sFolderPath为String=“D:\GPU信息”
Const sfName As String=“HardwareMonitoring.csv”
常量sCols为String=“B:H”
Const dName As String=“Sheet1”
Const dFirst As String=“A2”
Application.ScreenUpdating=False
"来源:
'检查文件是否存在。
将sFilePath设置为字符串:sFilePath=sFolderPath&sfName
如果Dir(sFilePath)=“”,则
MsgBox“文件”&“文件路径&”不存在
出口接头
如果结束
'如果打开,请关闭文件。
将swb设置为工作簿
出错时继续下一步
设置swb=工作簿(sfName)
错误转到0
如果不是swb,那就什么都不是了
swb.Close SaveChanges:=True
如果结束
'根据列表分隔符打开文件。这对我有用
'在我的计算机上,但不正确。您可能需要使用
'其中只有一个没有'Select Case'或使用'OpenTextFile'
“如FaneDuru的解决方案所示。
选择Case Application.International(xlListSeparator)
案例“,”
设置swb=Workbooks.Open(文件名:=sFilePath)
案例“;”
设置swb=Workbooks.Open(文件名:=sFilePath,本地:=True)
其他情况
MsgBox“列表分隔符”_
&不包括Application.International(xlListSeparator)和“.”
出口接头
结束选择
将sws标注为工作表:设置sws=swb。工作表(1)
暗sCell As范围
Set sCell=sws.Cells.Find(“*”,xlFormulas,xlByRows,xlPrevious)
如果sCell什么都不是
swb.Close SaveChanges:=False
MsgBox“工作表为空。”
出口接头
如果结束
Dim sData作为变量:sData=sws.Columns(sCols).Rows(sCell.Row).Value
swb.Close SaveChanges:=False
"结果,
长度为的Dim cCount:cCount=UBound(sData,2)
作为变量的Dim dData:ReDim dData(1到1,1到帐户)
dData(1,cCount)=sData(1,1)
尺寸c与长度相同
对于c=2的情况,应计算
dData(1,c-1)=sData(1,c)
下一个c
“目的地
将dwb设置为工作簿:设置dwb=ThisWorkbook包含此代码的工作簿
将dws标注为工作表:设置dws=dwb。工作表(dName)
尺寸dCell作为范围:设置dCell=dws.范围(dFirst)
dCell.Resize(,cCount).Value=dData
“dwb.Save
Application.ScreenUpdating=True
端接头

至少,讨论中使用csv文件的分隔符是什么?逗号<代码>选项卡?我的意思是,从这个角度看,它是一个逗号分隔的文件,还是什么类型的文件?在fleek上工作!非常感谢。CSV文件分隔符是“,”但当我执行它时,会显示“错误424,需要一个对象”。知道发生了什么吗?我正在驾驶niw。。。在什么代码行上?