Excel 导入 SQL 数据,但导入后更改 1 列的顺序
我从在 MS Query 中创建的查询导入数据。 假设查询编辑器中的列按 A、B、C、D、E、F 的顺序排列。 一旦我保存查询,将数据返回到 Excel,导入的数据就会具有新的列顺序
A、B、C、F、D、E - 请注意,F 列已移至 D 所在的位置。
关于如何解决这个问题有什么想法吗?
谢谢你们。假设变量定义正确,并且如果您愿意,则忽略代码试图执行的操作,保留部分不起作用
For Each wks In ThisWorkbook.Worksheets
Set qt = wks.QueryTables(1)
qt.PreserveColumnInfo = True
qt.PreserveFormatting = True
If wks.Name <> "Master" And wks.Name <> "Parameters" Then
wks.Range("A2:A1000").EntireRow.Copy Destination:=Worksheets("Master").Range("A65536").End(xlUp).Offset(1, 0)
End If
Next wks
I do an import data from a query I created in MS Query.
Lets say the columns are in order A,B,C,D,E,F in the Query Editor.
Once the I save the query, return data to Excel, the imported data has a new column order
A,B,C,F,D,E -- note the F column was moved where D was.
Any ideas on how to solve this issue?
Thanks guys. Assume variables are defined correctly and disregard what the code is trying to do if you want, the preserving part is not working
For Each wks In ThisWorkbook.Worksheets
Set qt = wks.QueryTables(1)
qt.PreserveColumnInfo = True
qt.PreserveFormatting = True
If wks.Name <> "Master" And wks.Name <> "Parameters" Then
wks.Range("A2:A1000").EntireRow.Copy Destination:=Worksheets("Master").Range("A65536").End(xlUp).Offset(1, 0)
End If
Next wks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QueryTable
对象有两个名为PreserveColumnInfo
和PreserveFormatting
的属性,它们应该可以帮助您。 (还有AdjustColumnWidth
,但我不确定您是否需要担心这个)。您应该能够使用类似于以下的代码来帮助保留列信息:There are two properties of the
QueryTable
object calledPreserveColumnInfo
andPreserveFormatting
, which should help you out. (There's alsoAdjustColumnWidth
, but I'm not sure if you need to worry about that one). You should be able to use code similar to the following to help preserve the column information: