2014-05-14 3 views
0

책에서 VBE 기초 학습 후 마침내 나는 csv 파일을 여는 매크로를 만들려고 노력했다. csvname으로 저장하고 모두 완료 될 때까지 다시 실행하십시오.csv 가져 오기

입력 csv 파일 다음 줄에 문제가 있습니다. 그것은 오류 번호 13을 제공합니다, 나는 그것을 해결하는 방법에 대한 단서가 전혀 없습니다. 나는 현재 거기에 변수를 넣을 수 없다고 생각합니까? 내가 맞습니까? 어떤 해결책이 있습니까?

Sub CSVnaarxlsx() 
    'On Error Resume Next 
    'declareer variabelen 
    Dim strpath As String 
    Dim fmn As Integer 
    Dim lmn As Integer 
    Dim csvname 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 al de files 
For fmn = fmn To (lmn + 1) 
csvname = "m" & fmn 
'input of csv file 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;strpath & "/" & csvname" & ".csv""" _ 
     , Destination:=Range("sheet1!$A$1")) 
     .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 

End Sub 

Sub CsvToXlsx(ByVal csvname, strpath) 
ChDir (strpath & "/verwerkt") 
csvname = csvname & .xlsx 
    ActiveWorkbook.SaveAs Filename:=csvname, FileFormat:=51 
End Sub 
+0

답변에 만족하는 경우 언제든지 수락하십시오. 다음은 스크린 샷 형태의 초고속 설명입니다. http://i.stack.imgur.com/uqJeW.png –

답변

0

루틴 아래의 호스트 통합 문서와 같은 디렉토리에 모든으로 .XLSX 파일을 CSV를을 선택하라는 메시지를 표시하고 저장합니다 해결하는 방법을 예를주십시오. 나는 일어나고있는 모든 것을 설명하기 위해 많은 의견을 추가했다 - 이것이 도움이되기를 희망한다!

Option Explicit 
Sub OpenCSVFiles() 

Dim Path As String 
Dim TargetFiles As FileDialog 
Dim Index As Long 
Dim CSVBook As Workbook 

'prompt user to select CSV files 
Set TargetFiles = Application.FileDialog(msoFileDialogOpen) 
With TargetFiles 
    .AllowMultiSelect = True 
    .Title = "Select CSV files:" 
    .ButtonName = "" 
    .Filters.Clear 
    .Filters.Add ".csv files", "*.csv" 
    .Show 
End With 

'check to see if the user clicked cancel, if so exit 
If TargetFiles.SelectedItems.Count = 0 Then Exit Sub 

'loop through the selected files 
For Index = 1 To TargetFiles.SelectedItems.Count 

    'open the CSV 
    Set CSVBook = Workbooks.Open(TargetFiles.SelectedItems(Index)) 

    'extract the file path and file name 
    Path = CSVBook.FullName 
    Path = Left(Path, Len(Path) - 4) '<~ remove the .csv extension 

    'save as an xlsx file, no alerts 
    Application.DisplayAlerts = False 
    CSVBook.SaveAs Filename:=Path, FileFormat:=51 '<~ .xlsx = 51 on Windows 
    Application.DisplayAlerts = True 

    'close the CSV and repeat 
    CSVBook.Close False 

Next Index 

End Sub 
+0

정말 고맙습니다. 그것은 완전히 다른 코드입니다. 나는 그것이 무엇을하는지 이해하려고 노력할 것이고, 많은 것을 배울 것입니다. 이제는 commaseparator가 있다는 것을 구현할 필요가 있습니다. – BtotheE

+0

@ 듣기 정말 기쁩니다. 이것은 전에는'QueryTables'를 사용한 적이 없기 때문에 다른데, 트릭을해야합니다. 이 답변이 귀하에게 적합한 경우 귀하의 편의를 위해이를 수락하십시오. 다음은 스크린 샷 형태의 초고속 설명입니다. http://i.stack.imgur.com/uqJeW.png –

+0

매크로 레코더를 사용하여 매크로의 해당 부분을 생성하고 일부 변수를 추가 할 수 있다고 생각했습니다. 루프 만들기. 잠시 후 나는 그것을 알지 못했다. – BtotheE