如何用VBScript读取CSV文件的最后一行?
Hey there! No worries at all—we all start somewhere, and there's no such thing as a "stupid" question when you're learning. Let's fix this together.
Looking at your code, right now you're looping through every row in the CSV and printing each one (which is why you're seeing the first row, plus all others if there are more). To get just the last row, we don't need that loop—we can directly target the final row of the used range. Also, using Excel to read a CSV is a bit overkill (and can be slow if the file updates often), so I'll show you two solutions: one that fixes your existing Excel-based code, and a lighter-weight approach that reads the CSV as a plain text file (which is better for continuously updated files).
Solution 1: Fix Your Excel-Based Script
We'll remove the unnecessary loop and directly calculate the last row in the worksheet:
Option Explicit Dim objExcel Dim excelPath Dim workSheetCount Dim counter Dim currentWorkSheet Dim usedRowsCount Dim top Dim lastRow Dim lastRowValue excelPath = "c:\Test\test.csv" WScript.Echo "Reading Data from " & excelPath Set objExcel = CreateObject("Excel.Application") objExcel.DisplayAlerts = 0 objExcel.Workbooks.open excelPath, false, true workSheetCount = objExcel.Worksheets.Count WScript.Echo "We have " & workSheetCount & " worksheets" For counter = 1 to workSheetCount WScript.Echo "-----------------------------------------------" WScript.Echo "Reading data from worksheet " & counter & vbCRLF Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(counter) usedRowsCount = currentWorkSheet.UsedRange.Rows.Count top = currentWorkSheet.UsedRange.Row ' Calculate the exact row number of the last entry lastRow = top + usedRowsCount - 1 ' Grab the value from the last row (column 1; adjust the number if you need a different column) lastRowValue = currentWorkSheet.Cells(lastRow, 1).Value WScript.Echo "Last row content: " & lastRowValue Next Set currentWorkSheet = Nothing objExcel.Workbooks(1).Close objExcel.Quit Set currentWorkSheet = Nothing Set objExcel = Nothing
Solution 2: Lighter-Weight Approach (No Excel Required)
Since CSV files are plain text, using the Scripting.FileSystemObject to read them directly is faster, doesn't depend on Excel being installed, and is better for files that update frequently. Here's how to get the last line:
Option Explicit Dim fso, file, line, lastLine Dim filePath filePath = "c:\Test\test.csv" Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile(filePath, 1) ' 1 = ForReading mode ' Loop through all lines, storing the most recent one as the last line Do Until file.AtEndOfStream line = file.ReadLine() lastLine = line Loop file.Close WScript.Echo "Last row content: " & lastLine ' Optional: Split the last line into columns if you need specific values Dim columns columns = Split(lastLine, ",") ' Example: Get the 2nd column (indexes start at 0) WScript.Echo "2nd column of last row: " & columns(1) Set file = Nothing Set fso = Nothing
Key Notes:
- If your CSV uses a different delimiter (like semicolons), replace
","in theSplitfunction with your delimiter. - The text-based method avoids the overhead of launching Excel, which is ideal for a continuously updating file.
内容的提问来源于stack exchange,提问作者Sean Jones

