2017-09-19 4 views
0

CSV Files CSV 파일을 여는 데 이상한 문제가 있습니다. CSV 파일을 열고 vba를 사용하여 Excel에서 처리하는 코드를 작성했습니다. 특정 CSV 파일 하나만 열 수 있지만 다른 CSV 파일은 열 수 없거나 Excel에서 처리 할 수 ​​없습니다. 단서가 없습니다. 엑셀 VBA에서 특정 파일 하나만 읽히는 이유는 무엇입니까? 코드 아래vba를 사용하여 CSV 파일 열기

Sub lithium() 
Dim ws As Worksheet 
Dim MyData As String, strData() As String 
Dim PathInit As String 
Dim i As Integer 
Dim z As Long, filecount As Long 
Dim l As Long 
Dim LR As Long 
k = 2 
LR = Range("A" & Rows.Count).End(xlUp).Row 

' Opening the txt file 
Dim myTxt 
myTxt = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=False) 



Open myTxt For Binary As #1 
MyData = Space$(LOF(1)) 
Get #1, , MyData 
Close #1 
strData() = Split(MyData, vbCrLf) ' Saving the entire value of the text file into the array. every element in the array in the entire row of the txt file 

Dim strRow1() As String 
Dim strRow2() As String 
Dim strRow3() As String 
Dim strRow4() As String 
Dim strRow5() As String 
Dim strRow6() As String 
Dim nCount As Integer 


nCount = 1 
Dim nRowLenth As Integer 
nRowLenth = UBound(strData) - LBound(strData) ' Length of the total data array 
j = 3 

For i = 18 To nRowLenth 


      strRow1() = Split(strData(i), ";") 

      Cells(j, 15).value = strRow1(0) 
      Cells(j, 16).value = strRow1(1) 
      If Cells(j, 16).value = "c0" Then Cells(j, 17).value = strRow1(2) 
      If Cells(j, 16).value = "c1" Then Cells(j, 18).value = strRow1(2) 
      If Cells(j, 16).value = "c4" Then Cells(j, 19).value = strRow1(2) 
      Cells(j, 21).value = Left(Cells(j, 17).value, 2) 
      Cells(j, 22).value = Left(Cells(j, 18).value, 2) 
      Cells(j, 23).value = Right(Left(Cells(j, 18).value, 6), 2) & Right(Left(Cells(j, 18).value, 4), 2) 
      Cells(j, 23).NumberFormat = "0000" 
      Cells(j, 24).value = Left(Cells(j, 19).value, 2) 
      Cells(j, 26).value = Right(Left(Cells(j, 19).value, 12), 2) & Right(Left(Cells(j, 19).value, 10), 2) 
      Cells(j, 27).value = Right(Left(Cells(j, 19).value, 16), 2) & Right(Left(Cells(j, 19).value, 14), 2) 
      If Cells(j, 16).value = "c0" Then Cells(j, 1).value = Cells(j, 15).value Else _ 
      If Cells(j, 16).value = "c1" Then Cells(j, 1).value = Cells(j, 15).value Else _ 
      If Cells(j, 16).value = "c4" Then Cells(j, 1).value = Cells(j, 15).value Else _ 

      If Cells(j, 21).value = "" Then Cells(j, 2).value = "#N/A" Else Cells(j, 2).value = CLng("&H" & Cells(j, 21).value) 
      If Cells(j, 22).value = "" Then Cells(j, 3).value = "#N/A" Else Cells(j, 3).value = CLng("&H" & Cells(j, 22).value) 
      If Cells(j, 24).value = "" Then Cells(j, 4).value = "#N/A" Else Cells(j, 4).value = CLng("&H" & Cells(j, 24).value) - 40 
     Cells(j, 5).value = CLng("&H" & Cells(j, 25).value) - 40 
      If Cells(j, 23).value = "" Then Cells(j, 5).value = "#N/A" Else Cells(j, 5).value = CLng("&H" & Cells(j, 23).value) - 32768 
      If Cells(j, 26).value = "" Then Cells(j, 6).value = "#N/A" Else Cells(j, 6).value = CLng("&H" & Cells(j, 26).value) 
      If Cells(j, 27).value = "" Then Cells(j, 7).value = "#N/A" Else Cells(j, 7).value = CLng("&H" & Cells(j, 27).value) 
      If Cells(j, 27).value = "" Then Cells(j, 8).value = "#N/A" Else Cells(j, 8).value = Cells(j, 6).value - Cells(j, 7).value 


    j = j +1 
    Next 

    End Sub 

