Пожалуйста, попробуйте этот код.
Sub RowsToCSV()
' Variatus @STO 06 Apr 2020
Dim Sinw As Integer ' remember setting
Dim Path As String
Dim Fn As String ' file name
Dim Ws As Worksheet ' for input
Dim CapsRng As Range
Dim Rng As Range
Dim Cl As Long ' last column
Dim Rl As Long ' last row
Dim R As Long
' you can specify another open workbook
Set Ws = ThisWorkbook.Worksheets("Data")
' you can specify another output path but it must exist
Path = Environ("UserProfile") & "\Desktop\CSV Test files\"
With Application
Sinw = .SheetsInNewWorkbook
.SheetsInNewWorkbook = 1
.ScreenUpdating = False
.Calculation = xlCalculationManual
.DisplayAlerts = False ' over-write existing without warning
End With
With Ws
Rl = .Cells(.Rows.Count, 1).End(xlUp).Row
Cl = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set CapsRng = .Cells(1, 1).Resize(1, Cl)
For R = 2 To Rl ' start in row 2
Set Rng = Application.Union(CapsRng, .Cells(R, 1).Resize(1, Cl))
' modify the file name here:-
Fn = Format(Date, "yymmdd ") & "Test " & _
Trim(.Cells(R, 1).Value) & ".csv"
With Workbooks.Add
Rng.Copy Destination:=.Sheets(1).Cells(1, 1)
.SaveAs Path & Fn, xlCSV
.Close SaveChanges:=False
End With
Next R
End With
With Application
.SheetsInNewWorkbook = Sinw ' return to original setting
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
End With
End Sub