在 Excel 中,使用 VBA — 如何从分隔文件导入和插入数据? (示例代码和数据)
我有一个带有以下 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
它基本上打开打印文件的 PDF 并关闭 excel)
然后我在工作簿中得到了这个:(
Private Sub Workbook_Open()
Run "PrintPDF"
End Sub
它“加载”时运行 PrintPDF 宏 - 意思是当打开 Excel 文件时的第一件事。)
问题:
因此,为了完成此任务,我现在缺少的是一些漂亮的干净代码,用于“加载时”导入分隔文件并将其映射到单张纸中的固定点。
示例数据(带有标题行):
ID<TAB>Name<TAB>Location
1<TAB>John<TAB>US
2<TAB>Mike<TAB>CN
3<TAB>Tom<TAB>CA
示例 Excel 行(在“加载”插入并映射到单元格之后):(
<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
偏移数据映射,因为我想确保能够将导入的数据映射到任何位置在 Excel 文件中,只要两端的行数和列数匹配;两端都是 excel 文件和分隔数据源文件;它们将一对一。)
如果您有任何问题,请告诉我。目标系统是 Window 7 (Office 2010) 或 Mac 10.5 (Office 2011) - 上面的代码适用于 Windows,但我认为唯一的区别是文件名代码,即:“C:\Users\
"
更新:
这是我到目前为止的代码,有什么问题吗:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VBA 发布在我的问题正文中,但简单的答案是,您可以使用内置的 PDF 函数录制宏来自动执行此操作。我建议的一件事是您在它将运行的平台上测试这个宏(或任何宏)。例如,上面的 VBA 代码是在 Windows 7 上使用 Office 2010 生成的 - 如果不在调试器中运行它并删除 Mac 版本的 Office 不支持的一些属性,它将无法在 Mac 10.5 (Office 2011) 上运行(虽然根据我的测试,Windows 版本很可能也不支持它们,但不要抛出错误。)如果您对代码/答案有疑问 - 只需发表评论,我已经很乐意提供任何附加信息。
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.