导入和对齐工作表上的数据

时间:2014-05-19 01:35:58

标签: excel vba excel-vba

有没有办法将工作簿中的已关闭工作表导入到当前打开的工作簿中的特定工作表并对齐表头?所有我看到的都是导入访问并导入到powerpoint,我无法找到导入excel excel。我会通过选择目录来选择文件。我怎么做到这一点?

截至目前,这是我的代码:

Private Sub CommandButton4_Click()
    Dim Filter As String, Title As String
    Dim FilterIndex As Integer
    Dim Filename As Variant
    On Error Resume Next
    Filter = "Excel Files (*.xlsx), *.xls,"
    FilterIndex = 3
    Title = "Select a File to Open"
    ChDrive ("E")
    ChDir ("E:\excel")
    With Application
        Filename = .GetOpenFilename(Filter, FilterIndex, Title)
        ChDrive (Left(.DefaultFilePath, 1))
        ChDir (.DefaultFilePath)
    End With
    If Filename = False Then
        MsgBox "No file was selected."
        Exit Sub
    End If
    ImportMethod CStr(Filename)
End Sub

Sub ImportMethod(sFileName As String)

End Sub

1 个答案:

答案 0 :(得分:1)

这是我录制导入电子表格时发生的事情。

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 19/05/2014 by AUser    '

'
    With ActiveSheet.QueryTables.Add(Connection:=Array( _
        "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Users\AUser\Documents\Super.xls;Mode=Share Deny " _
        , _
        "Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet " _
        , _
        "OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;" _
        , _
        "Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy " _
        , _
        "Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
        ), Destination:=Range("D10"))
        .CommandType = xlCmdTable
        .CommandText = Array("Sheet1$")
        .Name = "Super"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceDataFile = "C:\Users\AUser\Documents\Super.xls"
        .Refresh BackgroundQuery:=False
    End With
End Sub