2012-08-15 3 views
1

통합 문서를 선택하라는 메시지를 표시 한 다음 선택한 통합 문서의 첫 번째 워크 시트를 기존 (활성) 통합 문서의 탭으로 추가하는 서브 루틴을 만들려고합니다. 그런 다음 새 탭의 이름을 "데이터"로 지정합니다.다른 통합 문서의 워크 시트를 VBA를 사용하여 Excel에로드

Sub getworkbook() 
' Get workbook... 
Dim ws As Worksheet 
Dim filter As String 
Dim targetWorkbook As Workbook 

Set targetWorkbook = Application.ActiveWorkbook 

' get the customer workbook 
filter = "Text files (*.xlsx),*.xlsx" 
caption = "Please Select an input file " 
ws = Application.GetOpenFilename(filter, , caption) 

ws.Add After:=Sheets(Sheets.Count) 

ws.Name = "DATA" 

End Sub 

이 코드가 작동하는 다음과 같은 오류를 반환하지 않는 것 : 어떤 도움에 감사드립니다

"ws.Add" method or With Block not set.

여기에 지금까지 사용하고있는 코드입니다. 당신은 워크 시트로 wsGetOpenFilename을 선언 한

+0

비슷한 질문 : http://stackoverflow.com/questions/3840628/creating-and-naming-worksheet-in-excel-vba –

답변

4

덕분에, 파일 이름을 반환합니다. 내 게시물을 읽는 것이 좋습니다 link :

이것이 무엇입니까?

참고 : 오류 처리를 수행하지 않았습니다. 나는 당신이 그것을 돌볼 수 있다고 확신합니다.

Sub getworkbook() 
    ' Get workbook... 
    Dim ws As Worksheet 
    Dim filter As String 
    Dim targetWorkbook As Workbook, wb As Workbook 
    Dim Ret As Variant 

    Set targetWorkbook = Application.ActiveWorkbook 

    ' get the customer workbook 
    filter = "Text files (*.xlsx),*.xlsx" 
    Caption = "Please Select an input file " 
    Ret = Application.GetOpenFilename(filter, , Caption) 

    If Ret = False Then Exit Sub 

    Set wb = Workbooks.Open(Ret) 

    wb.Sheets(1).Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count) 

    ActiveSheet.Name = "DATA" 
End Sub 
+0

어떻게 이전 버전의 엑셀 파일, 즉 .XLS을 포함 할 수있다? – AME

+2

변경'filter = "텍스트 파일 (* .xlsx), * .xlsx"''filter = "텍스트 파일 (* .xls *), *. xls *"' –

관련 문제