对于 vb 和 excel,我完全是个傻瓜,试图将我在这里找到的 2 个宏组合成 1 个,但显然做了一些非常错误的事情,现在我被卡住了。首先我只是使用了这个宏(保存它作为personal.xlsb,以便能够在任何工作簿中使用它)
Sub CSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
ListSep = ";"
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & """" & GetUTF8String(CurrCell.Value) & """" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub
那加上 GetUTF8String 函数代码。现在一切正常。然后我想好了为什么不只是尝试我有限的(这是一个严重的轻描淡写)vb理解,添加以下代码并将CSVFile子更改为一个函数,然后我从下面的子调用它,输出文件名为一个参数(用于代替 FName = Application.GetSaveAsFilename)。我想是的,这段代码会自动保存所有工作表,现在让我们确保在保存每张工作表之前运行编码和分隔符/附件设置功能。这似乎不对,但我想嘿为什么不试试..
Public Sub SaveAllSheetsAsCSV()
On Error GoTo Heaven
' each sheet reference
Dim Sheet As Worksheet
' path to output to
Dim OutputPath As String
' name of each csv
Dim OutputFile As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
' Save the file in current director
OutputPath = ThisWorkbook.Path
If OutputPath <> "" Then
Application.Calculation = xlCalculationManual
' save for each sheet
For Each Sheet In Sheets
OutputFile = OutputPath & Application.PathSeparator & Sheet.Name & ".csv"
' make a copy to create a new book with this sheet
' otherwise you will always only get the first sheet
Sheet.Copy
' this copy will now become active
CSVFile(OutputFile)
ActiveWorkbook.SaveAs Filename:=OutputFile, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
Next
Application.Calculation = xlCalculationAutomatic
End If
Finally:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Exit Sub
Heaven:
MsgBox "Couldn't save all sheets to CSV." & vbCrLf & _
"Source: " & Err.Source & " " & vbCrLf & _
"Number: " & Err.Number & " " & vbCrLf & _
"Description: " & Err.Description & " " & vbCrLf
GoTo Finally
End Sub
保存了它,我已经成功地实现了一些非常不同的东西。在打开任何工作簿时,该宏会运行并将该特定工作簿中的工作表打开为 csv 文件(不保存它们)。现在我就像爱丽丝梦游仙境。它为什么在文件打开时运行?这是不可取的,所以我回到宏代码并将其更改回只是 csvfile 子。好吧,这没有帮助,不知道我在那里做了什么,肯定是在编辑同一个宏......所以我删除了宏,模块,我无法想象现在的东西在哪里,但它仍在运行 + 我收到这个警告宏被停用。无法摆脱它!现在小伙子们,我很抱歉我完全缺乏专业性,这只是对客户的一个小忙,没有浪费大量时间学习 vb,因为我的老板没有 t like that...我当然对如何实现在设置分隔符和附件后自动保存工作表的目标感兴趣。此刻我对如何摆脱那个宏以及它隐藏在哪里非常感兴趣。我做了什么?!感谢您的耐心等待!