2013-02-17 2 views
1

서버에 호스팅 된 Excel 파일을 통합 문서로 가져 오기 위해 매크로를 작성하려고합니다. 이것은 내가 지금까지 가지고있는 코드이다.서버의 Excel 파일을 다른 Excel 파일로 가져 오기

Sub ranker() 
' 
' ranker macro 
' 

' 
    Range("Ranker!A10:Ranker!Z100").ClearContents 
    URL = Range("url!F2" & i).Text 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "FINDER;" & URL _ 
     , Destination:=Range("Ranker!$A$1:$Z$100")) 
     .Name = "ranker" 
     .CommandType = xlCmdTable 
     .CommandText = Array("'MTD SAR$'") 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .PreserveColumnInfo = True 
     .Refresh BackgroundQuery:=False 
    End With 
End Sub 

유일한 문제는 다음과 같은 대화 상자가 표시된다는 것입니다.

dialog box

나는 옵션 'MTD SAR $'모든 시간을 선택해야합니다. 대화 상자를 피하기 위해 VBA 코드에서이 옵션을 선택할 수 있습니까? 어떤 도움이라도 대단히 감사하겠습니다.

답변

0

원하는 시트를 지정할 수있는 방법이있는 querytable 메서드가 기억 나지 않습니다. OLEDB를 사용하여이 문제를 해결했습니다. Query table with Excel as Data Source

:

Sub Excel_QueryTable() 

    Dim oCn As ADODB.Connection 
    Dim oRS As ADODB.Recordset 
    Dim ConnString As String 
    Dim SQL As String 

    Dim qt As QueryTable 

    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\SubFile.xls;Extended Properties=Excel 8.0;Persist Security Info=False" 
    Set oCn = New ADODB.Connection 
    oCn.ConnectionString = ConnString 
    oCn.Open 

    SQL = "Select * from ['MTD SAR$']" 

    Set oRS = New ADODB.Recordset 
    oRS.Source = SQL 
    oRS.ActiveConnection = oCn 
    oRS.Open 

    Set qt = Worksheets(1).QueryTables.Add(Connection:=oRS, _ 
Destination:=Range("Ranker!$A$1")) 

    qt.Refresh 

    If oRS.State <> adStateClosed Then 
     oRS.Close 
    End If 


    If Not oRS Is Nothing Then Set oRS = Nothing 
    If Not oCn Is Nothing Then Set oCn = Nothing 

End Sub 

이 나는 ​​물론 불통, 사용 무엇인가 : 다음은 그 예이다

관련 문제