2013-01-31 2 views
8

명명 된 워크 시트가 여러 개 포함 된 매크로 사용 Excel 통합 문서가 있습니다. 워크 시트 중 하나는 "패널"이라고하고 두 번째 워크 시트는 "데이터"라고합니다. "panel"이라는 시트에는 매크로가 할당 된 버튼이 있습니다. "panel"이라는 이름의 워크 시트에서 버튼을 선택하고 파일 탐색 창이 나타납니다. 사용자가 하드 드라이브에서 csv 파일을 선택하면 csv 파일의 내용을 A1 셀에서 시작하는 "data"라는 워크 시트로 가져 오십시오.매크로 csv 파일을 엑셀 비활성 워크 시트로 가져 오기

문제 1 : 단추에 할당 한 vba가 csv 파일의 내용을 단추 ("패널"워크 시트)와 동일한 워크 시트에 배치합니다. 나는 csv 파일의 내용을 "데이터"시트에 넣고 싶습니다.

문제 2 : 또한 내 하드 드라이브와 "capture.csv"라는 파일을 참조하는 코드 문자열이 있습니다. 따라서 매크로가 활성화 된 Excel 파일이 다른 컴퓨터에 있으면 파일이 충돌합니다. 모든 컴퓨터에서 파일을 사용할 수 있도록 경로 문자열을 제거하는 방법은 무엇입니까?

이 문제를 해결하는 데 도움을 주시면 매우 감사하겠습니다. 버튼에 할당 된 매크로는 다음과 같습니다.

Sub load_csv() 
Dim fStr As String 
With Application.FileDialog(msoFileDialogFilePicker) 
.Show 
If .SelectedItems.Count = 0 Then 
MsgBox "Cancel Selected" 
End 
End If 
'fStr is the file path and name of the file you selected. 
fStr = .SelectedItems(1) 
End With 
Range("A1").Select 
With ActiveSheet.QueryTables.Add(Connection:= _ 
"TEXT;C:\Users\laptop\Desktop\CAPTURE.csv", Destination:=Range("$A$1")) 
.Name = "CAPTURE" 
.FieldNames = True 
.RowNumbers = False 
.FillAdjacentFormulas = False 
.PreserveFormatting = True 
.RefreshOnFileOpen = False 
.RefreshStyle = xlInsertDeleteCells 
.SavePassword = False 
.SaveData = True 
.AdjustColumnWidth = True 
.RefreshPeriod = 0 
.TextFilePromptOnRefresh = False 
.TextFilePlatform = 437 
.TextFileStartRow = 1 
.TextFileParseType = xlDelimited 
.TextFileTextQualifier = xlTextQualifierDoubleQuote 
.TextFileConsecutiveDelimiter = False 
.TextFileTabDelimiter = True 
.TextFileSemicolonDelimiter = False 
.TextFileCommaDelimiter = True 
.TextFileSpaceDelimiter = False 
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
.TextFileTrailingMinusNumbers = True 
.Refresh BackgroundQuery:=False 
MsgBox fStr 
End With 
End Sub 

답변

12

이게 당신이하려는 겁니까?

Sub load_csv() 
    Dim fStr As String 

    With Application.FileDialog(msoFileDialogFilePicker) 
     .Show 
     If .SelectedItems.Count = 0 Then 
      MsgBox "Cancel Selected" 
      Exit Sub 
     End If 
     'fStr is the file path and name of the file you selected. 
     fStr = .SelectedItems(1) 
    End With 

    With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
    "TEXT;" & fStr, Destination:=Range("$A$1")) 
     .Name = "CAPTURE" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 437 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 

    End With 
End Sub 
+0

. 이것은 도움이 될 것입니다. – Rick

+0

저를 도와 주셔서 감사합니다. 내가 작성한 코드에 버튼을 지정했습니다. csv 파일을로드하면 "Run-time error '-2147024809 (80070057) 대상 범위가 쿼리 테이블을 만들 때와 동일한 워크 시트에 있지 않습니다."라는 메시지 창이 나타납니다. – George

+1

'Destination : = Range ("$ A $ 1"))'목적지 : = ThisWorkbook.Sheets ("데이터") 범위 ("$ A $ 1"))' –

0

는 Mac에서 Excel 용, 쿼리 테이블 객체가 "PreserveFormatting"와 "RefreshPeriod"속성을 지원하지 않으며, 당신이 시도하고이를 설정하면 당신에게 런타임 오류를 줄 것 같다.

또한 Application.FileDialog는 Mac에서 작동하지 않지만 다른 게시물에서 다루어집니다. 맥에 대한

: 내가 말할 거라고 정확히 무엇

Sub load_csv() 
Dim fStr As String 

fStr = "Macintosh HD:Users:anthony:Documents:example.csv" 'Keeping file String simple for example. 

With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
"TEXT;" & fStr, Destination:=Range("$A$1")) 
    .Name = "CAPTURE" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    '.PreserveFormatting = True **commented out for Mac 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    '.RefreshPeriod = 0 **commented out for Mac 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 437 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

End With 
End Sub 
관련 문제