Sub SaveTxtFiles() Dim Path As String Dim FileName As String Dim TextContent As String Dim i As Integer ' 设定文件夹路径 Path = "D:\9-2-jxw\" ' 检查文件夹是否存在 If Not FolderExists(Path) Then MsgBox "文件夹不存在,请检查路径是否正确。", vbCritical, "错误" Exit Sub End If ' 循环遍历A1:B50区域,创建并保存txt文件 For i = 1 To 50 ' 获取文件名和内容 FileName = Range("A" & i).Value TextContent = Range("B" & i).Value ' 创建并保存txt文件 CreateTextFile Path & FileName, TextContent Next i MsgBox "所有txt文件已成功保存到指定文件夹。", vbInformation, "完成" End Sub ' 检查文件夹是否存在 Function FolderExists(ByVal FolderPath As String) As Boolean On Error Resume Next If Not Dir(FolderPath, vbDirectory) = "" Then FolderExists = True Else FolderExists = False End Function ' 创建并保存txt文件 Sub CreateTextFile(ByVal FilePath As String, ByVal FileContent As String) Dim FileNumber As Integer FileNumber = FreeFile() Open FilePath For Output As #FileNumber Print #FileNumber, FileContent Close #FileNumber End Sub