編輯14可能excel中導入csv
經過大量閱讀,我終於明白了VBA的基礎知識。我已經創建了下面的宏,但它仍然無效,它不會插入csv文件。 這個宏完成後,保存的文件全部爲空。用debug.print我確認了文件的字符串是完整的,但仍然缺少一些東西?
任何人可以幫助我解決這個問題
在此先感謝
Sub CSVimporterennaarxlsx()
'On Error Resume Next
'declare variable
Application.ScreenUpdating = False
Dim strpath As String
Dim fmn As Integer
Dim lmn As Integer
Dim csvname As String
Dim strpathcsvname As String
'active workbook pathway
strpath = Application.ActiveWorkbook.Path
'ask user for first and last number
fmn = InputBox("first mouse number")
lmn = InputBox("last mouse number")
'einde sub if inputbox is empty
' If fmn = "" Then
' MsgBox "No first mouse number"
' Exit Sub
' End If
' If lmn = "" Then
' MsgBox "No Last mouse number"
' Exit Sub
' End If
'assign variables
'loop all the files
For fmn = fmn To lmn
csvname = "m" & fmn
strpathcsvname = strpath & "\" & csvname & ".csv"
'input of csv file
' ActiveSheet.Cells.Delete
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" + strpathcsvname, _
Destination:=Range(A1))
'filename without extension
.Name = csvname
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1)
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = ","
.TextFileTrailingMinusNumbers = True
End With
Call CsvToXlsx(ByVal csvname, strpath)
Next fmn
Application.DisplayAlerts = True
End Sub
Sub CsvToXlsx(ByVal csvname, strpath)
ChDir (strpath & "/verwerkt")
Application.DisplayAlerts = False
csvname = csvname & ".xlsx"
ActiveWorkbook.SaveAs Filename:=csvname, FileFormat:=51
End Sub
這能否幫助? http://stackoverflow.com/questions/10551353/saving-excel-worksheet-to-csv-files-with-filenameworksheet-name-using-vb – jpr
http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/merge-函數/ csvs-to-sheets可能是感興趣的。 – pnuts