将.SRT文件数据导入Excel:模板与VBA代码适配求助
适配标准/截断格式的SRT文件完整导入Excel方案
你之前通过直接打开SRT文件再复制的方法,会因Excel对文本文件的格式解析逻辑,导致部分内容出现截断或识别错误。改用逐行读取文本文件的方式可完美解决该问题,无论SRT是标准格式还是缺少出点时间码的截断格式,都能完整保留所有原始内容到Excel的A列。
修改后的VBA代码
Sub ImportSRTToExcel() Dim fileDialog As FileDialog Dim filePath As String Dim fileNum As Integer Dim lineText As String Dim currentRow As Long ' 关闭屏幕刷新与弹窗,提升运行效率 Application.ScreenUpdating = False Application.DisplayAlerts = False ' 弹出文件选择框,限定仅选择SRT文件 Set fileDialog = Application.FileDialog(msoFileDialogFilePicker) With fileDialog .Filters.Clear .Filters.Add "SRT字幕文件", "*.srt" .Title = "选择要导入的SRT文件" If .Show <> -1 Then GoTo Cleanup ' 用户取消选择,直接退出 filePath = .SelectedItems(1) End With ' 初始化写入起始行,清空A列旧数据(若需要追加可修改起始行) currentRow = 1 ThisWorkbook.Worksheets(1).Range("A:A").ClearContents ' 打开SRT文件,逐行读取并写入A列 fileNum = FreeFile() Open filePath For Input As #fileNum Do Until EOF(fileNum) Line Input #fileNum, lineText ThisWorkbook.Worksheets(1).Cells(currentRow, 1).Value = lineText currentRow = currentRow + 1 Loop Close #fileNum Cleanup: ' 恢复系统默认设置 Application.ScreenUpdating = True Application.DisplayAlerts = True Set fileDialog = Nothing End Sub
代码核心优势
- 直接读取文本文件原始内容,完全规避Excel对SRT格式的错误解析
- 完整保留SRT中的每一行内容,包括空行、单时间码行等特殊格式
- 可灵活调整:若需追加内容到现有数据后,将
currentRow = 1改为currentRow = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1即可
内容的提问来源于stack exchange,提问作者ManuelPieperDrums
相关产品推荐
相关产品推荐

