[英]Excel: Specify table columns in .csv export macro
I have a workbook with several worksheets that each contain an inventory list. 我有一个包含几个工作表的工作簿,每个工作表都包含一个清单。 Within that workbook I have another "Generate Order" worksheet that contains a table, which I've set up to consolidate the data from the other sheets.
在该工作簿中,我还有另一个“生成订单”工作表,其中包含一个表,该表已设置为合并其他表中的数据。 I would like a button on that sheet called "Export Order," which would export the contents of the
generateOrder
table as a .csv file. 我想在该工作表上创建一个名为“导出订单”的按钮,该按钮会将
generateOrder
表的内容导出为.csv文件。
The catch is, in order to upload the order to the vendor's system, the .csv file must only contain the item number and the quantity, set up as "number,quantity" with each item on its own line. 要注意的是,为了将订单上载到供应商的系统,.csv文件必须仅包含商品编号和数量,并设置为“数量,数量”,并将每个商品放在自己的行中。
Currently I have this macro set up: 目前,我已经设置了此宏:
Sub export_button()
Dim tbl As ListObject
Dim csvFilePath As String
Dim fNum As Integer
Dim tblArr
Dim rowArr
Dim csvVal
Set tbl = Worksheets("Generate Order").ListObjects("generateOrder")
csvFilePath = "C:\Users\username\Desktop\order.csv"
tblArr = tbl.DataBodyRange.Value
fNum = FreeFile()
Open csvFilePath For Output As #fNum
For i = 1 To UBound(tblArr)
rowArr = Application.Index(tblArr, i, 0)
csvVal = VBA.Join(rowArr, ",")
Print #1, csvVal
Next
Close #fNum
Set tblArr = Nothing
Set rowArr = Nothing
Set csvVal = Nothing
End Sub
Through a lot of Googling I've managed to get it working so that it does export the contents of generateOrder
and save it as a .csv, but I'm trying to figure out the following modifications: 通过大量的Google搜索,我设法使其正常运行,以便它确实导出
generateOrder
的内容并将其保存为.csv,但我试图找出以下修改:
PUBLISHER ITEM #
, REORDER QTY
(in that order). PUBLISHER ITEM #
, REORDER QTY
的两列( REORDER QTY
顺序)。 If it matters, there's a line break between PUBLISHER and ITEM in the first column header. value in cell C3
-order- current date
.csv value in cell C3
-order- current date
.csv value in cell C3
, and then save the file there (instead of to the Desktop as it currently does). value in cell C3
名为value in cell C3
Orders内创建一个子文件夹,然后保存文件那里(而不是当前的桌面)。 I need to figure out all of that but if you can at least help me figure out how to get only the two columns, I can try figuring out how to save it the way I want later. 我需要弄清楚所有这些内容,但是如果您至少可以帮助我弄清楚如何仅获取两列,那么我可以稍后尝试找出如何保存它的方法。 Many thanks!
非常感谢!
Collect the column index numbers of the two desired columns from the listobject's header row and use those to parse the array of databodyrange values. 从列表对象的标题行中收集两个所需列的列索引号,然后使用它们来解析databodyrange值的数组。
dim c1 as long, c2 as long
Set tbl = Worksheets("Generate Order").ListObjects("generateOrder")
csvFilePath = "C:\Users\username\Desktop\order.csv"
tblArr = tbl.DataBodyRange.Value
'you should really know where the two columns are but this should fetch their
'position from the header row. Hard-code the positions if you run into trouble.
c1 = application.match("publisher" & vblf & "item #", tbl.HeaderRowRange, 0)
c2 = application.match("reorder qty", tbl.HeaderRowRange, 0)
fNum = FreeFile()
Open csvFilePath For Output As #fNum
For i = LBound(tblArr, 1) To UBound(tblArr, 1)
csvVal = Join(array(tblArr(i, c1), tblArr(i, c2)), ",")
Print #1, csvVal
Next
Note that I'm looking up "publisher" & vblf & "item #"
with no space after publisher . 请注意,我在找了
"publisher" & vblf & "item #"
与出版商后没有空间。 Multi-line values in an excel cell only use line feed; excel单元格中的多行值仅使用换行; not carriage return and line feed.
不是回车和换行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.