또한 하나의 특정 파일에 대해 제대로 작동하지만 하나의 오류 인덱스가 나는 이유를 모르겠다. 누군가가 도울 수 있다면

+0

우리는 전혀 모른다. ... 작동하지 않는 CSV 파일 및 작동하지 않는 CSV 파일의 구조에 대한 세부 정보를 게시하십시오. .... 우리는 어떤 줄이 당신에게 오류를 줄지 추측해야합니까? .... 당신의 질문은'나는이 그림 붓을 가지고 있습니다. 페인트 한 통에서 괜찮아 보이지만 다른 페인트 통에서는 잘 칠하지 않습니다. 당신은 저에게 이유를 말해 줄 수 있습니까? ' – jsotola

+0

CSV 파일은 모두 비슷한 구조입니다 ... 파일을 업로드 할 것입니다 ... – Santhosh

+0

질문에 각각 몇 줄을 붙여 넣으십시오. – jsotola

답변

0

아마도이 두 가지 해결책 중 하나에서 아이디어를 얻을 수 있습니다.

' Merge data from multiple sheets into seperate sheets 
Sub R_AnalysisMerger2() 
    Dim WSA As Worksheet 
    Dim bookList As Workbook 
    Dim SelectedFiles As Variant 
    Dim NFile As Long 
    Dim FileName As String 
    Dim Ws As Worksheet, vDB As Variant, rngT As Range 
    Dim vFn, myFn As String 

    Application.ScreenUpdating = False 

    SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.csv*), *.csv*", MultiSelect:=True) 
    If IsEmpty(SelectedFiles) Then Exit Sub 

    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) 
     FileName = SelectedFiles(NFile) 
     vFn = Split(FileName, "\") 
     myFn = vFn(UBound(vFn)) 
     myFn = Replace(myFn, ".csv", "") 
     Set bookList = Workbooks.Open(FileName, Format:=2) 
     Set WSA = bookList.Sheets(1) 
     vDB = WSA.UsedRange 
     bookList.Close (0) 
     Set Ws = Sheets.Add(after:=Sheets(Sheets.Count)) 
     ActiveSheet.Name = myFn 
     Ws.Range("a1").Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB 
    Next 
    Application.ScreenUpdating = True 

End Sub 

' Merge data from multime files into one sheet. 
Sub R_AnalysisMerger() 
    Dim WSA As Worksheet 
    Dim bookList As Workbook 
    Dim SelectedFiles() As Variant 
    Dim NFile As Long 
    Dim FileName As String 
    Dim Ws As Worksheet, vDB As Variant, rngT As Range 

    Application.ScreenUpdating = False 


    Set Ws = ThisWorkbook.Sheets(1) 
    Ws.UsedRange.Clear 
    'change folder path of excel files here 
    SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.csv*), *.csv*", MultiSelect:=True) 


    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) 
     FileName = SelectedFiles(NFile) 
     Set bookList = Workbooks.Open(FileName, Format:=2) 
     Set WSA = bookList.Sheets(1) 
     With WSA 
      vDB = .UsedRange 
      Set rngT = Ws.Range("a" & Rows.Count).End(xlUp)(2) 
      If rngT.Row = 2 Then Set rngT = Ws.Range("a1") 
      rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB 

      bookList.Close (0) 
     End With 
    Next 
    Application.ScreenUpdating = True 
    Ws.Range("A1").Select 

End Sub 
관련 문제