In Excel, using VBA — how do I import and insert d

2019-08-30 12:27发布

问题:

I've got a sheet setup with the following VBA:

Sub PrintPDF()

Application.DisplayAlerts = False

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\<insert_username>\Desktop\macro\Book1.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False _

Application.Quit

End Sub

(which basically opens prints a PDF of the file and closes excel)

Then I've got this in the workbook:

Private Sub Workbook_Open()
Run "PrintPDF"
End Sub 

(which runs the PrintPDF macro "on load" -- meaning when first thing when the excel file is opened.)

QUESTION:

So, what I'm missing now to finish out this task is some nice clean code to import a delimited file "on load" and map it to a fixed point in a single sheet.

Sample Data (with header row):

ID<TAB>Name<TAB>Location
1<TAB>John<TAB>US
2<TAB>Mike<TAB>CN
3<TAB>Tom<TAB>CA

Sample Excel Rows (after "on load" insert and mapping to cells):

<A1>null<B1>null<C1>null<D1>null
<A2>null<B2>ID<C2>Name<D2>Location
<A3>null<B3>1<C3>John<D3>US
<A4>null<B4>2<C4>Mike<D4>CN
<A5>null<B5>3<C5>Tom<D5>CA

(offset the data mapping because I'd like to make sure that I'm able to map the imported data to any where in the excel file as long as the row and column count match in both ends; both ends being the excel file and the delimited data source file; which they will 1-for-1.)

If you have any questions, let me know. The target system is Window 7 (Office 2010) or Mac 10.5 (Office 2011) -- the code above is for Windows, but the only difference I believe would be the filename code, that being: "C:\Users\<insert_username>\Desktop\macro\Book1.pdf"


UPDATE:

Here's the code I have so far, anything wrong with it:

Sub ImportCSV()
'
' ImportCSV Macro
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\<insert_username>\Desktop\macro\sample_pipeline_data.txt", Destination:= _
        Range("$B$2"))
        .Name = "sample_pipeline_data"
        .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 = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

回答1:

VBA is posted above in the body of my question, but the simple answer is that you can just record a macro using the built in PDF functions to automate this. One thing I would suggest is that you test this marco (or any marco) on the platforms it will be running on. For example, in the VBA code above which was generated using Office 2010 on Windows 7 -- it will not work on Mac 10.5 (Office 2011) without running it in the debugger and deleting a few attributes that Mac's version of office doesn't support (though based on my testing, it appears very likely that they're not supported by the Window's version either, but just don't throw errors.) If you ever have questions about the code/answer -- just comment and I've be happy to provide any additional info.