我是一個虛擬的vb和excel,試圖將我在這裏找到的2個宏合併爲1,但顯然做了一些非常錯誤的事情,現在我「M卡。首先我只是用這個宏(作爲personal.xlsb保存它,以便能夠在任何工作簿中使用它)excel宏保存工作表作爲具有特定分隔符和外殼的csv
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,因爲我的老闆不喜歡這樣的事情......我我當然對如何實現在設置分隔符和外殼後自動保存紙張的目標感興趣。在這一刻,我對如何擺脫這個宏以及它隱藏的位置非常感興趣。我做了什麼?!感謝您的耐心等待!