尝试在Mac计算机上将过滤后的记录保存到txt。代码可以编写文本文件,但是当使用excel打开时,整个记录将转储到一列而不是多列。它还在双引号之间写入文本。 Text to Column命令可以将它转换为多列而没有任何问题,但我希望它能够自动化以防止手动将文本转换为列并防止文本的双引号。希望有人可以提供帮助。感谢。
不好:
所需:
这是代码:
Sub Filter_To_CSV_MAC()
Dim My_Range As Range
Dim DestSh As Worksheet
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim rng As Range
Dim MyPath As String
Dim foldername As String
Dim filename As String
Dim sFName As String
Dim intFNumber As Integer
Dim cellValue As Variant, i As Integer, j As Integer
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
Set My_Range = Range("A1:I" & LastRow(ActiveSheet))
My_Range.Parent.Select
'Fill in the path\folder where you want the new folder with the files
MyPath = ActiveWorkbook.Path
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> Application.PathSeparator Then
MyPath = MyPath & Application.PathSeparator
End If
'Create folder for the new files
foldername = MyPath & "Lot Sheet Copy" & Application.PathSeparator
filename = "Lot Sheet Copy " & Format(Now, "yyyy-mm-dd hh-mm")
CreateMyFolderIfNotExist (foldername)
'Set the destination worksheet
Set DestSh = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Lot Filter"
Exit Sub
End If
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Firstly, remove the AutoFilter
My_Range.Parent.AutoFilterMode = False
'Filter and set the filter field and the filter criteria :
My_Range.AutoFilter Field:=2, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>"""
'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
On Error GoTo 0
If CCount > 0 Then
'Setting the name and the path of text file based on workbook path
sFName = foldername & filename & ".txt"
'Get an unused file number
intFNumber = FreeFile
'Create a new file (or overwrite an existing one)
Open sFName For Output As #intFNumber
Write #intFNumber, "Number", "ShortDescription", "Description", "Sales Tax/VAT", "BuyersPremiumRate", "StartPrice", "Reserve", "Increment", "Categories", vbCrLf
'Apply filter
With My_Range.Parent.AutoFilter.Range
On Error Resume Next
' Set rng to the visible cells in My_Range
Set rng = .SpecialCells(xlCellTypeVisible)
'Setting the name and the path of text file based on workbook path
sFName = foldername & filename & ".txt"
'Get an unused file number
intFNumber = FreeFile
'Create a new file
Open sFName For Output As #intFNumber
On Error GoTo 0
If Not rng Is Nothing Then
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next j
Next i
End If
'Close the text file
Close #intFNumber
End With
End If
On Error Resume Next
'Close AutoFilter
My_Range.Parent.AutoFilterMode = False
'Restore ScreenUpdating, Calculation, EnableEvents, ....
ActiveWindow.View = ViewMode
'Application.Goto DestSh.Range("A1")
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub